Populating Dropdown SimpleForm

I’m working on populating this dropdown and finding myself having some
trouble even though in theory it would be pretty simple. I’m used to
rails
4 and we are using rails 3 on this project as well as haml which I’m
familiar with, but not super comfortable with.

_extra_fields.html.haml
= form.input :category_id, as: :select, collection: Category.all.map{|x|
[x.name,x.id]}, input_html: {:style => “width:300px”}, :include_blank =>
true
= form.input :sub_category_id, as: :select, collection:
@package.category.sub_categories.map{|x| [x.name,x.id]}, input_html:
{:style => “width:300px”}, :include_blank => false

On 22 July 2015 at 02:01, Simone Battiste-Alleyne [email protected]
wrote:

@package.category.sub_categories.map{|x| [x.name,x.id]}, input_html: {:style
=> “width:300px”}, :include_blank => false

A clue to the problem you are having would be helpful. It can often
be useful to look at the html generated to see how that differs from
what you would expect.

Colin

This works for me in Rails 3
<%= form_for … do |form| %>
<%= form.collection_select(:category_id, Category.all, :id,
:category_type)
%>

I am not super familiar with HAML, but I googled github haml
collection_select and the code examples that I saw seem consistent with
what I am offering…

collection_select prototype from
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select:
collection_select(object, method, collection, value_method, text_method,
options = {}, html_options = {})

Returns and tags for the collection of existing return
values of method for object’s class. The value returned from calling
method
on the instance object will be selected. If calling method returns nil,
no
selection is made without including :prompt or :include_blank in the
options hash.

The :value_method and :text_method parameters are methods to be called
on
each member of collection. The return values are used as the value
attribute and contents of each tag, respectively. They can also
be
any object that responds to call, such as a proc, that will be called
for
each member of the collection to retrieve the value/text.

Rewrite:
= form_for … do |form|
= form.collection_select(nil, :category_id, Category.all, :id, :name,
{:include_blank => true}, {:style => “width:300px”})
## I think that you may omit nil as the object, maybe your object
here
is @package??
= form.collection_select(nil, :sub_category_id,
@package.category.sub_categories.all, :id, :name, {:include_blank =>
false}, {:style => “width:300px”})

Hope this helps

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs