How to submit multiple forms with one button - ruby-on-rails

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?

Related

Rails 5 ActiveRecord Update Serialized Array in Form

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] }) %>

Dynamic checkboxes to send array to controller only sending first choice

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

form not creating new entries

I am trying to create a form to add a transporter:
I have app/views/transporters/new.html.erb that calls a template,app/views/_form.html.erb that looks like:
<%= form_for(#transporter) do |f| %>
<% if #transporter.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#transporter.errors.count, "error") %> prohibited this transporter from being saved:</h2>
<ul>
<% #transporter.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :phone %><br>
<%= f.text_field :phone %>
</div>
<div class="field">
<%= f.label :id_number %><br>
<%= f.text_field :id_number %>
</div>
<div class="actions">
<%= f.submit 'Add transporter', class: 'btn btn-success' %>
</div>
<% end %>
<%= link_to 'Back to all transporters', transporters_path, class: "btn" %>
</div>
</div>
This is the create action in my transporters controller:
def create
#transporter = Transporter.new(transporter_params)
end
private
def transporter_params
params.require(:transporter).permit(:name, :phone, :id_number)
end
When I click Add transporter the form doesn't go anywhere. What do I have wrong?
Update:
this is my routes file:
Cowsnhills::Application.routes.draw do
resources :transporters
resources :deliveries
root 'welcome#index'
end
When I click submit the form reloads but the entries will not show on my transporters index, here is that code:
transporters controller method:
def index
#transporters = Transporter.all
end
and transporters index:
<h1>Listing transporters</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Id</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #transporters.each do |transporter| %>
<tr>
<td><%= transporter.name %></td>
<td><%= transporter.phone %></td>
<td><%= transporter.id %></td>
<td><%= f.link_to_add "Add a delivery", :deliveries %></td>
<td><%= link_to 'Show transporter details', transporter %></td>
<td><%= link_to 'Edit transporter details', edit_transporter_path(transporter) %></td>
<td><%= link_to 'Delete transporter', transporter, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Transporter', new_transporter_path, class: "btn" %>
Also there's a transporter has_many deliveries and a velivery belongs_to transporter association going on
You just create a new object with your params from the form in your create action, but you don't save it.
add #transporter.save to your create action.
use
#transporter = Transporter.new(params[:transporter])
or
#transporter = Transporter.new(params["transporter"])
check my last comment to save it.

Image tag shows all columns of model

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 %>

Render partial scaffold in the index view - Rails

I want exibe the action 'Show' of scaffold in my index using ajax but when i click in show dont appear nothing my files is:
finances_controller.rb
def show
#finance = Finance.find(params[:id])
end
_show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Date update:</b>
<%= #finance.updated_at %>
</p>
<p>
<b>Money:</b>
<%= #finance.money %>
</p>
<p>
<b>Entrance:</b>
<%= #finance.entrance %>
</p>
<p>
<b>Description:</b>
<%= #finance.description %>
</p>
<p>
<b>Situation:</b>
<%= #finance.situation %>
</p>
<p>
<b>Local:</b>
<%= #finance.local %>
</p>
<%= link_to 'Edit', edit_finance_path(#finance) %> |
<%= link_to 'Back', finances_path %>
my
index.html.erb modified the button show:
Bem vindo <%= session[:user_name]%>
<div id='list' align='left'>
<h1>Finanças - Hoje é dia <%= Date.today %></h1>
<%= link_to 'New Finance', new_finance_path %>
<table cellpadding="5" >
<tr>
<th>Data de Inserção</th>
<th>Caixa</th>
<th>Entrada</th>
<th>Descrição</th>
<th>Situação</th>
<th>Local</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #finances.each do |finance| %>
<tr>
<td><%= finance.created_at %></td>
<td><%= finance.money %></td>
<td><%= finance.entrance %></td>
<td><%= finance.description %></td>
<td><%= finance.situation %></td>
<td><%= finance.local %></td>
<td><%= link_to 'Show', finance,:action => 'show',:id => #finance , :remote =>:true %></td>
<td><%= link_to 'Edit', edit_finance_path(finance) %></td>
<td><%= link_to 'Destroy', finance, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
</div>
<div id='tools' align='right'>
<%if #finance %>
<%= render('show') %>
<% end %>
</div>
<%= link_to 'Logout', new_session_url,method: :delete %>
when i click in the show appear the url 'localhost:3000/finances/1' but dont happening nothing when i click this i want click and show my partial 'show' in the same page, i render the partial 'show' if especified the #finance ,
<%if #finance %>
<%= render('show') %>
<% end %>
try to use = render 'finances/show'

Resources