form not creating new entries - ruby-on-rails

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.

Related

Form_tag routing error after unsuccessful submit

I have an index page with a partial form to submit new records to Package model. I keep this form in the index page, so the user doesn't need to leave the page when repeating this action, several times.
In the same page I have a form_tag fir multiple updates for the same controller, namely packages_controller.
Everything works fine, except the following: when hit the update button, going to the form BUT instead of submitting I go back (with the browser) and try to select other records to be updated then I have a routing error:
Routing Error
No route matches [PUT] "/projects/47/orderlines/18/packages"
My index page looks like this:
<% if current_user %>
<%= render "packages/form" %>
<% end %>
<% if #packages.count >= 1 %>
<table class="table table-striped">
<thead>
<tr>
<th> <input type="checkbox" id="selectAll" value="selectAll"></th>
<th>Packed </th>
<th>#No.</th>
<th>Type</th>
<th>Gross weight</th>
<th>Length</th>
<th>Width</th>
<th>Height</th>
<th></th>
<th>Container</th>
</tr>
</thead>
<%= form_tag edit_multiple_project_orderline_packages_path, method: :get do %>
<tbody>
<% for package in #packages %>
<% if package.packed== true %>
<% #label_type="success" %>
<% else %>
<% #label_type="default" %>
<% end %>
<tr>
<td><%= check_box_tag "package_ids[]", package.id %></td>
<td><span class="label label-<%= #label_type %>"><% if package.packed==true %>Packed<% else %>Unpacked<% end %></span></td>
<td><%= package.package_no %></td>
<td><%= package.package_type %></td>
<td><%= package.gross_weight %></td>
<td><%= package.length %></td>
<td><%= package.width %></td>
<td><%= package.height %></td>
<% if #orderline.packages.count >= 1 %>
<td><%= link_to 'Delete', [package.orderline.project, package.orderline, package],
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
<td><%= #containers.find(package.container_id).container_id if package.packed %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Add to container", class: "btn btn-primary" %>
<% end %>
<br />
<%= will_paginate %>
<br>
And the multiple_edit form
<div class="col-sm-4">
<%= form_tag update_multiple_project_orderline_packages_path, method: :put do %>
<ul>
<% #packages.each do |package| %>
<li>
<%= hidden_field_tag "package_ids[]", package.id %>
<%= package.package_no %>
<%= package.container_id %>
<% package.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</li>
<% end %>
</ul>
<%= fields_for :package do |f| %>
<div class="field">
<%= f.label :package_no %><br />
<%= f.text_field :package_no, :class => "form-control" %>
</div>
<br />
<div class="field">
<%= f.label :container_id %><br />
<%= select_tag 'package[container_id]', options_from_collection_for_select(#project.containers, 'id', 'container_id', default_blank: true), prompt: "- Select container -", :class => "form-control" %>
</div>
<br />
<div class="field">
<%= f.label :packed %><br />
<%= f.select :packed, [["Packed", true], ["Unpacked", false]],{ prompt: "- Packing -"},{ :class => "form-control" } %>
</div>
<% end %>
<div class="actions">
<br />
<%= submit_tag "Update", :class => "btn btn-primary" %>
</div>
<% end %>
</div>
And the packages controller edit_multiple actions:
def edit_multiple
#project = Project.find(params[:project_id])
#packages = Package.find(params[:package_ids])
end
def update_multiple
#packages = Package.find(params[:package_ids])
#packages.reject! do |package|
package.update_attributes(package_params.reject { |k,v| v.blank? })
end
if #packages.empty?
redirect_to project_orderline_packages_url
else
#package = Package.new(package_params)
render "edit_multiple"
end
end
packages_controller create action:
def create
project = Project.find(params[:project_id])
orderline = project.orderlines.find(params[:orderline_id])
#package = orderline.packages.new(package_params)
#package.save
if #package.save
flash[:success] = "Package(s) was successfully added."
redirect_to :back
else
render 'new'
end
And my routes:
resources :projects do
resources :containers
resources :orderlines do
resources :packages do
collection do
put :packed
get :edit_multiple
put :update_multiple
end
end
end
end
I just added my routes here:
edit_multiple_project_orderline_packages_path GET /projects/:project_id/orderlines/:orderline_id/packages/edit_multiple(.:format)
packages#edit_multiple
update_multiple_project_orderline_packages_path PUT /projects/:project_id/orderlines/:orderline_id/packages/update_multiple(.:format)
packages#update_multiple
project_orderline_packages_path GET /projects/:project_id/orderlines/:orderline_id/packages(.:format)
packages#index
POST /projects/:project_id/orderlines/:orderline_id/packages(.:format)
packages#create
new_project_orderline_package_path GET /projects/:project_id/orderlines/:orderline_id/packages/new(.:format)
packages#new
edit_project_orderline_package_path GET /projects/:project_id/orderlines/:orderline_id/packages/:id/edit(.:format)
packages#edit
project_orderline_package_path GET /projects/:project_id/orderlines/:orderline_id/packages/:id(.:format)
packages#show
PATCH /projects/:project_id/orderlines/:orderline_id/packages/:id(.:format)
packages#update
PUT /projects/:project_id/orderlines/:orderline_id/packages/:id(.:format)
packages#update
DELETE /projects/:project_id/orderlines/:orderline_id/packages/:id(.:format)
your form_tag code is update_multiple_project_orderline_packages_path
I think it should be update_multiple_project_orderline_package_path(project_id, orderline_id, package_id)
I am not 100% sure with my statement above, because you gave scrambled Rails Routes, hard to read
and your form action seems goes to packages#edit_multiple controller
so paste your edit_multiple method, not create method
are you implementing your scenario above with javascript, or just plain HTML?

Nested Resource Rails

I have two models namely todo and tasks. Have set up a Has_many, Belongs_to assosciation between them (todo has many tasks, task belongs to todo).
I have set up a nested resource between them as shown below in my routes file
resources :todos do
resources :tasks
end
My form for creating new task is as shown below:
<%= form_for([#todo, #task], html:{class:'form-horizontal'}) do |f| %>
<% if #task.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#task.errors.count, "error") %> prohibited this todo from being saved:</h2>
<ul>
<% #task.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<script type="text/javascript">
$(document).ready(function () {
$('#datep').on('change', function () {
$('.datepicker').hide();
});
});
</script>
<div class="container">
<table style="width:70%" class="table">
<tr>
<td><%= f.label :title , "Task Title"%>
<%= f.text_field :title,:class=>'form-control input-sm' %></td>
</tr>
<tr>
<td><%= f.label :description,"Task Description"%>
<%= f.text_field :description,:class=>'form-control input-sm' %></td>
</tr>
<tr>
<td><%= f.label :deadline,"Task DeadLine"%>
<h id="datep"><%= f.text_field :deadline, :class=>'form-control input-sm', "data-provide" => "datepicker" ,"data-date-format" => "mm-dd-yyyy"%></h></td>
</tr>
<tr>
<td><%= f.label :assignee,"Task Assignee" %><br/>
<%= f.collection_select :assignee, User.all, :name, :name, {}, :class => "btn btn-default dropdown-toggle"%></td>
</tr>
<tr>
<td><%= f.label :tags,"Tags for Task"%>
<%= f.text_field :tags,:class=>'form-control input-sm' %></td>
</tr>
<tr>
<td><%= f.label :state,"Task Status"%><br/>
<%= f.select :state, ['Yet To Start', 'In Progress', 'Finished'], {}, :class => "btn btn-default dropdown-toggle"%></td>
</tr>
</table>
</div>
<br />
<div class="actions form-group">
<div class="container">
<%= f.submit 'Create New Task', class:'btn btn-success'%>
<%= link_to todos_path, class: 'btn btn-warning' do %>
<i class="glyphicon glyphicon-hand-left"></i> Back To List
<% end %>
</div>
</div>
<% end %>
My tasks controller code is shown below:
class TasksController < ApplicationController
def new
#task = Task.new
end
def create
#todo = Todo.find.(params[:todo_id])
#task = #todo.tasks.build(task_params)
if #task.save
redirect_to [#todo, #task]
else
redirect_to root_path
end
end
private
def set_todo
#todo = Todo.find(params[:todo_id])
end
def task_params
params.require(:task).permit(:assignee, :deadline, :state, :tags, :title, :description)
end
end
However when i want to add a new task to an particular todo i get this error saying
undefined method `tasks_path' for #<#<Class:0x007fd16a9c8810>:0x007fd169c95a80>
Did you mean? asset_path
Need help to know what am I doing wrong here???
redirect_to only accepts hash, record or string so this line redirect_to [#todo, #task] won't work. See http://api.rubyonrails.org/classes/ActionController/Redirecting.html. You can change it to redirect_to todo_tasks_path(#todo) which is reasonable here.
I also just noticed you didn't put before_filter :set_todo in the controller. You can remove #todo = Todo.find.(params[:todo_id]) in create action as it's no longer needed.
Hope it helps.

How to submit multiple forms with one button

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?

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'

Display array element in rails

I try to make the two columns list of products, this is my code
<table>
<% (0..#products.length-1).step(2) do |i| %>
<tr><td><div id="product_left">
<%= image_tag #products[i].photo.url(:small) %>
<%= #products[i].name %><br/>
Price: <%= #products[i].price %><br/>
<%= link_to "details", :action=>"show", :id=> #products[i].id %>
<%= link_to "add to card", {:controller=> "carts", :action=>"add", :id=> #products[i].id}, :remote=> true %>
</div> </td>
<% if #products.length > i %>
<td><div id="product_right">
<%= image_tag #products[i+1].photo.url(:small) %>
<%= #products[i+1].name %><br/>
Price: <%= #products[i+1].price %><br/>
<%= link_to "details", :action=>"show", :id=> #products[i+1].id %>
<%= link_to "add to card", {:controller=> "carts", :action=>"add", :id=> #products[i+1].id}, :remote=> true %>
</div></td>
<% end %>
</tr>
<% end %>
</table>
the problem is in the second div, rails give me an error on #products[i+1]. How can I solve it?
<table>
<% #products.each_slice(2) do |products| -%>
<tr>
<% products.zip(["left", "right"]).each do |product, side| -%>
<td>
<div id="product_<%= side %>">
<%= image_tag product.photo.url(:small) %>
<%= product.name %><br/>
Price: <%= product.price %><br/>
<%= link_to "details", product %>
<%= link_to "add to card", [:add, :carts, product], :remote=> true %>
</div>
</td>
<% end %>
</tr>
<% end -%>
</table>
Also you shouldn't use not uniq id. Here you have got multiple product_left and product_right ids. That's not good

Resources