ruby on rails - how do i get the id of the selected item in dropdown -
<%= form_for(:offer,:url=>{:controller=>'offers',:action=>'combo'}) |f|%>
<%= f.select :catid_get, options_from_collection_for_select(@categories, "id", "name"), prompt: "select category" %>
i new in rails.i have dropdown
categories there.when select category dropdown
want category id in controller,so can use id it's child dropdown
.
select
each select option in html has 2 values -- value
, 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 value passed controller. means if able create select
tag in rails app correct value
/ label
setup, pass correct data require.
rails
here's how i'd handle it:
<%= form_for :offer, offers_combo_path |f|%> <%= f.collection_select :cat_id, @categories, :id, :name, prompt: "select category" %>
this pass following params categories_controller
:
#app/controllers/categories_controller.rb class categoriescontroller < applicationcontroller def combo params[:offer][:cat_id] end end
recommendation
i'd recommend use form_tag
helper this, rather form_for
. reason being form_for
activerecord objects, , although can use :symbols
in helper, need use less elaborate system
i'd replace form_for
following:
<%= form_tag offer_combo_path %> <%= collection_select :cat_id, @categories, :id, :name, prompt: "select category" %> <% end %>
Comments
Post a Comment