Is it possible to add an at the end of a
created with the
collection_select
helper method?
Right
Short answer: no.
Long answer: Sure, but you have to be crafty.
Create a class like so:
class New_option_placeholder
def id
"new"
end
def name
"...or create a new one"
end
end
Instead of passing @categories
, pass @categories+New_option_placeholder.new
If (as indicated by the comments) you're looking for something terser you could require "ostruct"
and then pass @categories + [OpenStruct.new(:id => 'new',:name => '...or create a new one')]
to accomplish essentially the same this.
Agreed on the short answer No and long answer " Be Crafty ", but here's what I just did which I think is simpler than either of these two solutions and worked for me:
Wrap the next line inside erb tags i.e. <%=
and %>
:
f.collection_select :advertisement_group_id, AdvertisementGroup.find(:all, :order => "name DESC") << AdvertisementGroup.new(:name => "New Group"), :id, :name, :include_blank => true
Just create a new object with .new
and pass in whatever text you want displayed along with :include_blank => true
.
I can't comment otherwise I'd add this to the answer above.
To get the option->value "new","or create a new one".. instead of
f.select(:category_id, @categories.collect {|p| [ p.name, p.id ] } + ['Or create a new one','new'], {:include_blank => 'Please select a category'})
do
f.select(:category_id, @categories.collect {|p| [ p.name, p.id ] } + [['Or create a new one','new']], {:include_blank => 'Please select a category'})
note the extra [] around the options. This makes the array work as an option,value pair
You should probably use select
instead.
Like so:
f.select(:category_id, @categories.collect {|p| [ p.name, p.id ] } + [ [ 'Or create a new one', 'new' ] ], {:include_blank => 'Please select a category'})
Good luck!