Add Custom HTML ID to Rails Select Box - html

Usually, adding an ID tag at the end of a rails form helper, does the trick. However, this does not seem to be working for select boxes. What am I doing wrong?
<%= form_for(#song) do |f| %>
<%= f.select :category, [['Pop', 1], ['Rap', 2]] , :id=>"choose-category"%>
<% end %>
^The ID is not getting set here properly, what am I doing wrong?
Thanks.

The select method takes four arguments, html_options is the fourth, therefore you have to pass an empty hash as the third parameter (options):
f.select(:category, [['Pop', 1], ['Rap', 2]], {}, :id => "choose-category")

Related

Rails: Dropdown menu to select results per page with will paginate

I currently have a page on my rail site that will show, by default, 25 results per page using will_paginate. I would, however, like to have a dropdown menu that allows the user to select the number of results shown from a predetermined list of values. I looked at the answer to a similar question here but the problem I have with the top solution is that I also have a search field in the same page and changing the number of results per page thus removes the search parameter if there is one.
I was wondering if there was any way for me to get around this problem? Thanks in advance!
The select box proved to be troublesome but i found a different way to do the same functionality.
So by using anchor tags and grabbing the search params when building the path it preserves the search criteria as well as any new per_page value.
so 5 and 10 page links would look like this
<%= Link_to "5", current_path(:per_page =>5,:search => params[:search])%>
<%= Link_to "10", current_path(:per_page =>10,:search => params[:search])%>
and the URL in the href will look like this: whatever.com/current?per_page=10&search=DevOps changing with whatever search term is provided.
Be sure to add the per_page value to the controller as well
per_page = params[:per_page]
#items = Item.all.paginate(page: params[:page], per_page: per_page)
Try this:
In your view -
<%= form_tag xyz_path, method: :get do %>
<%= text_field_tag :search, params[:search] %>
<%= select_tag :per_page, options_for_select([10, 20, 50, 100], params[:per_page]) %>
<%= button_tag :submit %>
<% end %>
In your controller -
unless params[:per_page].present?
params[:per_page] = 20 #default
end
if params[:search].present?
ModelName.where(query: params[:search]).paginate(:page => params[:page], :per_page => params[:per_page])
else
ModelName.all.paginate(:page => params[:page], :per_page => params[:per_page])
end

ERB checkboxes with different id but same value acting weirdly

For my ERB, if there are checkboxes with same values but for different forms, after a new render due to create error the checkboxes for different forms with same value gets checked. It doesn't seem to matter their different ids.
I have several checkboxes for model Option with two columns :size and :color. Currently, I'm using a bigger model to accept_nested_attributes for the Option model.
In my form, for each Option, I'm putting a checkbox with hidden value for color and checkbox for size such that:
<%= form_for #big do |big| %>
<%= f.fields_for :options do |option| %>
<%= f.hidden_field :color, value: "Red" %>
<%= f.check_box :size, {}, "Small", nil %>
<% end %>
<%= f.fields_for :options do |option| %>
<%= f.hidden_field :color, value: "Blue" %>
<%= f.check_box :size, {}, "Small", nil %>
<% end %>
.....
<% end %>
The options above have each unique ids according to html and everything but just gets checked equally, which I suspect is due to the same value.
Is there any way to prevent this behaviour?
I solved the problem.
This was more deeply rooted into my shabby controller practice mixed with overusing ERB fields_for.
So for my controller, I only have one #bigmodel.options.build, and in the controller I have four f.fields_for option. The problem was that the big model had one association to the options and therefore all options were being treated equally, regardless of the html output.
Since the html output was unique, the value submission was okay - not buggy - but when something failed server side, there were myriad of problems bound to come out to html.

Rails .map produces unwanted ["\n"]

I got the following lines in my view:
<p><%= #runnings_past.map do |f| %>
<%= f.title %>
<% end %>
</p>
which works just fine in the console --> output:
2.1.2 :076 > #runnings_past.map do |f|
2.1.2 :077 > f.title
2.1.2 :078?> end
=> ["Murtenlauf"]
2.1.2 :079 >
But when I use it in the view as seen above, I get an expression like this:
Murtenlauf ["\n"]
Where does the ["\n"] come from?
You are using the wrong iterator. You want to use each instead of map. Map creates a new array where each element is the result of each iteration in the block. While each simply calls the given block once for each element in the array.
In addition, as pointed out by #JTG, you want to remove the =. You only need = for showing output. You don't need it for logic.
<%= #runnings_past.map do |f| %>
<%= f.title %>
<% end %>
Should actually be
<% #runnings_past.each do |f| %>
<%= f.title %>
<% end %>
Notice the lack of = (in addition, I changed map to each because you're just iterating through the array)
<%= means evaluate the code and insert the result into the html structure. So what you were doing was evaluating the .map and putting the result into your html. So you would iterate through the #runnings_past code, place the f.first into the html (which is what you want) but then when you were done with that, you were placing the result of the mapping (which apparently was an array with a string return character) into the html afterwards.

Rails default value of select tag + active record

I'm trying to set the default value of a select tag to the value that is saved in the model in active record. At the same time, if no value is specified (e.g. on model-creation) I want a list to be displayed to select a certain field of the select tag.
This is how I'd like to do it:
<% valid_years = Array.new %>
<% for i in 1900..Time.now.year %>
<% valid_years << i %>
<% end %>
<%= f.label t :label_year_of_birth %>
<%= f.select :year_of_birth, options_for_select(valid_years), class: "" %>
However, the value that's saved in active record is not displayed in this select tag. Instead, the first value of the array is shown. Also, if I write options_for_select(valid_years, 1950), rails won't put the value to 1950.
However, if I use a simple text field tag, rails WILL put the saved value in it.
<%= f.label t :label_year_of_birth %>
<%= f.text_field :year_of_birth, class: "" %>
Replace
<%= f.select :year_of_birth, options_for_select(valid_years), class: "" %>
With
<%= f.select :year_of_birth, options_for_select(valid_years, form_for_object_name.year_of_birth), class: "" %>
where
form_for_object_name is the object that you are passing to your form since you didn't share the <%= form_for(?) do |f| %>.
options_for_select takes the second argument i.e. form_for_object_name.year_of_birth as the selected value.
If form_for_object_name.year_of_birth is nil then select will default to the first value of your options i.e. 1900 in your case.

Display data in form from 2 tables in Ruby on Rails 3

I used nifty scaffolding for create controller 'A'. For this controller I have in DB table(again called 'A').
I have view, in which is a form for entering information. These data will be stored in database.
But was request have in this form input that isn't in table 'A' (column), but in table 'B'.
<%= form_for #a do |u| %>
<%= u.error_messages %>
<p>
<%= u.label :name %><br />
<%= u.text_field :name %>
</p>
<p>
<%= u.label :age %><br />
<%= u.text_field :age %>
</p>
<p>
<%= u.label :email %><br />
<%= u.text_field :email %>
</p>
...
Input called 'name' will be stored in table 'A' (into column 'name'), input called 'age' will be stored in table 'B' (into column 'age').
I want display this form in browser, I am getting error:
undefined method `age' for...
Can you help me, please, what is needed for this situation for right displaying the form?
Use the fields_for helper Rails provides.
In your code you have done some mistake because as your code all the fields should be at the same table. But actually you have fields with different tables. So that's why it shows that error message.
You can use fields_for to save age in "B", but this will slowdown your loading process only ,you not need to create such extra table.