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 %>
Related
I want to display a checkbox for all elements in a model called MyModel. Here is what I wrote:
<%= MyModel.all.each do |c| %>
<%= check_box_tag(:id) %>
<%= label_tag(:name, c[:name]) %><br>
<% end %>
It does show the checkboxes as expected but at the end, I also get a list of the model content as shown in this screenshot.
Actually, it seems to be related to <%= MyModel.all.each do |c| %> because just printing out simple text still prints the whole model table content at the end:
<%= MyModel.all.each do |c| %>
toto<br>
<% end %>
shows this screenshot
Any idea how to get rid of this list at the end?
Thanks!
<%- MyModel.all.each do |c| %>
<%= check_box_tag(:id) %>
<%= label_tag(:name, c[:name]) %><br>
<% end %>
- - evaluates code.
= - evaluates code and outputs.
When iterating through a collection of records, how can i add a radio_button to each image. here is what i have so far
<% #default_images.each do |d| %>
<%= image_tag(d.image_url(:campaign_form), 'data-image-id' => d.id)) %>
<% end %>
Im not sure on the syntax to generate a radio button with an associated image here.
I have previously done it within a form_for
f.collection_radio_buttons(:default_image_id, DefaultImage.all, :id, :image) do |b|
b.label { b.radio_button + image_tag(b.object.image) }
end
but ive already got the collection stored within #default_images in this scenario
any help appreciated
Thanks
Ended up doing this which seems to work
<% #default_images.each do |d| %>
<%= radio_button_tag :default_images, d.id %>
<%= image_tag(d.image_url(:campaign_form)) %>
<% end %>
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])
I want to update a model - only lines where the checkox is clicked and insert a remark
View:
<%= form_tag update_fb_instruction_users_path, :method => :put do %>
<% #user_wishes.each do |u| %>
<%= u.user.name %>
<%= fields_for "instruction[]", u do |f| %>
<%= f.text_field :remark_tl %>
<% end %>
<%= check_box_tag "instruction_user_ids[]", u.id %>
<% end %>
Controller:
def update_fb
params[:instruction_user_ids].each do
#check = InstructionUser.update(params[:instruction].keys, params[:instruction].values).reject { |p| p.errors.empty? }
end
The issue there is that they all have the same name. So whatever value the last one is, that's what it will be in the request params.
It's a bit old, but you might want to check out the railscast here: http://railscasts.com/episodes/73-complex-forms-part-1. The basic idea is to use fields_for on top of each user object. I haven't done it myself before, otherwise i'd write a full solution :).
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.