f.collection_select not displaying the selected value - ruby-on-rails

There are so many results for this search on google, and it's even asked at SO - but the solutions discussed so far are not helping me. Here's the issue: I have a form_for #company |f| and I am using f.collection_select for company_status_id - but when the form loads, I want the actual company status selected if it is set. Through the debugger I know, that it's been set, yet I am getting a default value displayed there. Here's the code:
= puts #company.company_status_id
= f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => #select_value}
Here's the generated htmnl
<select id="company_company_status_id" prompt="-Select-" name="company[company_status_id]">
<option value="1">-Not Available-</option>
<option value="2">Active</option>
<option value="3">Bankrupt</option>
<option value="4">Acquired</option>
</select>
And the conditions remain the same even if I do:
f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => #select_value, :selected => :selected => #company.company_status}
Or
f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => #select_value, :selected => #company.company_status}

This is what I finally did:
f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => #select_value, :selected => #company.company_status_id.to_i}
I read on of the answers on a similar question that collection_select automatically selects the selected value by making comparisons of what is passed with the attributes of collection. apparently there was a difference of their types, and comparing the int from CompanyStatus to the int of #company.company_status_id.to_i worked out. Though #company.company_status_id is supposed to be int as well. I can see that in the db. Anyway, it this line of code worked.
If anyone can exaplain, I will be much thankful!

If you use collection_select helper, syntax is very simple:
<%= f.collection_select :category_id, Category.all, :id, :name,
prompt: true, selected: #product.category_id %>
I hope this help

