I have this link in my code:
link_to "New question", new_question_url(:category_id => #category.id)
I have this code in my new question form:
<p>
<%= f.label :category_id %><br />
<%= f.collection_select :category_id, Category.all, :id, :name %>
</p>
How can I make Rails automatically select the category from the category_id querystring item, so it is the default one in the collection_select?
Thanks.
Your need to pass in a reference to the current object like this:
<%= f.collection_select(:your_object, :category_id, Category.all, :id, :name) %>
collection_select(:post, :author_id, Author.all, :id, :name_with_initial, {:prompt => true})
"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."
See here:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
Related
I am getting this error in the Production site and this is the code in my views
<%= form_for [:admin, #course], :remote => true do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :duration %>
<%= f.number_field :duration, class: "input-md form-control mb-20"%>
<%= f.label :program_id %>
<%= f.collection_select :program_id, Program.where('id'), :id, :name, {}, {class: "input-md form-control mb-20" } %>
<%end%>
This works in my local server where i have sql db setup.
Program model
has_many :courses
can anyone guide me?
Where clause isn't calling anything to compare with, so PG doesn't know what to include in the results. A where clause must evaluate to true/false.
just replace
<%= f.collection_select :program_id, Program.where('id'), :id, :name, {}, {class: "input-md form-control mb-20" } %>
by
<%= f.collection_select :program_id, Program.all, :id, :name, {}, {class: "input-md form-control mb-20" } %>
If you have some problem with some of the Program in your data base, add a column as status in programs table and make changes here as
<%= f.collection_select :program_id, Program.where("status =?", true), :id, :name, {}, {class: "input-md form-control mb-20" } %>
Using a bootstrap and rails along with datatables but keep getting a blank option on the datatable instead of a List helper that displays a topic label in the options list.
<%= f.label :activity, 'Select Activity' %><br />
<%= f.select :activity, options_for_select([
['#', 'Subject'],
['Math', 'Math'],
['Science', 'Science'],
['English', 'English']
]) %>
Here's how it looks on the filter page listing all postings should have the subject field displayed instead of blank space:
Thanks for your help,
RS
Rails's select accepts an argument for the empty value, so you can change your code to be the following (Since I don't think you want the Subject to be selectable):
<%= f.label :activity, 'Select Activity' %><br />
<%= f.select :activity, options_for_select([
['Math', 'Math'],
['Science', 'Science'],
['English', 'English']
]), include_blank: "Subject" %>
You can also improve your code by doing the following:
<%= f.label :activity, 'Select Activity' %><br />
<% activities = %w(Math Science English) %>
<%= f.select :activity, options_for_select(activities.zip(activities)), include_blank: "Subject") %>
option_for_select give you as option to select a value, you can select a value either from collection or can give your own.
In your case
<%= f.label :activity, 'Select Activity' %><br />
<%= f.select :activity, options_for_select([
['#', 'Subject'],
['Math', 'Math'],
['Science', 'Science'],
['English', 'English']
] , selected: "Math" ) %>
Hope this help you
Super Rails n00b here: Currently I have a form with the following code:
<%= f.collection_select :account_ids, #accounts, :id, :name, include_blank: true %>
and it currently works how I want it to but now I would like to have multiple drop down menus so I can select multiple accounts. I do not want the multiple select to be on the same dropdown.
If I do this:
<%= f.collection_select :account_ids, #accounts, :id, :name, include_blank: true %>
<%= f.collection_select :account_ids, #accounts, :id, :name, include_blank: true %>
<%= f.collection_select :account_ids, #accounts, :id, :name, include_blank: true %>
only the last selection appears in the params. How can I make it so the params would look like this:
"journal"=>{"account_ids"=>["1","2","3"]}
Can collection.select do this or should I be using something different? Any help would be greatly appreciated. Thanks!
You need to add one option :multiple :
<%= f.collection_select :account_ids, #accounts,
:id, :name, { include_blank: true },
{ multiple: true } %>
Note: :multiple- If set to true the selection will allow multiple choices.
I wrote a little snippet to test it. My code :
<%= form_for #track, url: fetch_path do |f| %>
<%= f.collection_select :label, #tracks, :id, :title, {include_blank: true}, {multiple: true} %>
<% end %>
Here is the page :
Or, if you really want to duplicate:
<% klass = f.object.class.model_name.param_key %>
<%= f.collection_select :account_ids, #accounts, :id, :name, { include_blank: true } , { name: "#{klass}[account_ids][]" } %>
Write the above line 3 times.
If your parameter name ends in "[]", then all inputs with that name will be collated into an array with that name.
So, your select tag (in html) will be like
<select name="account_ids[]"><option>...
and to make this using the collection_select helper, try
<%= f.collection_select :account_ids, #accounts, :id, :name, {include_blank: true}, {:name => 'account_ids[]'} %>
I have the following form to edit
<%= form_for #post do |f| %>
<%= f.text_field :title %> #This shows correctly
<%= f.collection_select :product, Product.all, :id, :name %>
<% end %>
product is the column which will save the id(primary key) of Product table. How to show the saved value in my select box.
Just use :selected option
<%= f.collection_select :product, Product.all, :id, :name, :selected => #post.product %>
But this works
<%= f.collection_select :product, Product.all, :id, :name, :selected => #post.product.id %>
I have a form that has multiple collect_select fields and I want to be able to store the selected values into an array or hash.
So for example:
<%= f.collection_select(:package_ids, Package.all, :id, :concatTitle, {:prompt => true}) %>
<%= f.collection_select(:package_ids, Package.all, :id, :concatTitle, {:prompt => true}) %>
<%= f.collection_select(:package_ids, Package.all, :id, :concatTitle, {:prompt => true}) %>
I want all the selected id's to be stored into the same field :package_ids. What would be the correct syntax to do this?