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)
Related
I have this code within my rails form:
Categories: <%= f.collection_select :tag_ids, Tag.order(:name), :id, :name, {}, {multiple: true} %>
This code is working, but I want to use simpleform gem to redesign my form. However, I cannot seem to figure how to 'translate' this code into simple form. Anyone have any idea how? Thanks.
Something like this should do the trick:
If you have a many to many relation you could first try what the default does.
<%= f.association :tags %>
If the defaults don't work out you can make an explicit collection:
<%= f.input :tag_ids, as: :select, collection: Tag.order(:name), label_method: :name, input_html: {multiple: true} %>
# or
<%= f.input :tag_ids, as: :select, collection: Tag.order(:name).pluck(:name, :id), input_html: {multiple: true} %>
Alternatively if you define the Tag#to_label method you don't have to pass the name of the label method. The Tag#id gets used as default value method. If you would like another value specify the method like so: value_method: :something_else.
See the simple_form Usage section (intro, collections and associations).
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.
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" %>
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.
how can we define id for this rails select statement , i have tried doing in this way like
<%= f.select :state, options_for_select(Contact::STATES), :id=>"state_job" %>
but it is not showing any id when i inspect it in the browser. Please help me out
<%= f.select :state, options_for_select(Contact::STATES) %>
The select tag helper looks for options, then html_options, you just need to make sure your id is in the right place (html_options) by passing something to the options parameter:
<%= f.select :state, options_for_select(Contact::STATES), {}, {:id=>"state_job"} %>