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" %>
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'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)
I'm working on some app and I have 2 models. Categories in which I create category names and Question. Category has_many questions and Question belongs_to Category.
I've added category_id to Question model.
Now I need to take all Category_names and display them in form where I create Question so User can choose in which category_name will save question.
I've tried something like this in firs line of code but not working.
<%= f.input :category_id, Category.all.map(&:name) %>
<%= f.input :question_name, wrapper: :vertical_text_input, as: :text %>
<%= link_to "Markdown help", "http://assemble.io/docs/Cheatsheet-Markdown.html", target: "_blank", class: "right" %>
<%= f.input :answer %>
<%= f.input :image, as: :attachinary %>
QUESTION: How to display all Category names in form where I create new Questions?
you may wanna use a select box and use the collection select rails helper :
f.collection_select(:category_id, Category.all, :id, :name)
Also you can use this way.
<%= f.select :category_id, Category.all.map(&:name), {prompt:"Choose Category"}%>
#Jhon suggestion.
<%= f.select :category_id, Category.pluck(:name), {prompt:"Choose Category"}%>
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.
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