How to DRY up this conditional collection_select in form partial? - ruby-on-rails

I have a form partial that is being called in a content_for :sidebar. This collection_select should have "selected" set if the page calling the partial is a specific package page. Otherwise, it should have a "prompt" to select. How would I DRY this up? I tried an inline ternary on a single collection_select to no avail.
<%- if #package.blank? -%>
<%= f.collection_select :package_name, Package.all, :name, :name, :prompt => "Please Select" %>
<%- else -%>
<%= f.collection_select :package_name, Package.all, :name, :name, :selected => #package.name %>
<%- end -%>
Thanks

How about:
<%= f.collection_select :package_name, Package.all, :name, :name,
#package.blank? ? { :prompt => "Please Select" } : { :selected => #package.name } %>

Related

Submitting multiple attributes to controller from collection select in form

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}) %>

Rails: Multiple dropdown menus collection_select

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[]'} %>

Add two values to a form select box

I want to include BOTH manager first name and last name in a select box below. How can I accomplish this?
Form:
<%= simple_form_for #office do |f| %>
<%= f.input :street_address %>
<%= f.input :city %>
<%= f.input :postal_code %>
<%= f.input :description, as: :text %>
<%=f.input :manager_id, collection: Manager.all, :id, :last_name, include_blank: true %>
<%= f.submit 'Add Office' %>
You can add something like
label_method: lambda { |manager| "#{manager.first_name} #{manager.last_name}" }
to your f.input
or you can create a new method "name" in your model, and use this one instead
def name
"#{first_name} #{last_name}"
end
then
label_method: :name

populate combo box for edit form in rails 4

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 %>

Not able to Pass customer_id Value in the form to a partial as hidden field value in ruby on rails

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

Resources