<% form_for(#company) do |f| %>
<%= f.select(:company_status_id, ListCache.all.map {|lc| [lc.name, lc.id]} ) %>
<% end %>

Sometimes you just need to go to the browser address bar and press enter. Normal reloading the page clicking the refresh button doesn't help. My problem was solved that way.

Use select_tag instead
<%= form_for(#product, :html => {:multipart => true}) do |f| %>
<%= select_tag("product[category_id]", options_for_select(#categories.map { |cat| [cat.name, cat.id] })) %>
<%end%>
Hope this help you.....

Related

Rails: collection_check_boxes to filter search results

I'm trying to implement a search view which enables users to search other users based on keyword AND multiple checkboxes for their tags. I'm still trying to figure out how to place the collection_check_boxes right, so that view is rendered correctly. Here's what I have so far:
<%= form_tag users_path, :method => 'get' do %>
<%= collection_check_boxes :options, :tag_ids, Tag.order(:name), :id, :name, {multiple: true} %>
<%= text_field_tag :search, params[:search], :placeholder => "Search", :id => "query" %>
<%= submit_tag "Search", :name => nil, :style => "float:left;"%>
<% end %>
Can you help me complete the view function above by making sure that, when a user clicks search collection_check_boxes will add something like tag=tag1&tag2&tag3 to the url?
I will try :)
Generally everything is OK. Only small correction, if you change this line:
<%= collection_check_boxes :options, :tag_ids, Tag.order(:name), :id, :name, {multiple: true} %>
to this:
<%= collection_check_boxes :tag, :ids, Tag.order(:name), :id, :name, {multiple: true} %>
after submitting request, in URL will be something like this:
&tag%5Bids%5D%5B%5D=&tag%5Bids%5D%5B%5D=3&tag%5Bids%5D%5B%5D=2&search=test
more human view:
&tag[ids][]=&tag[ids][]=3&tag[ids][]=2&search=test
and this is OK. It always look like this when sending an array. In this particular case I've checked two checkboxes (with id=2 and 3). I don't know why there is also empty element :), but this is not a problem. I think there is no way to get results exactly like tag=tag1&tag2&tag3
In your "users" model you can get to the params by (after submitting form):
params[:tag][:ids] - array of id (checked checkboxes)
params[:search] - search input value
If you want to have checked some checkboxes on start (or after submit), add to collection_check_boxes additional parameter:
options = {:checked=>tag_ids}
and deliver in variable tag_ids array of checkboxes id (which should be checked). All the code looks like this:
<%if params.has_key?(:tag)%>
<%tag_ids = params[:tag][:ids]%>
<%else%>
<%tag_ids = Array.new%>
<%end%>
<%=form_tag drons_path, :method => 'get' do %>
<%=collection_check_boxes :tag, :ids, Tag.order(:name), :id, :name, {multiple: true}, options = {:checked=>tag_ids}%>
<%=text_field_tag :search, params[:search], :placeholder => "Search", :id => "query"%>
<%=submit_tag "Search", :name => nil, :style => "float:left;"%>
<%end %>

Rails collection_select "selected" value is cancelled out by the default "prompt"

Couldn't find any reference to my problem in the API, so here goes:
<%= f.collection_select :category_id, #categories, :id,
:name, {prompt: true}, { selected: #selected_value } %>
My users arrive to the form from different links, and depending on the link, they get their categories pre-selected for them from the #categories set. Sometimes they come from a general page, so instead of a pre-selected option they see the default prompt.
Problem: with my current code prompt replaces the selected value.
Thanks for any advice!
So I went with placing a conditional inside my collection_select and now it works fine
<%= f.collection_select :category_id, #categories, :id, :name,
#selected_category ? {prompt: "Your text"} : {selected: #selected_category} %>

How to use a table column in my select box on rails?

I'm trying to create a select box that takes data from my db. I'm having trouble setting this up. I tried this code:
<%= f.fields_for :unit do |u| %>
<%= u.label :name %>
<%= u.select :name, :class => "ingredient_unit", :prompt => "Please Select" %>
<% end %>
but I'm missing the part of the choices, I don't know how to pull them out of the database. I tried using collection_select, which worked, but then the class option wasn't working... collection_select went like this:
<%= u.collection_select :unit, Unit.all, :id, :name, :class => "ingredient_unit", :prompt => "Please Select" %>
I also don't understand what the first symbol means (:unit), it seems to be setting the html id and name, so that can be anything I want it to be?
You should look at the documentation for collection_select and select. But to answer your question, for the select part, you forgot to pass the list of options to choose from. You also need to swap the order for prompt and class since prompt is an option for the helper and class is an html option
<%= u.select :unit_id, Unit.all.map { |u| [u.name, u.id] }, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
For the collection select
<%= u.collection_select :unit_id, Unit.all, :id, :name, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
The first parameter passed to both helper is the column name where you want the selected answer to be saved. The 2 codes above just shows 2 different ways to generate the same select tag.
The first symbol tells it which field to populate with the id returned from the user selection.
Also, you should wrap your class section in {}
:unit refers to the model attribute that you're using for the select element. Yes, it will setup the name/id of the element (and name is the most important for the params hash).
To set a class in the collection_select, specify it as a hash as that helper takes it as an html_option.
<%= u.collection_select :unit, Unit.all, :id, :name, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>

selected values are resetting in page reload in ruby on rails

Iam new to ruby on rails.I want to show the selected values from drop down and checked radio button in page reload.
When the page is reloaded, the selected value of the drop down and radio button are getting reset.So please let me know how to fix this issue.Here is the code i'am using.
<%= form_tag :action => 'show' do%>
<strong>Select device: </strong> <%= collection_select(:device, :id, #devices, :id, :name, options ={:prompt => "-Select a device"}) %>
<br></br>
<strong>Chose:</strong><%= radio_button_tag :name,:time, false, :onclick => "this.parentNode.submit();"%>Time
<%= radio_button_tag :name,:graph%>Graph
<% end %>
Try:
collection_select(:device, :id, #devices, :id, :name, {:selected => #your_selected_value, :prompt => "-Select a device"})
see radio_button_tag(name, value, checked = false, options = {})
<%= radio_button_tag :name,:time, #your_checked_value.eql?('time'), :onclick => "this.parentNode.submit();"%>Time
<%= radio_button_tag :name,:graph, #your_checked_value.eql?('graph') %>Graph

Rails select list reverts to top?

I have number of select lists in my rails application like this:
<li>Company<span><%= f.select :company_id, Company.all.collect {|m| [m.companyname, m.id]} %></span></li>
They all work well, except - sometimes if you go to the edit view, the select list reverts to the top item, not the item that was chosen when creating. So if you go to an edit view and then click update without actually making any changes, the lists default to the top item - even though you didn't touch them.
Is there a way around this?
Thanks,
Danny
EDIT:
<% form_for (#kase), :html => { :multipart => true } do |f| %>
<li>Appointed Surveyor<span><%= f.select :appointedsurveyor_id, Company.all.collect {|m| [m.companyname, m.id]}, {:selected => #kase.appointedsurveyor_id} %></span></li>
<li>Surveyor Name<span><%= f.select :surveyorperson_id, Person.all.collect { |x| [x.personname, x.id]}, {:selected => #kase.surveyorperson_id} %></span></li>
I have tried the above, but sadly it still seems to revert to the default value.
I'm stuck on this, I can't find any tutorials etc on this at all.
EDIT 2:
<li>Appointed Surveyor<span><%= f.select :appointedsurveyor_id, Company.all.collect {|m| [m.companyname, m.id]}, {:selected => (#kase.appointedsurveyor_id rescue "")} %></span></li>
<li>Surveyor Name<span><%= f.select :surveyorperson_id, Person.all.collect { |x| [x.personname, x.id]}, {:selected => (#kase.surveyorperson_id rescue "")} %></span></li>
You might be able to make sure of it with the third parameter, try the following code:
<li>Company<span><%= f.select :company_id, Company.all.collect {|m| [m.companyname, m.id]}, {:selected => #your_instance_name.company_id} %></span></li>
Make sure that you replace #your_instance_name with the instance variable you use at the form_for tag.
Hopefully that helps.
It turns out I had the fields in the database as strings not integers.

Resources