I have a table of data created like so:
<% #products.each do |product| %>
<tr>
<td><%= form_tag push_to_products_path(product), :id =>
'submit_products' do %>
<%= check_box_tag('send[]', product.sellersku) %>
<% end %>
</td>
<td><%= product.title %></td>
<td><%= product.asin %></td>
<% end %>
</tr>
I created the nested form_tag because I have a button outside of the form that uses the checkbox data to send to my Product controller. This is the button code:
<div class="row pad_bottom col-md-4 col-md-offset-1 text-center">
<button type="submit" form="submit_products" class="btn btn-primary">
<span class="glyphicon glyphicon-upload" aria-hidden="true"></span> Push
Selected Products
</button>
</div>
The checkboxes are correctly created in the view, but when I test only the first box has the sellersku value.. all the other checkboxes don't pass anything. They should also be passing the sellersku.
Pretty sure its my loop, just not sure what I did wrong??
Declare your form outside of the loop. Right now, you're looping through your #products and creating a form for each one.
<%= form_tag push_to_products_path, :id => 'submit_products' do %>
<% #products.each do |product| %>
<tr>
<td><%= check_box_tag('send[]', product.sellersku) %></td>
<td><%= product.title %></td>
<td><%= product.asin %></td>
</tr>
<% end %>
<%= submit_tag("Push Selected Products") %>
<% end %>
<%= form_tag products_push_to_path, :id => 'submit_products' do %>
<% #products.each do |product| %>
<tr>
<td><%= check_box_tag('send[]', product.sellersku) %>
</td>
<td><%= product.title %></td>
<td><%= product.asin %></td>
</tr>
<% end %>
<%= submit_tag "Push Selected Products" %> #replace this with glyphicon part
<% end %>
routes.rb #assuming your action name is push_to with resources products
resources :products do
collection do
post 'push_to'
end
end
Related
I have a field that is a serialized array. It's loaded into my model and accessed in a form:
class Site < ApplicationRecord
serialize :steps, Array
end
<table class="listing" summary="Site list">
<tr class="header">
<th>Name</th>
<th>Step 1</th>
<th>Step 2</th>
<th>Step 3</th>
<th>Actions</th>
</tr>
<% #sites.each do |site| %>
<tr>
<td><%= site.name %></td>
<% site.steps.each do |step| %>
<td><%= step %></td>
<% end %>
<td class="actions">
<%= link_to("Show", site_path(site), :class => 'action show') %>
<%= link_to("Edit", edit_site_path(site), :class => 'action edit') %>
<%= link_to("Delete", delete_site_path(site), :class => 'action delete') %>
</td>
</tr>
<% end %>
</table>
I'm trying to update my edit form so that I can edit each "step" in the array.
<%= form_for(#site) do |f| %>
<table summary="Site form fields">
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<% a=1 %>
<% #site.steps.each do |step| %>
<tr>
<th>Step <%= a %>
<td><%= text_field :site, :steps, :value => step %></td>
<% a += 1 %>
</tr>
<% end %>
</table>
<div class="form-buttons">
<%= f.submit("Update Site") %>
</div>
<% end %>
The edit form displays the steps field correctly as each individual string in the array. However, when I attempt to submit the form I get the following error:
Attribute was supposed to be a Array, but was a String.
So steps is being submitted as the last entry in the array. How can I display the form correctly and also present the updated array back to the controller for processing?
Your text_field would need to multiple set to true. I believe in your case, something like this should work.
<%= f.text_field(:steps, { multiple: true, value: #site.steps[step] }) %>
I'm trying to make a view where you can edit info about all the Users at once. Here's how I made the view right now:
<% #users.each do |u| %>
<%= render 'set_days_row', user: u %>
<% end %>
And here's 'set_days_row':
<div class="row">
<div class="col-md-3">
<span><%= user.name %></span>
</div>
<div class="col-md-9">
<%= form_for user do |f| %>
<table class="days-table">
<tr>
<td>M</td>
<td>Tu</td>
<td>W</td>
<td>Th</td>
<td>F</td>
</tr>
<tr>
<td><%= f.check_box :monday %></td>
<td><%= f.check_box :tuesday %></td>
<td><%= f.check_box :wednesday %></td>
<td><%= f.check_box :thursday %></td>
<td><%= f.check_box :friday %></td>
</tr>
</table>
<% end %>
</div>
</div>
The issue is that since there's multiple forms, I can't just make an f.submit button because it wouldn't submit all the form objects. How can I do this?
Yesterday i made some experience with carrierwav, all works fine but at the image tag where normally only the image should displayed rails also shows the hole model, with created_at etc. Here you cann see it!
So now my view:
<% #patient.treatments.each do |treatment| %>
<tr>
<td><%= treatment.category.try(:typ) %></td>
<td><%= treatment.content %></td>
<td><%= treatment.day %></td>
<td><div class="arrow"></div></td>
</tr>
<tr>
<td colspan="5">
<%= link_to 'Löschen', [treatment.patient, treatment],
:confirm => 'Sind sie sicher?',
:method => :delete %>
<%= treatment.paintings.each do |paint| %>
<%= image_tag paint.name %>
<% end %>
</td>
</tr>
<% end %>
The problem has to be in <%= image_tag paint.name %>
Remove the = from <%= treatment.paintings.each do |paint| %>, that is making you also print the treatment.paintings array.
<% treatment.paintings.each do |paint| %>
<%= image_tag paint.name %>
<% end %>
I'm trying to edit a collection of users. I want to render table with a list of names and checkboxes. I'm close, but I'm missing something.
My form looks like so:
<%= form_for 'users[]', :url => approve_users_path, :html => {:class => 'form-horizontal'} do |f| %>
<tbody>
<% for user in #users %>
<%= fields_for user do |u| %>
<tr>
<td><%= user.name %></td>
<td><%= u.check_box :vouched %></td>
</tr>
<% end %>
<% end %>
</tbody>
<% end %>
which generates
<tr>
<td>steve cabillero</td>
<td><input name="user[vouched]" type="hidden" value="0" /><input id="user_vouched" name="user[vouched]" type="checkbox" value="1" /></td>
however, I need the input name in the form of users[id][vouched] and vouched is a virtual attribute.
when I try to use f.check_box I get a method not found error.
you should use form_tag if you are not using a specific resource in the form. I also suggest using #each instead of a for loop and check_box_tag for the checkbox
<%= form_tag approve_users_path, :class => 'form-horizontal' do %>
<tbody>
<% #users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= check_box_tag "users[#{user.id}][vouched]" %></td>
</tr>
<% end %>
</tbody>
<% end %>
I'm using ruby on rails.
wondering if this is achievable.
Original Code
<%= form_for(:page, :url=>{:action => 'create'}) do |f| %>
<table summary="Subject Form Fields" %>
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th>Position</th>
<td><%= f.text_field(:position) %></td>
</tr>
<%end%>
desired code something along the lines of creating forms
by iterating through the object attributes.
<% for attribute in #subject.attributes.keys %>
<tr>
<td><%= attribute.humanize %></td>
<td><%= #subject.attributes[attribute].to_s %></td>
</tr>
<% end %>
so I am not sure if this is possible.
I believe what you are looking for is a .each loop:
<% #subject.attributes.each do |attribute| %>
<%= attribute.humanize %>
<% end %>
That will loop through each attribute of the #subject. If you also want to loop through the keys of each attribute, you need to add another nested loop:
<% #subject.attributes.each do |attribute| %>
<% attribute.keys.each do |key| %>
<%= attribute.humanize %> or <%= key %>
<% end %>
<% end %>
Hope that helps.