Rails collection_select , and value automatically added in textfield - textfield

Hi i am currently using Collection_select like so , and as you can see its working properly but i want to make it, so that when i pick 1 item from the Item field that the data from my database , bought price and sold price would automatically be added into the textfields like in the example below.
but as to how i can do it, remains a mystery to me, can anybody help me with this and thanks! any help will be helpful! and thanks again!
the code that i used
<%= form_for(#sale) do |f| %>
<% if #sale.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#sale.errors.count, "error") %> prohibited this sale from being saved:</h2>
<ul>
<% #sale.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :item_id %><br>
<%= collection_select( :sale, :item_id, Item.all, :id, :name, {} ) %>
</div>
<div class="field">
<%= f.label :bought %><br>
<%= f.text_field :bought %>
</div>
<div class="field">
<%= f.label :sold %><br>
<%= f.text_field :sold %>
</div>
<div class="field">
<%= f.label :number %><br>
<%= f.number_field :number %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

you can do this by send ajax request and bought data from database then show "bought price" and "sold price" in there respective fields. Something like that
$("#collection_select_id").change(function () {
$.ajax({
url: "your_url_where_you_get_your_data",
method: "GET",
data: {},
success: function(data) {
$("#bought_price_id").val(data.bought_price)
$("#sold_price_id").val(data.sold_price)
}
});
});
Thanks

Related

How to setup Prismjs inside rich_text_area (Rails 7)

Trying to figure out how to use Prismjs inside rich_text_area so I can use it for syntax highlighting in a blog with Rails 7.
_form.html.erb- currently working great as below
<%= form_with(model: blog) do |form| %>
<% if blog.errors.any? %>
<div style="color: red">
<h2><%= pluralize(blog.errors.count, "error") %> prohibited this blog from being saved:</h2>
<ul>
<% blog.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form.label :title, style: "display: block" %>
<%= form.text_field :title %>
</div>
<div>
<%= form.label :content, style: "display: block" %>
<%= form.rich_text_area :content %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
As you can see in both instances where I have input the code using the "code block" and by simply adding it to the "text area" the results are the same.
When I add the same code to any other page it works as expected.

How to show file upload button once in a nested form in rails 4?

I have a parent (playlist) -> child (tracks) structure in my rails app.
What I am doing in the form is showing attributes from the parent and then I have a table for the child attributes.
<%= form_for(#playlist) do |f| %>
<% if #playlist.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#playlist.errors.count, "error") %> prohibited this playlist from being saved:</h2>
<ul>
<% #playlist.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Playlist Name: " %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label " Image: " %>
<%= f.file_field :photo %>
</div>
<br>
<div class="field">
<h5> description: </h5>
<br>
<%= f.text_area :description, :size => "80x3"%>
</div>
<br>
<div>
<h5> Add some music: </h5>
</div>
<div class="field">
<%= f.fields_for :tracks, Track.new do |ff| %>
<%= ff.file_field :audio, :multiple => true %>
<% end %>
</div>
<% if !#playlist.tracks.blank? %>
<table id="tracks" class="display">
<thead>
<tr>
<th>Delete</th>
<th>Track</th>
<th>Album</th>
<th>Artist</th>
<th>Label</th>
</tr>
</thead>
<tbody>
<%= f.fields_for :tracks do |ff| %>
<%= render "track_fields", :f => ff %>
<% end %>
</tbody>
</table>
<% end %>
<div class="actions">
<%= f.submit "Save" %>
</div>
<% end %>
I am using paperclip to upload the files. I am uploading images on playlist model and mp3s on the tracks model.
The code in question is the following:
<div class="field">
<%= f.fields_for :tracks, Track.new do |ff| %>
<%= ff.file_field :audio, :multiple => true %>
<% end %>
</div>
I feel this is a hack. Why do I have to add a new track for me to show the file_field (choose file button) once? Towards the bottom of the form, you will notice that I have to call
<%= f.fields_for :tracks do |ff| %>
again because I want to iterate through all the tracks for the playlist and show them in a table.
Not sure if this question makes sense, but how do I show the file_field once (I feel Track.new is a hack and I think its messing up in saving the model)?
EDIT:
here is a screenshot of what happens if I don't have Track.new:

Hide 'Publish' Button From non Admins?

Am trying to hide this publish 'tick-box' from non-admin users. I used the CanCan plug-in and set up the correct permissions but am struggling with the code syntax. I have used <%= if can? :publish, article %> in the views/articles/_form.html.erb partial but it doesn't work?
<div class="field">
<%= f.label :tag_names, "Tags" %> <br />
<%= f.text_field :tag_names %>
</div>
<div class="field">
<%= check_box("article", "published" ) %>
**<%= if can? :publish, #article %>**
<%= "Publish article" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
You should be using <%, not <%=. Also, the if statement is in the wrong place, and there's no closing end statement. Here's correct code:
<div class="field">
<%= f.label :tag_names, "Tags" %> <br />
<%= f.text_field :tag_names %>
</div>
<% if can? :publish, #article %>
<div class="field">
<%= check_box("article", "published" ) %>
<%= "Publish article" %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>

Rails 3 - Forms - Jquery :remote make duplicates and prompts 2x?

Hi
Im working with a form and have added :remote => true to the form and the destroy link. But when I do that this happends:
I submit the form and it makes 2 versions that are the same.
I destroy, I get prompted twice?
If i remove the :remote => true it only makes one version and only prompt once.
This is my form
<%= form_for([#comment], :remote => true) do |f| %>
<% if #comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
<ul>
<% #comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %>
<br/>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :body %>
<br/>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.hidden_field :user_id %>
<%= f.hidden_field :retailer_id %>
<%= f.hidden_field :product_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This is my destroy link
<%= link_to 'Destroy', comment, :confirm => 'Are you sure?', :method => :delete, :remote => true %>
Am I doing something very wrong here? Thanks in advance!
I think you have included your rails.js file twice. that might be the problem. check it.

Rails 3 dropbox help

i did a scaffold to make my DB and menus for group i need a drop down box instead of a text field
here is the code i have atm
<%= form_for(#ad) do |f| %>
<% if #ad.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#ad.errors.count, "error") %> prohibited this ad from being saved:</h2>
<ul>
<% #ad.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :ad_name %><br />
<%= f.text_field :ad_name %>
</div>
<div class="field">
<%= f.label :group %><br />
<%= f.text_field :group %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :credits %><br />
<%= f.text_field :credits %>
</div>
<div class="field">
<%= f.label :image_url %><br />
<%= f.text_field :image_url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
if you can let me know how to change group to a pull down with selections it would be helpful.
Have you tried changing f.text_field to f.select?
You need something like
<div class="field">
<%= f.label :group %><br />
<%= f.select :group, Group.all.collect{|g| [g.id, g.name]} %>
</div>
That is to say, pass as second argument the list [id, label] of the field you want to generate.