Rails collection_select vs f.collection_select - ruby-on-rails

I can't understand the difference between the two. Can someone please explain the difference when using form_for?
Say you have this: <%= form_for(#post) do |f| %>
Examples- When would you use this?
<%= collection_select(:post, :author_id, Author.all, :id, :name_with_initial, prompt: true) %>
vs. use this?
<%= f.collection_select(:post, :author_id, Author.all, :id, :name_with_initial, prompt: true) %>
Rails Api

When you use f.some_form_helper the helper will already know the name of the model you want to make the field name for. This way you can drop that :post argument. form_for(#post) gives you the f form builder object that knows what model the form is for.
With the regular collection_select (or any other helper with f.) you have to pass in, as the first argument, the name of the model the field is for.
Your example is a bit off because you passed in the same arguments to both. f.collection_select doesn't need the :post.
This is correct use of the non f. helper:
<%= collection_select(:post, :author_id, Author.all, :id, :name_with_initial, prompt: true) %>
This is a corrected way to use the f. helper:
<%= f.collection_select(:author_id, Author.all, :id, :name_with_initial, prompt: true) %>
the f object has a reference back to the model you passed in to form_for via f.object. This is how it knows to call collection_select(:post, ...) under the hood.

Related

Ruby on Rails collection output in collection_select

I've got this code:
<%= collection_select :channel, #channelList, :id, :channelname, {prompt: (t "channel.add.prompt")}, class: "form-control"%>
And got this error:
Its a normal active relation
Whats my mistake in this case?
thanks
http://edgeapi.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
You have to specify: object, method, collection, value_method, text_method, you probably forgot to set the object param.
Which would make this:
<%= collection_select :object, :channel, #channelList, :id, :channelname, {prompt: (t "channel.add.prompt")}, class: "form-control"%>
(replace :object with your object you want to save the channel to)

undefined method `merge' for :name:Symbol Rails 4.1.x collection_select

I have a form element with:
<%= f.collection_select :race, :id, Race.all, :id, :name, prompt: true %>
This allows you to select your characters race in a text adventure I am creating. The goal is to have a drop down with all available races, select by name and have the params pass the id of that race back.
But when I load the page I get undefined method 'merge' for :name:Symbol.
I looked up the docs and I think I am doing it right, but I guess not? what am i doing wrong?
The f. indicates you are in a form_for block? Which means the method signature of f.collection_select is different to just plain collection_select. The first parameter is automatically supplied by the FormBuilder, so if the :race is an attribute of the form object, which I assume is a Character, you just need:
<%= f.collection_select :race, Race.all, :id, :name, prompt: true %>
See the documentation for the FormBuilder#collection_select method.

Use array from model in form drop down

Ruby on Rails 4
In my model I have:
#category_check = ['cables', 'printers', 'monitors', 'accessories', 'towers', 'interaction']
It is used for a validates, now I want to display the array as options in my form. This does not work:
<%= f.label :category %><br>
<%= f.collection_select :category, #category_check, {prompt: "Select Category"}, class: "input-lg" %>
Do I have to make another instance variable in my controller or is there a way to display each in a drop down from the model variable? Thank you
You need to call something like this
<%= f.collection_select :category, #category_check, :to_s, :to_s, {prompt: "Select Category"}, class: "input-lg" %>
This will pass :to_s to each element of the the #category_check collection.
You should be able to access that array through the model, assuming you have one instantiated:
#my_model.category_check
However it looks like those categories are static, so ideally they'd be a class-level constant:
class MyModel
CATEGORY_CHECK = ['cables', 'printers', 'monitors', 'accessories', 'towers', 'interaction']
end
Then you could access it anywhere, without an instance of the class around:
MyModel::CATEGORY_CHECK
As Lalitharyani mentions in his answer, you'll also need to provide name and value methods to call on each item of the array so that the form helper knows what to display.
You are probably missing one parameter on the method call:
<%= f.collection_select :category, #category_check, {prompt: "Select Category"}, class: "input-lg" %>
should be:
<%= f.collection_select :category, :category_id, #category_check, {prompt: "Select Category"}, class: "input-lg" %>

Rails form dropdown using two text_method

I am creating a form has a drop down selection. I want to use two "text_method"s for the input but I am unsure how to do this. I want to include the year and name (both are two different columns in my rails model.
Here is what I have but it does not work:
<%= f.collection_select :bat_id, Bat.all, :id, :model_year, :model_name, include_blank: true %>
Here is the official documentation- http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
Use this in your view:
<%= f.collection_select :bat_id, Bat.all, :id, :model_year_and_name, include_blank: true %>
Add a method like this to your Bat model:
def model_year_and_name
"#{model_year} #{model_name}"
end

Rails collection_select not working but equivalent select with options_from_collection_for_select does

Here is my code snippet that works exactly as intended:
<%= f.select(:other_model_id,
options_from_collection_for_select(
OtherModel.all,
:id,
:full_name,
{ :selected => #this_model.other_model_id} )) %>
But for some reason, this doesn't work:
<%= f.collection_select :this_model, :other_model_id,
OtherModel.all, :id, :full_name %>
There error I get is:
undefined method `merge' for :full_name:Symbol
Any suggestions? The fact that :full_name works properly with the working code leads me to believe I screwed up the syntax in the simplified collection_select code and that the problem is not elsewhere.
I think you're mixing up two different collection_select methods. You're calling the FormBuilder#collection_select using the FormOptionsHelper#collection_select arguments. Maybe you want this:
<%= f.collection_select :other_model_id, OtherModel.all, :id, :full_name %>
Or perhaps this:
<%= collection_select :this_model, :other_model_id, OtherModel.all, :id, :full_name %>
You end up trying to put :full_name in the options argument but that's supposed to be a Hash, that's where the complaint about "no merge method" comes from.

Resources