I want a collection select tag in rails to submit two attributes of the chosen item to the controller when the form is submitted. Basically, I have a list of counties and I want to submit both the county and the state as parameters. No problem having it submit one or the other, but not both. Am I thinking about this the wrong way? Here's what I have so far...
<%= form_tag(plans_path, method: 'get', action: 'screen2') do %>
<%= text_field_tag :ZIP, "ZIP Code", id: "zipBlur"%>
<%= collection_select(nil, :county, #counties.order('RES_RATIO DESC'), :COUNTY, :COUNTY_NAME, {:selected => "#{params[:county]}"}) %>
<%= submit_tag 'Screen', :name=> nil %>
<% end %>
Thanks for your help!
using :multiple => true
ex:
<%= collection_select(:ingredient, :supplier_ids,
Supplier.all(:order=>"name ASC"),
:id, :name, {:selected => #ingredient.supplier_ids, :include_blank => true}, {:multiple => true}) %>
Related
I am attempting to create a feature where users can add an existing record, recipe, to a collection of records, menu. I am using collection_select with a simple_form to allow users to select multiple records from a list however, the form is not responding to the input_html: { multiple: true } option, which should allow users to select multiple values. The form is as below, please let me know if any other code would be helpful for context.
Form:
<%= simple_form_for #menu, local: true do |f| %>
<%= f.label :title, :class => "form-component-header" %>
<%= f.text_field :title, :class => "form-field" %>
<%= f.label :recipe_ids %>
<%= f.collection_select :recipe_ids, f.object.user.recipes, :id, :title, input_html: { multiple: true } %>
<%= f.submit :class => "form_button" %>
<% end %>
.permit(....recipe_ids: [])
You need to update the permitted parameters in your controller. Now that you are sending multiple selections the parameter needs to be marked as expecting an array.
I have the following form:
<%= form_tag users_path, method: :get, id: 'uco' do %>
<%= select_tag "country", options_from_collection_for_select(ISO3166::Country.countries.sort_by(&:name), 'un_locode', 'name'), :include_blank => true %>
<%= submit_tag "Search" %>
<% end %>
When I submit the form I end up with:
www.example.com/users?country=US
I would then like the form to pre-select params[:country].
However I do not know how to attach params[:country] into the select_tag. I unsuccessfully tried:
<%= select_tag "country", options_from_collection_for_select(ISO3166::Country.countries.sort_by(&:name), 'un_locode', 'name', params[:country]), :include_blank => true %>
based off of this example from the apidock.
Nevermind, I solved it with this answer:
How to make the select_tag keep value of last search?
:selected => params[:country]
i'm creating a form where a user can choose a product and a quantity. I need to pass the id value from the object #event to the controller, but i dont know whats the right way to do it. As it is now, it the params[:event_id] field is always nil in the controller.
<%= form_tag logic_giveRandomGifts_path :method => 'post' %>
<div class="form-group">
<%= collection_select(:params, :product_id, Product.all, :id, :name, :prompt => true) %>
Quantidate:
<%= text_field_tag :quantity, params[:quantity], :size => 2 %>
<%= submit_tag "GO!",params[:event_id] => #event.id,:class => 'btn btn-default' %>
</div>
Add hidden filed in your form and set its value equal to #event.id
<%= form_tag logic_giveRandomGifts_path :method => 'post' %>
<div class="form-group">
<%= collection_select(:params, :product_id, Product.all, :id, :name, :prompt => true) %>
Quantidate:
<%= text_field_tag :quantity, params[:quantity], :size => 2 %>
<%= hidden_field_tag :event_id, value: #event.id%> #add this
<%= submit_tag "GO!",params[:event_id] => #event.id,:class => 'btn btn-default' %>
</div>
<% end %>
You can use now event id in your controller as params[:event_id]
<%= hidden_field_tag :event_id, #event.id %>
So this will be available in params[:event_id].
Simply pass an hidden field in form as followings
<%= hidden_field_tag :event_id, value: #event.id%>
It will available in controller as
params[:event_id]
I am using simple_form in rails and have a situation where I want the label to be the value from another field. In this case that field is not to be changed and so I don't want to be on the form.
To explain a bit better I have two lines that look like
<%= f.input :name, :label => false, :disabled => true, :input_html => { :class => 'input-small' } %>
<%= f.input :status, :collection => ["Not started", "Passed", "Failed"], :include_blank => false, :label => false %>
What I'd like to do is have the first element to be the label of the second element. Now I could do this by having them inline, but I'd like them to be lined up with the other elements so that the labels and inputs are lined up.
so doing something like
<%= f.input :status, :collection => ["Not started", "Passed", "Failed"], :include_blank => false, :label => f.name %>
or
<%= f.input :status, :collection => ["Not started", "Passed", "Failed"], :include_blank => false, :label => {f.input :name, :label => false, :disabled => true} %>
Any thoughts on how to get around this?
Michael
'f.object' gets the object associated to that form and then you can get to the fields:
<%= f.select(:status, [["Not started","Not started"], ["Passed", "Passed"], ["Failed", "Failed"]]), :label => f.object.name %>
In the end I did indeed go for the inline option, which isn't ideal but did the job. However I had to do the following.
The main form set as a norm form
The block below that set as form-horizontal
then the subform partial defined as a none simple_form
<div class="control-group form-inline">
<div class="controls">
<%= f.text_field :name, :disabled => 'true', :size => 10 %>
<%= f.select(:status, [["Not started","Not started"], ["Passed", "Passed"], ["Failed", "Failed"]]) %>
</div>
</div>
If it was set as a simpleform the inline would not work as required. Again not perfect, and certainly not elegant, but worked
I have this in the Main form
<%= simple_nested_form_for #customer_bill do |f| %>
<%= f.label :customer_id %>
<%= f.collection_select :customer_id, Customer.all,:id,:name, {:prompt => "Select Customer"}, :style => 'width:205px;' %><br />
<%= f.link_to_add "Add", :customer_bill_line_items, :locals => {:text_1 => :customer_id} %>
/* rest of code */
<%end%>
And i have this in my customer_bill_line_items Partial
<%= f.hidden_field :customer_id, :value => :text_1 %>
/*rest of code*/
But i am not Able to capture the selected customer id in the partial. The value of hidden field is coming as 0. Any guidance on how i can solve this matter will be great. Thanks in advance
<%= f.hidden_field :customer_id, :value => :text_1 %>
I am guessing that customer_id in an integer field, and you are setting the :value as text_1 which is a string.
if the value of text_1 is an integer then text_1.to_i