How do I change all count value at same time, currently i have to change each of them individually.
<% #book.each do |book| %>
<%= form_for(book) do |f| %>
<%= f.number_field :count %>
<% end %>
<% end %>
lets say my current value 2,5,1 and when i change :count field to 3, result should be like 3,3,3 to all. Thanks
You need to be more specific.
if you need to update a column for all rows use this
Book.update_all(:count, 3)
or on book controller
Book.update_all(:count, params[:book][:count])
Related
I'm a beginner on my first project. If I have 10 similar fields, is there a way to create them dry so they would still work with validations? I would also like to put the loop number in the attribute name, because the attributes are named field_name_1 through to field_name_10, for example.
<% 10.times do |asdf| %>
<%= f.input :field_name_(asdf + 1), etc %>
<% end %>
This may work !!!
<% (1..10).each do |n| %>
<%= f.input ("field_name_"+n.to_s).to_sym, etc %>
<% end %>
Here, I have 10 columns i.e., answer1, answer2, answer3, ..., answer10 in the table MgAnswer.
I have to check whether each column value is present or not. Only if it present,then I have to display it in the page.
Im giving column names dynamically within for loop
<% (1..10).each do |i| %>
<% if MgAnswer."answer#{i}".present? %>
<%= MgAnswer."answer#{i}" %>
<% end %>
<% end %>
Im ending up with Syntax error.
You can indeed dynamically invoke methods in ruby, but this is not the syntax. Instead do
<% (1..10).each do |i| %>
<% if MgAnswer.public_send("answer#{i}").present? %>
<%= MgAnswer.public_send("answer#{i}") %>
<% end %>
<% end %>
It should seem like the following:
<% (1..10).each do |i| %>
<%= MgAnswer.send("answer#{i}") %>
<% end %>
Since ruby can't evaluate line as MgAnswer."method". Also you can just skip if condition, because it will be evaluated to empty string "".
I am trying to create a checklist in rails using form_for. This checklist is taken from a table which I gained in the create action of my sign_ins controller:
#content = OrientationContent.where(site_id: session[:site_id])
In my view I want to use the form_for helper to iterate through the list in #content:
<%= form_for(:sign_ups ) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>
However this is not working and it produces two square brackets on the page: [].
How do I go through the list and print the name while creating a check box on the left of it? The check box does not have any meaning or data, I just need it present for reference.
Solved:
In the controller, need to pluck an individual field:
#content = OrientationContent.where(site_id: 1).pluck(:content)
In the view, structure as so:
<%= form_for(:sign_ups) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>
I have an action in the controller:
def user_detail
#user_detail = UserDetail.find_by_id(11)
end
And in the view:
<%= #user_detail -%> // displays me like #
I am trying to retrieve the contents of #user_detail: actually the hash contains {:empid=>"11111", :prjtname=>"aaaaa", :prjtrole=>"Developer"}
How do I display the user detail's empid and other values?
Since I know what question you asked earlier, I think this is the syntax you actually want to use:
<%= #user_detail.additional_info[:empid] %>
Unless of course you renamed the name of the hash :)
Another approach, if you want all the content from the hash but the keys varies from each record, you could loop through them like this:
<% #user_detail.additional_info.each_pair do |key, value| %>
<p>Key: <%= key %> Value: <%= value %></p>
<% end %>
To get simple debug output like the example you posted, this will handle it:
<%= #user_detail.inspect %>
try this <%= #user_detail.emplid %> <%= #user_detail.prjtname %> <%= #user_detail.prjtr %>
More of an extraction from #dln's answer
try using
<%= #user_detail[:emplid] %>
<%= #user_detail[:prjtname] %>
<%= #user_detail[:prjtr] %>
Hope this solves your prob
I have a subject model with two fields - "name" and "level". I want to be able to visit "/subjects#new" and add 10 subjects objects at once. How can I do this in formtastic.
In regular forms I would do this:
<% #subjects.each do |s| %>
<% fields_for "subject[#{s.id}]", s do |f|%>
<%= f.name%>
<%= f.level %>
<% end %>
<% end %>
Change fields_for to semantic_fields_for for and it should work.