why a dropdown selection passes nil as parameter? - mysql

i have created a dropdown in my application using the following code:
<% form_for :categories, :url=> {:controller => 'products', :action => 'cat'} do |f| %>
<%= f.select(:category , Categories.all.map{ |u| [u.name, u.id] }, :prompt => "Select a category") %>
<input type="submit" value="go"/>
<%end%>
controller:
def cat
#products = Product.search_category params[:category]
end
it is returning 'nil' as the parameter when i select any category from dropdown. what could be the reason?

You should expect the category id to be in params[:categories][:category] because that's how the form was set up. If you want to use params[:category], pass a name option to select
<%= f.select :category, Categories.all.map{ |u| [u.name, u.id] }, { :prompt => "Select a category" }, { name: 'category' } %>
That aside, I find it better to use collection_select when you're dealing with an ActiveRecord table
<%= f.collection_select :category, Categories.all, :id, :name, { prompt: 'Select a category' }, { name: 'category' } %>

I think the problem lies with your symbol used with form_for. Generally, a object is passed to form for as it will generate the proper routes.
This question is discussed here
fields_for doesnt working when form_for use symbol
and
Ruby on Rails : symbol as argument in form_for

Related

How to create select tag in form_for Rails

I am trying to create a select tag in a form_for where I can select multiple categories from the options. I have looked at the Rails documentation and this SO, but neither of them seem to work. So far, I have this:
<select class="selectpicker" data-style="form-control" multiple title="Choose Department(s)" data-size="5">
<%= options_from_collection_for_select(Category.all, :id, :name)%>
</select>
And my form_for looks like this:
<%= form_for(#listing, :html => {class: "form-horizontal" , role: "form"}) do |f| %>
My listings can have many categories. How am I supposed to make this save to my form? Right now, the categories aren't saving when I submit my form.
It's not working because your select isn't scoped to your #listing object. Try:
<%= f.collection_select(:category_id, Category.all, :id, :name) %>
To address #ddubs's comment suggesting to replace the select tag with a Rails form helper as well as keeping your custom HTML data attributes:
<%= f.collection_select(:category_ids, Category.all, :id, :name, {}, class: "selectpicker", title: "Choose Department(s)", multiple: true, data: { style: "form-control", size: "5" }) %>
For more information on collection_select options, look at the Rails api.
Final answer ended up being <%= f.collection_select(:category_ids, Category.all, :id, :name,{:"data-style" => "form-control", :"data-size" => "5"}, {class: "selectpicker", title: "Choose Department(s)", multiple: true}) %> as mmichael pointed out.

What is the proper Ruby syntax for values in select option form?

I have a form that is submitting data to a model with nested attributes. I am using select to choose from a list of options.
The Articles model accepts_nested_attributes_for the Documents model, which has_attached_file called Attachment. The url_path of the attachment is what the select field should be selecting.
How to I change the value of the select field?
The HTML looks like this right now:
<select id="article_document" name="article[document]">
<option>...</option>
</select>
But I want this:
<select id="article_document_url_path" name="article[document][url_path]">
<option>...</option>
</select>
The Ruby looks like this:
<%= form_for #article, :html => { :multipart => true } do |f| %>
<%= f.select( :document,
options_for_select(#article.attachment_list.map{ |c| [c, {'data-img-src' => c }] }) ) %>
<% end %>
Rails 4.1.8, ruby 2.1.5p273
Try this:
<%= form_for #article, :html => { :multipart => true } do |f| %>
<%= f.fields_for :document do |doc|%>
<%= doc.select(
:url_path,
options_for_select(
#article.attachment_list.map{ |c| [c, {'data-img-src' => c }] }
)
) %>
<% end %>
<% end %>

Rails collection_select custom name attribute

I have the following collection select which acts as a filter in a Rails app.
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All'} %>
<% end %>
This always generates a name attribute of the select element like name="doctor[id]" which results in the browser to ?utf8=✓&doctor%5Bid%5D=1, which is not quite readable.
How can I change the name attribute to just name = "doctor" or basically just remove the brackets from it?
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
The collection_select method contains the parameters "options" and "html_options". "options" allow you to add specific information, like {:include_blank => 'All'}, but does not replace html attributes.
You have to add the name to the next hash, like this:
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All'}, {:name => 'doctor'} %>
<% end %>
Have you tried:
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All', :name => 'doctor'} %>
<% end %>

How populate select box in edit page using rails?

I have this in my form:
<%= f.select :status, options_for_select(Article::STATUS), :include_blank => true, :selected => #article.status, :class => 'select_field' %>
Model:
class Article < ActiveRecord::Base
attr_accessible :author, :content, :publication_date, :status, :title, :tag_tokens
attr_reader :tag_tokens
STATUS = ['Rascunho', 'Em Revisão', 'Publicado']
end
Why does not populate the field?
I believe you can simply pass an array in [#select][1]. You don't need to call options for collection. I also think you can ommit the :selected option if this is a model form, Rails will figure it out using the attribute value.
Try this:
<%= f.select :status, Article::STATUS, :include_blank => true, :class => 'select_field' %>

How does the HTML5 multiple file upload field map to a nested model in Rails 3?

I'm trying to use the HTML5 multiple attribute on a file field in a nested form.
The models are as follows:
class Album < ActiveRecord::Base
has_many :album_images
has_many :images, :through => :album_images
accepts_nested_attributes_for :images
end
class Image < ActiveRecord::Base
has_many :album_images
has_many :albums, :through => :album_images
mount_uploader :filename, ImageUploader
validates_presence_of :filename
end
The view:
<%= semantic_form_for #album, :url => upload_path do |f| %>
<%= f.inputs do %>
<%= f.input :name, :label => 'Album title' %>
<% end %>
<%= f.input :images, :as => :file, :input_html => {:multiple => true} %>
<%= f.buttons do %>
<%= f.commit_button 'Upload' %>
<% end %>
<% end %>
When I use for the file field:
<%= f.input :images, :as => :file, :input_html => {:multiple => true} %>
I get:
<input id="album_images" multiple="multiple" name="album[images][]" type="file">
Which doesn't doesn't seem right since I think I want to set the filename on the object directly, but I'm not sure about this. When I try to upload with this field, the incoming params look like:
"album"=>{"name"=>"2011-01-09", "images"=>["IMG_0052.JPG", "IMG_0053.JPG", "IMG_0054.JPG", "IMG_0055.JPG"]}
However, I get the following error:
ActiveRecord::AssociationTypeMismatch (Image(#2157004660) expected, got String(#2151988680)):
OK, that error is probably due to the fact that it just received a filename and not an image object. So instead, I use for the file field:
<%= f.input :images, :as => :file, :input_html => {:multiple => true, :name => 'album[images][][filename]'} %>
for which Formtastic generates:
<input id="album_images" multiple="multiple" name="album[images][][filename]" type="file">
The incoming params look like:
"album"=>{"name"=>"2011-01-09", "images"=>[{"filename"=>"IMG_0052.JPG"}, {"filename"=>"IMG_0053.JPG"}, {"filename"=>"IMG_0055.JPG"}]}
But then I get this error:
Image(#2153868680) expected, got ActiveSupport::HashWithIndifferentAccess(#2158892780)
So how does one go about setting up this multiple file input filed mapping in Rails?
Thanks.
You need to include :html => { :multipart => true } in your form_for (or in your case semantic_form_for) call so that your <form> tag is set to support file uploads.
Then revert back to your original syntax for f.input and you should be right then.