The author built a UPS scaffold and rails created the model as up instead of ups. I've hacked some solutions in to make it work, but I can't get the create method to work when called from the new form. Rails calls the index function and no new object is created.
ups_controller.rb
def create
#up = Ups.new(params[:up])
respond_to do |format|
if #up.save
format.html { redirect_to #up, notice: 'Up was successfully created.' }
format.json { render json: #up, status: :created, location: #up }
else
format.html { render action: "new" }
format.json { render json: #up.errors, status: :unprocessable_entity }
end
end
end
def new
#up = Ups.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #up }
end
end
ups/form.html.erb
<%= form_for(#up) do |f| %>
<table class="table table-striped table-bordered">
<tr>
<th>name</th>
<td><%= f.text_field :name %></td>
</tr>
<tr>
<th>attr</th>
<td><%= f.select :attr_id, options_from_collection_for_select(Attr.order(:name).where(:major => true).all, "id", "name", #up.attr_id) %></td>
</tr>
<tr>
<th>Action</th>
<td><%= f.submit "Submit New UPS" %></td>
</tr>
</table>
<% end %>
routes.rb
resources :ups
match 'ups/create' => 'ups#create'
match 'ups/index' => 'ups#index'
match 'pdus/link_all_ups' => 'pdus#link_all_ups'
Here is the button that calls the new method of the ups_controller:
<%= link_to 'Enter New UPS', new_ups_path, :class => "btn btn-success pull-right" %>
rake routes
ups GET /ups(.:format) ups#index
POST /ups(.:format) ups#create
new_up GET /ups/new(.:format) ups#new
edit_up GET /ups/:id/edit(.:format) ups#edit
up GET /ups/:id(.:format) ups#show
PUT /ups/:id(.:format) ups#update
DELETE /ups/:id(.:format) ups#destroy
ups_index /ups/index(.:format) ups#index
Thanks for any advice.
You also want to make sure rails is sending a Post request like this:
<%= form_for #up, :url=> ups_path, :method => :post do |f| %>
My guess is that your url is correct but rails in sending it as a get instead of a post. As you can see from your routes, the index and create action both have the same url and the only difference is the get/post.
ups GET /ups(.:format) ups#index
POST /ups(.:format) ups#create
Related
project_site model has one attribute submission_status. i want to set that status from index#action for there corresponding row. i want on submit button click on each row to change there correspong submission_status to true. how can i do this in rails 5.
project_sites_controller.rb
def index
#project_sites = current_user.project_sites
end
def new
#project_site = current_user.project_sites.build
end
def create
#project_site = current_user.project_sites.build(project_site_params)
#users = current_user.director_id
#projects = Project.where(user_id: #users)
respond_to do |format|
if #project_site.save
format.html { redirect_to project_sites_url, notice: 'Attendance was successfully Uploaded.' }
format.json { render :show, status: :created, location: #project_site }
else
format.html { render :new }
format.json { render json: #project_site.errors, status: :unprocessable_entity }
end
end
end
def update
#users = current_user.director_id
#projects = Project.where(user_id: #users)
respond_to do |format|
if #project_site.update(project_site_params)
format.html { redirect_to project_sites_url, notice: 'Attendance was successfully updated.' }
format.json { render :show, status: :ok, location: #project_site }
else
format.html { render :edit }
format.json { render json: #project_site.errors, status: :unprocessable_entity }
end
end
end
index.html.erb
<table>
<thead>
<tr>
<th>Uploaded Date</th>
<th>Attendance File</th>
<th>Submit Attendance</th>
</tr>
</thead>
<tbody>
<% #project_sites.each do |project_site| %>
<tr>
<td><%= project_site.created_at.strftime('%d-%m-%Y') %></td>
<td><%= link_to "View Attendance", project_site.attendance.url, :class => "fi-page-export-csv" %></td>
<td>
<%= form_for ProjectSite.new do |f| %>
<%#f.hidden_field :project_site_id, value: project_site.id%>
<%=f.hidden_field :submission_status, value: true%>
<div>
<%= f.submit 'Submit', :class => 'button primary small float-right' %>
</div>
<% end %>
</tr>
<% end %>
</tbody>
</table>
add a route for an action which set status
#routes.rb
resources :project_sites do
put :set_submission_status, on: :member
end
define action which set status
#submission_status_controller.rb
def set_submission_status
#project_site = ProjectSite.find(params[:id])
#project_site.update(submission_status: true)
redirect_to project_sites_path
end
replace form_for with following link
#view
= link_to set_submission_status_project_site_path(project_site), method: :put
You have to pass the id of project_site for which you want to update the submission_status and on the controller updates the same project_site(id pass as hidden field), for this you need write **ajax** call
<table>
<thead>
<tr>
<th>Uploaded Date</th>
<th>Attendance File</th>
<th>Submit Attendance</th>
</tr>
</thead>
<tbody>
<% #project_sites.each do |project_site| %>
<tr>
<td><%= project_site.created_at.strftime('%d-%m-%Y') %></td>
<td><%= link_to "View Attendance", project_site.attendance.url, :class => "fi-page-export-csv" %></td>
<td>
<%= form_for ProjectSite.new do |f| %>
<%#f.hidden_field :project_site_id, value: project_site.id%>
<%=f.hidden_field :submission_status, value: true%>
<%= f.hidden_field :project_site_id, project_site.id%>
<div>
<%= f.submit 'Submit', :class => 'button primary small float-right' %>
</div>
<% end %>
</tr>
<% end %>
</tbody>
</table>
I have a method called calculation_of_total_cost in model Tippy
It's running into problems being called in index.html.erb via tippies views directory.
This is the error I receive: undefined method*' for nil:NilClass`
I have googled it, and now understand that it is the result of the one of the variables being nil.
How do I resolve this, i.e, how do I make the method work in index.html.erb? This is index view that I am calling it from, so I need an instance method, not class, right?
Also, addendum: this same method works fine in show.html.erb
show.html.erb
<br/><br/>
<h1 class="text-center">Your Total Cost</h1>
<br/><br />
<table class="table table-striped">
<tr>
<td>
Cost of Your Meal:
</td>
<td>
<%= humanized_money_with_symbol #tippy.cost %>
</td>
</tr>
<tr>
<td>
Tip You Picked:
</td>
<td>
<%= number_to_percentage(#tippy.tip * 100, format: "%n%", precision: 0) %>
</td>
</tr>
<tr>
<td>
The Total Cost:
</td>
<td>
<%= humanized_money_with_symbol #tippy.calculation_of_total_cost %>
</td>
</tr>
</table>
<%= link_to 'New Tippy', new_tippy_path %>
<%= link_to "Index", tippies_path %>
Here is the Tippy model:
class Tippy < ApplicationRecord
validates :tip, presence: true
validates :cost, presence: true
#monetize :tip_cents
monetize :cost_cents, :numericality => {:greater_than => 0}
TIP_CHOICES = { "10%" => ".10", "20%" => ".20", "30%" => ".30", "40%" => ".40", "50%" => ".50",
"60%" => ".60", "70%" => ".70", "80%" => ".80", "90%" => ".90" }
def calculation_of_total_cost
cost + (tip * cost)
end
end
Here is the index.html.erb file
<p id="notice"><%= notice %></p>
<h1>Tippies</h1>
<table>
<thead>
<tr>
<th>Tip</th>
<th>Cost</th>
<th>Total</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #tippies.each do |tippy| %>
<tr>
<td><%= tippy.tip %></td>
<td><%= tippy.cost %></td>
<td><%= tippy.calculation_of_total_cost %></td>
<td><%= link_to 'Show', tippy %></td>
<td><%= link_to 'Edit', edit_tippy_path(tippy) %></td>
<td><%= link_to 'Destroy', tippy, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Tippy', new_tippy_path %>
Tippy Controller
class TippiesController < ApplicationController
#before_action :set_tippy, only: [:show, :edit, :update, :destroy]
# GET /tippies
# GET /tippies.json
def index
#tippies = Tippy.all
end
# GET /tippies/1
# GET /tippies/1.json
def show
##calculation_of_total_cost
end
# GET /tippies/new
def new
#tippy = Tippy.new
end
# GET /tippies/1/edit
def edit
end
# POST /tippies
# POST /tippies.json
def create
#tippy = Tippy.new(tippy_params)
respond_to do |format|
if #tippy.save
format.html { redirect_to #tippy, notice: 'Tippy was successfully created.' }
format.json { render :show, status: :created, location: #tippy }
else
format.html { render :new }
format.json { render json: #tippy.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tippies/1
# PATCH/PUT /tippies/1.json
def update
respond_to do |format|
if #tippy.update(tippy_params)
format.html { redirect_to #tippy, notice: 'Tippy was successfully updated.' }
format.json { render :show, status: :ok, location: #tippy }
else
format.html { render :edit }
format.json { render json: #tippy.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tippies/1
# DELETE /tippies/1.json
def destroy
#tippy.destroy
respond_to do |format|
format.html { redirect_to tippies_url, notice: 'Tippy was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_tippy
#tippy = Tippy.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def tippy_params
params.require(:tippy).permit(:tip, :cost)
end
end
To solve this problem you need to set a binding.pry or a breakpoint at this line of index.html.erb, so that we can understand in the loop you are executing why tippy is getting value of nil.
You need to install pry gem.
Please also share the values of #tippies and the details of the other variable in the loop that fails, because tippy=nil.
An alternative for pry is just printing the value of the variable in the log with puts tippy.calculation_of_total_cost.
Right now I am guess is that #tippies which includes all #tippy in your tippies table, could have one field that has calculation of total cost = nil. To verifiy this you should check with the debug the value of tippy and of tippy.calculation_of_total_cost in the index.html.erb view.
<% #tippies.each do |tippy| %>
<tr>
<% binding.pry %>
<td><%= tippy.tip %></td>
<td><%= tippy.cost %></td>
<td><%= tippy.calculation_of_total_cost %></td>
<td><%= link_to 'Show', tippy %></td>
<td><%= link_to 'Edit', edit_tippy_path(tippy) %></td>
<td><%= link_to 'Destroy', tippy, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
also it is a good idea to inspect show.html.erb as there it is working.
def calculation_of_total_cost
cost + (tip * cost)
end
Pls i need help here.
i have 2 models:- due_job and outgoing_job
due_job has_many outgoing_jobs
outgoing_job belongs_to due_job.
Im trying to update a user's outgoing_job as done while at the same time create a due_job for another user.
My models:
class DueJob < ActiveRecord::Base
belongs_to :user
has_many :outgoing_jobs
accepts_nested_attributes_for :outgoing_jobs
end
class OutgoingJob < ActiveRecord::Base
belongs_to :user
belongs_to :outgoing_jobs
accepts_nested_attributes_for :outgoing_jobs
end
Controllers:
class OutgoingJobsController < ApplicationController
def index
#outgoing_job = OutgoingJob.new
#outgoing_jobs = OutgoingJob.all
end
def new
#outgoing_job = OutgoingJob.new
end
def create
#outgoing_job = OutgoingJob.new(outgoing_job_params)
respond_to do |format|
if #outgoing_job.save
flash.now[:success] = "saved"
format.html {redirect_to current_user}
format.json {render json: #outgoing_job, status: :created, location: #outgoing_job}
else
flash[:danger] = "not saved"
format.html {redirect_to root_path}
format.json {render json: #outgoing_job.errors, status: :unprocessable_entity }
end
end
end
def show
#outgoing_job = OutgoingJob.find(params[:id])
end
def update
#outgoing_job = OutgoingJob.find(params[:id])
respond_to do |format|
if #outgoing_job.update(outgoing_job_params)
format.html { redirect_to '/users/outgoing_job_dashboard', notice: 'job updated' }
format.json {render action: 'show', status: :ok, location: #outgoing_job }
else
format.html { render action: 'edit'}
format.json { render json: #outgoing_job.errors, status: :unprocessable_entity}
end
end
end
def destroy
#outgoing_job.destroy
respond_to do |format|
format.html {redirect_to current_user}
format.json { head :no_content}
end
end
private
def outgoing_job_params
params.require(:outgoing_job).permit(:outgoing_job_value,
:sent,
:confirmed,
:done,
:due_job_id,
:user_id )
end
end
the controller for due_jobs is essentially same.
However, when i do this in my view:
<% OutgoingJob.all.each do |od| %>
<table class="table table-striped table-responsive">
<thead>
<tr>
<th>ID</th>
<th>Done By</th>
<th>Done for</th>
<th>Beneficiary</th>
<th>Amount proposed</th>
<th>Amount to paid</th>
<th>Create due job</th>
<th>Actions</th>
</tr>
</thead>
<% if (od.confirmed == true) && (od.done== false) %>
<tbody>
<tr>
<td><%= od.id %></td>
<td><%= od.user.first_name %> <%= od.user.last_name %></td>
<td><%= od.due_job.user.first_name %> <%= od.due_job.user.last_name %></td>
<td><%= od.due_job.user.user_detail %></td>
<td>$ <%= number_with_delimiter(od.outgoing_job_value, delimiter: ',') %> </td>
<td> <%= --- %> </td>
<td>
<%= simple_form_for (DueJob.new) do |u| %>
<%= u.hidden_field :due_job_value, value: od.outgoing_job_value %>
<%= u.hidden_field :user_id, value: od.user.id %>
<%= u.fields_for od do |f| %>
<%= f.hidden_field :done, value: true %>
<%end%>
<%= u.submit "create", class: "btn btn-success" %>
<%end%>
</td>
<td><%= link_to "View", od %></td>
</tr>
</tbody>
<%end%>
</table>
.....
Using nested form i am able to create a new record for DueJob tho, but it doesnt update outgoing_job. What am i missing pls?
I suggest you make use of ActiveRecord callbacks to relegate some of the code to your models (I doubt that trying to do everything from one view is the way to go).
In your model for DueJob add something like:
class DueJob < ActiveRecord::Base
# Register a callback to execute every time you create a new DueJob
after_create :update_done_flag_on_outgoing_job
# ... and add some code for that callback.
def update_done_flag_on_outgoing_job
outgoing_job.update_attribute :done, true
end
Just from reading your source code, I was struggling to understand how you identify the connections between the newly created DueJob and the specific OutgoingJob record that you want to update. Like #coreyward pointed out, it would be helpful if you could present your code a bit more neatly. If you can't use my example snippet as is, I guess you can always adapt the update_done_flag_on_outgoing_job method to your needs.
Hope you find that helpful.
Hi I generated a scaffold for my rails application. I put in resources for that model in the config file. I figure I can cut/modify what scaffold generated later. I removed the show method/view etc and now destroy/edit no longer works.
Turns out that destroy/edit need show to be there as well because of resource.
I want to push show back into my application to fix this issue, can someone help me pinpoint the issue?
trashes_controller.rb
class TrashesController < ApplicationController
# GET /trashes
# GET /trashes.json
def index
#trash = Trash.all
#json = #trash.to_gmaps4rails
end
end
# GET /trashes/1
# GET /trashes/1.json
def show
#trash = Trash.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #trash }
end
end
# GET /trashes/new
# GET /trashes/new.json
def new
#trash = Trash.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #trash }
end
end
# GET /trashes/1/edit
def edit
#trash = Trash.find(params[:id])
end
# POST /trashes
# POST /trashes.json
def create
#trash = Trash.new(params[:trash])
respond_to do |format|
if #trash.save
format.html { redirect_to #trash, notice: 'Trash was successfully created.' }
format.json { render json: #trash, status: :created, location: #trash }
else
format.html { render action: "new" }
format.json { render json: #trash.errors, status: :unprocessable_entity }
end
end
end
# PUT /trashes/1
# PUT /trashes/1.json
def update
#trash = Trash.find(params[:id])
respond_to do |format|
if #trash.update_attributes(params[:trash])
format.html { redirect_to #trash, notice: 'Trash was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #trash.errors, status: :unprocessable_entity }
end
end
end
# DELETE /trashes/1
# DELETE /trashes/1.json
def destroy
#trash = Trash.find(params[:id])
#trash.destroy
respond_to do |format|
format.html { redirect_to trashes_url }
format.json { head :no_content }
end
end
index.html.erb within trashes folder
<h1>Listing Boston/Cambridge trash bin locations</h1>
<%= gmaps4rails(#json) %>
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Category</th>
<th></th>
<th></th>
</tr>
<% #trash.each do |trash| %>
<tr>
<td><%= trash.name %></td>
<td><%= trash.address %></td>
<td><%= trash.category %></td>
<td><%= link_to 'Show', trash %></td>
<td><%= link_to 'Edit', edit_trash_path(trash) %></td>
<td><%= link_to 'Destroy', trash, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Boston Solar Powered Trash Can Location', new_trash_path %>
show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= #trash.name %>
</p>
<p>
<b>Address:</b>
<%= #trash.address %>
</p>
<p>
<b>Category:</b>
<%= #trash.category %>
</p>
<%= link_to 'Edit', edit_trash_path(#trash) %> |
<%= link_to 'Back', trashes_path %>
I keep on getting undefined method `name' for nil:NilClass when I click show in my index root page in local host.
Here is my rake routes pages, probably more useful.
trashes GET /trashes(.:format) trashes#index
POST /trashes(.:format) trashes#create
new_trash GET /trashes/new(.:format) trashes#new
edit_trash GET /trashes/:id/edit(.:format) trashes#edit
trash GET /trashes/:id(.:format) trashes#show
PUT /trashes/:id(.:format) trashes#update
DELETE /trashes/:id(.:format) trashes#destroy
root / trashes#index
page GET /pages/*id high_voltage/pages#show
If your error occur when you try to click 'show' link, than modify your link_to path like this :
td><%= link_to 'Show', trash_path(trash) %></td>
Hope this will help.
I am implementing an application in which i want to change the settings of the application.
While doing so I am getting an error
no routes matches {:action=>"show", :controller=>"settings", format=>"nil"}
while clicking on the open new settings tab.
my index.html is as follows:-
<h1>Listing settings</h1>
<table class="table table-striped table-bordered">
<tr>
<th>ID</th>
<th>Name</th>
<th>Value</th>
<th>Description</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<% #settings.each do |c| %>
<tr>
<td><%= c.id %> </td>
<td><%= c.name %> </td>
<td><%= c.value %> </td>
<td><%= c.description %> </td>
<td><%= link_to 'Edit', {:action => 'edit', :id => c.id} %> </td>
<td><%= link_to 'Delete', {:action => 'delete', :id => c.id},
:data => { :confirm => "Are you sure you want to delete this value?" } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Setting', {:action => 'new'} %>
My settings controller is as follows:-
class SettingsController < ApplicationController
# GET /setting
# GET /setting.json
def index
#settings = Setting.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #settings }
end
end
# GET /setting/1
# GET /setting/1.json
def show
#setting = Setting.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #setting }
end
end
# GET /setting/new
# GET /setting/new.json
def new
#setting = Setting.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #setting }
end
end
# GET /setting/1/edit
def edit
#setting = Setting.find(params[:id])
end
# POST /setting
# POST /setting.json
def create
#setting = Setting.new(params[:setting])
respond_to do |format|
if #setting.save
format.html { redirect_to #setting, notice: 'Setting was successfully created.' }
format.json { render json: #setting, status: :created, location: #setting }
else
format.html { render action: "new" }
format.json { render json: #setting.errors, status: :unprocessable_entity }
end
end
end
# PUT /setting/1
# PUT /setting/1.json
def update
#setting = Setting.find(params[:id])
respond_to do |format|
if #setting.update_attributes(params[:setting])
format.html { redirect_to #setting, notice: 'Setting was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #setting.errors, status: :unprocessable_entity }
end
end
end
# DELETE /setting/1
# DELETE /setting/1.json
def delete
#setting = Setting.find(params[:id])
#setting.deleted = 1
#setting.save
respond_to do |format|
format.html { redirect_to settings_url }
format.json { render :json => { :success => true } }
end
end
end
My new.html is as follows:-
<h1>New settings</h1>
<%= form_for #setting do |f| %>
<% if #setting.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(#setting.errors.count, "error") %> prohibited this setting from being saved:</h2>
<ul>
<% #setting.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Id: <%= f.text_field :id %><br>
Name: <%= f.text_field :name %><br>
Values: <%= f.text_field :value %><br>
Description: <%= f.text_field :description %><br>
<% end %>
<%= link_to 'Back', settings_path %>
My routes.rb is as follows:-
Lms::Application.routes.draw do
resources :books do
member do
post 'add'
post 'remove'
end
collection do
get 'add'
get 'list' => "books#index"
post 'get_books'
get 'get_books'
end
end
resources :books
resources :book_transactions
resources :book_issues
resources :book_holds
resources :categories
resources :users
resources :admins
resources :library_locations
resources :lov_values
resources :loan_fines
resources :lov_names
resources :loans_fines do
member do
post 'add'
post 'remove'
end
collection do
get 'add'
get 'list'
post 'get_loans_fines'
get 'get_loans_fines'
end
end
resources :settings do
member do
post 'add'
post 'remove'
end
collection do
get 'add'
get 'list'
post 'get_settings'
get 'get_settings'
end
end
root :to => 'books#index'
match ':controller(/:action(/:id))(.:format)'
The strange part is that when i click on new action it is going/redirecting to the show action..I am a bit confused why this is happening..
Can some one help me with this..
To check your routes you can run
rake routes
to inspect if you have the routes defined correctly. Also I would suggest to use
<%= link_to "New Setting", new_setting_pah %>
For your routes definition you are not defining the routes for other methods than add or remove.
I advise you to read this great tutorial on rails routes