Ruby on Rails Collection Select - How to Pre-Select the Right Value

Ruby on Rails Collection select - how to pre-select the right value?

collection_select doesn't support the selected option, in fact, it doesn't need it.
It automatically selects the option whose value matches the value of the form builder object.

Let me show you an example. Assuming each post belongs to a category.

@post = Post.new

<% form_for @post do |f| %>
<!-- no option selected -->
<%= f.collection_select :category_id, Category.all, :id, :name, :prompt => true %>
<% end %>

@post = Post.new(:category_id => 5)

<% form_for @post do |f| %>
<!-- option with id == 5 is selected -->
<%= f.collection_select :category_id, Category.all, :id, :name, :prompt => true %>
<% end %>

EDIT:

I'd suggest to use representative variable names. Use @categories instead of @category. :)
Also, split the update logic from the read-only view.

def categories #Step 2
@listing = Listing.find(params[:listing_id])
@seller = Seller.find(@listing.seller_id)
@categories = Category.find(:all)
@listing.complete = "step1"

respond_to do |format|
if @listing.update_attributes(params[:listing])
flash[:notice] = 'Step one succesful. Item saved.'
format.html #categories.html.erb
end
end
end

<% form_for @listing do |f| %>
<%= f.collection_select :category_id, @categories, :id, :name, :prompt => true %>
<% end %>

If it doesn't work (that is it selects the prompt) it means either you don't have a category_id associated to that record or the Category collection is empty. Be sure to not reset the value of category_id for @listing somewhere before the object is passed to the form.

EDIT 2:

class Category
def id_as_string
id.to_s
end
end

<%= f.collection_select :category_id, Category.all, :id_as_string, :name, :prompt => true %>

Rails: Preselect a value in ActionView-Helper 'collection_select'

From the docs:

Sample usage (selecting the associated Author for an instance of Post, @post):

collection_select(:post, :author_id, Author.all, :id, :name_with_initial)

If @post.author_id is already 1, this would return:

<select name="post[author_id]">
<option value="">Please select</option>
<option value="1" selected="selected">D. Heinemeier Hansson</option>
<option value="2">D. Thomas</option>
<option value="3">M. Clark</option>
</select>

So you just need to make sure that @my_object.my_method returns a value that matches one of the available option values. If there's a match then that option will be selected.

HOw do I pre-select a value in my select menu if a cookie is set?

Hey according to docs at APIdock:

collection_select(:post, :author_id, Author.all, :id, :name_with_initial, {:selected => current_book.authors.map(&:id)})

So You should write it like this:

In You controller specify you objects: @selected = cookies[:some_key]

collection_select(:user_object, :object, @objects, :id, :description, {:selected => @selected.map(&:id), :prompt => true})

You do not need whole objects, just the ids to check out selected ones. I think this should do the trick.

f.collection_select not displaying the selected value

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!

Rails select helper - Default selected value, how?

This should do it:

<%= f.select :project_id, @project_select, :selected => params[:pid] %>

Preselect value dynamically in a select

You need to use options_from_collection_for_select which allow you to specify which option to select

Documentation:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_from_collection_for_select

In this example, the Shop with id 17 will be selected:

<%= f.select :shop_id, options_from_collection_for_select(Shop.all, :id, :name, 17) %>

How to pre-select in options_from_collection_for_select?

Nevermind, I solved it with this answer:

How to make the select_tag keep value of last search?

:selected => params[:country]

Ruby on rails: Select options menu with default value attribute

<%= f.select(:parent_id, [["==None==", 0]] + @parent_menus.collect {|p| [ p.name, p.id ] }) %>


Related Topics



Leave a reply



Submit