How to dispaly selected value in edit page.
in my form i need to display selected value. it is not selecting the value which i selected and submitted. else it shows "please select Study material".
This is my StudyMaterial model
class StudyMaterial < ActiveRecord::Base
TYPES = ['Question Paper', 'Book', 'Audio', 'Video']
enum study_material_type: TYPES
end
This is my 'form.html.erb'
<select class=" required form-control" name="study_material[study_material_type]" id="study_material_study_material_type" data-validation="required" data-validation-error-msg="Select study material">
<option value="">Please select study material</option>
<option value="Question Paper">Question Paper</option>
<option value="Book">Book</option>
<option value="Audio">Audio</option>
<option value="Video">Video</option>
</select>
How to dispaly selected value in edit page.
I am getting this error when i click edit studymaterial page
Please help me to solve this error
<%= form_for #study_material do |f| %>
<%= f.select :study_material_type, StudyMaterial::TYPES.map{|v| [v,v]}, selected: f.object.try(:study_material_type) , required: true, include_blank: "Select" %>
<% end %>
I think, you look something like that:-
<%= form_for #study_material do |f| %>
<%= f.select :study_material_type, StudyMaterial::TYPES, include_blank: "Please select study material", required: true %>
<% end %>
It will display selected value.
Related
I have a form without a model backing it built using form_with in Rails 6:
<%= f.text_field :one %>
<%= f.select :two, [['Option 1',1],['Option 2',2]] %>
<%= f.submit 'Submit' %>
The only documentation I can find to set which of the select options are selected by default says that it will pre-select whatever is in the model. Since I don't have a backing model, how can I choose which option is selected? I've poked around the options a little and found nothing, but I do not necessarily know where to look.
You must have missed it, there's an optional selected keyword argument.
Lastly, we can specify a default choice for the select box with the
:selected argument:
<%= form.select :city, [["Berlin", "BE"], ["Chicago", "CHI"], ["Madrid", "MD"]], selected: "CHI" %>
Output:
<select name="city" id="city">
<option value="BE">Berlin</option>
<option value="CHI" selected="selected">Chicago</option>
<option value="MD">Madrid</option>
</select>
https://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease
How do I assign the selected option value:
<select class="select2-simple-dropdown">
<% Season.all.each do |season| %>
<option id="chosen-season" value="<%= season.id %>"><%= season.name %></option>
<% end %>
</select>
To a form's field, let's say: Voyage.given_season ?
Use the rails select field instead and do it like this
<%= f.select :season_id, Season.all.pluck(:name, :id), {},
{ class: 'select2-simple-dropdown'} %>
Hope this helps.
If you want your select input to accept multiple options, you can pass multiple: true
<%= f.select(:season_id, Season.all.collect {|m| [ m.name, m.id] }, class: "form-control select2-simple-dropdown", id: "list-markets", multiple: true) %>
https://aalvarez.me/posts/select2-with-simple-form-in-rails/
I'm not sure why, but my form is not showing the options selected on submit, even though the params hash shows that the information is being returned to the page.
Collection select code:
<%= f.collection_select :post_topic_ids, PostTopic.all, :id, :name, {}, { multiple: true, class: 'form-control' } %>
Which renders:
<select multiple="multiple" class="form-control" name="post[post_topic_ids][]" id="post_post_topic_ids">
<option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option>
</select>
Params returned after form validation error
params = {"post"=>{"post_topic_ids"=>["", "1"]}}
Update
I have also tried:
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }), multiple: true %>
and:
<%= select_tag 'post_topic_ids', options_from_collection_for_select(PostTopic.all, "id", "name"), multiple: true %>
Which renders:
<select name="post_topic_ids[]" id="post_topic_ids" multiple="multiple"><option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option></select>
you need to specify which element is selected a third parameter
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }, --->selected_element<---), multiple: true %>
look at http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select for some examples.
I am a newbie to Ruby on Rails and am stumped. As part of my exercise I am creating an app that let's people put in their goal, a description, a deadline and assign it to a person.
I've got two models, a user model and a goal model. In my ERB form to create a new goal, I've created a dropdown menu of all the created users but now I have no idea how to convert that into the foreign key that gets saved.
<%= form_for(#goal) do |f| %>
<% if #goal.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#goal.errors.count, "error") %> prohibited this goal from being saved:</h2>
......
<div class="field">
<%= f.label :user_id, "Whose goal is it?" %><br>
<%= collection_select(:goals, :user_id, User.all, :id, :name, prompt: true ) %>
</div>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The above seems to create the dropdown menu I need. I just have no idea how to get from the :name generated in the dropdown to the :user_id associated with it to submit.
Will greatly appreciate your help.
Thanks!
Frist you should use singular as object in collection_select:
<%= collection_select(:goal, :user_id, User.all, :id, :name, prompt: true ) %>
Then your collection_select creates a select tag with name goal[user_id]. It will look like
<select name="goal[user_id]">
<option value="">Please select</option>
<option value="1" selected="selected">D. Heinemeier Hansson</option>
<option value="2">D. Thomas</option>
<option value="3">M. Clark</option>
</select>
The options have user IDs as value an the names as content. So the parameter
params[:goal][:user_id]
contains the ID of the selected user. This is exactly how Rails expects the foreign key.
See the documentation of collection_select
Adition: there are great gems to simplify form generation, i.e. simple_form which reduces your form to
<%= simple_form_for(#goal) do |f| %>
...
<%= f.association :user %>
<% end %>
Your question is already answered but I'd like to share an awesome tutorial I found:
Advanced forms
Just add to your 'new' controller
#user_options = User.all.map{|u| [ u.name, u.id ] }
Then in your form:
<%= f.select(:user_id, #user_options) %>
<%= form_for(:offer,:url=>{:controller=>'offers',:action=>'combo'}) do |f|%>
<%= f.select :catId_get, options_from_collection_for_select(#categories, "id", "name"), prompt: "Select Category" %>
I am new in rails.I have a dropdown where all categories are there.When i select a category from this dropdown i want to get its category id in my controller,so that i can use that id for it's child dropdown.
Select
Each select option in HTML has two values -- the value and the label:
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
It's only the value which is passed to your controller. This means if you are able to create the select tag in your Rails app with the correct value / label setup, it will pass the correct data you require.
Rails
Here's how I'd handle it:
<%= form_for :offer, offers_combo_path do |f|%>
<%= f.collection_select :cat_id, #categories, :id, :name, prompt: "Select Category" %>
This will pass the following params to your categories_controller:
#app/controllers/categories_controller.rb
class CategoriesController < ApplicationController
def combo
params[:offer][:cat_id]
end
end
Recommendation
I'd actually recommend you use the form_tag helper for this, rather than form_for. Reason being that form_for is mainly for ActiveRecord objects, and although you can use :symbols in the helper, you will really need to use a much less elaborate system
I'd just replace your form_for with the following:
<%= form_tag offer_combo_path do %>
<%= collection_select :cat_id, #categories, :id, :name, prompt: "Select Category" %>
<% end %>
Your id should be accessible by
params[:offer][:catId_get]
in your controller.