We are trying to insert an object into our rails controller (terminology may be wrong i'm new to ruby and rails) that has objects which contain objects. For just the basic portion we are trying to insert a location that has a restaurant, so the restaurant has an id that refers to the location. Ideally we'd like it to only insert the location if it doesn't exist and give it the id after insert, if it already exists just fill in the id that already exists. Right now we are inserting the location but the restaurants insert never happens.
Heres the sample json object we are trying to insert, it also has the other controllers we are trying to insert into. Location has one restaurant that has many inspections that has many violations. Tables are (locations, restaurants, inspections, violations)
{
"locations": {
"restaurants_attributes": {
"name": "CORNERSTONE GROUP HOME",
"type": "INSTITUTION",
"inspections_attributes": {
"date": "2013-11-19",
"number": "26134",
"violations_attributes": {
"comment": "WOODEN SHELVING IN DISREPAIR",
"code": "4-501.11",
"critical": "0"
}
}
},
"st_apt_num": "",
"st_dir": "E",
"st_number": "1250",
"st_suffix": "RD",
"zip_code": "65201",
"latitude": 38.9808446,
"longitude": -92.289225
}
}
Here is the models for locations / restaurants
class Location < ActiveRecord::Base
has_one :restaurants
accepts_nested_attributes_for :restaurants
end
class Restaurant < ActiveRecord::Base
belongs_to :locations
has_many :inspections
accepts_nested_attributes_for :inspections
end
Here is the migrate for locations / restaurants
class CreateLocations < ActiveRecord::Migration
def change
create_table :locations do |t|
t.integer :st_apt_num
t.string :st_dir
t.integer :st_number
t.string :st_suffix
t.integer :zip_code
t.decimal :latitude, :precision => 9, :scale => 7
t.decimal :longitude, :precision => 9, :scale => 7
t.timestamps
end
end
end
class CreateRestaurants < ActiveRecord::Migration
def change
create_table :restaurants do |t|
t.belongs_to :locations
t.string :name
t.string :type
t.timestamps
end
end
end
Here is the controllers for locations / restaurants (haven't changed anything here though)
class LocationsController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :set_location, only: [:show, :edit, :update, :destroy]
# GET /locations
# GET /locations.json
def index
#locations = Location.all
end
# GET /locations/1
# GET /locations/1.json
def show
end
# GET /locations/new
def new
#location = Location.new
end
# GET /locations/1/edit
def edit
end
# POST /locations
# POST /locations.json
def create
#location = Location.new(location_params)
respond_to do |format|
if #location.save
format.html { redirect_to #location, notice: 'Location was successfully created.' }
format.json { render action: 'show', status: :created, location: #location }
else
format.html { render action: 'new' }
format.json { render json: #location.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /locations/1
# PATCH/PUT /locations/1.json
def update
respond_to do |format|
if #location.update(location_params)
format.html { redirect_to #location, notice: 'Location was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #location.errors, status: :unprocessable_entity }
end
end
end
# DELETE /locations/1
# DELETE /locations/1.json
def destroy
#location.destroy
respond_to do |format|
format.html { redirect_to locations_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_location
#location = Location.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def location_params
params.require(:locations).permit(:st_apt_num, :st_dir, :st_number, :st_suffix, :zip_code, :latitude, :longitude)
end
end
class RestaurantsController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :set_restaurant, only: [:show, :edit, :update, :destroy]
# GET /restaurants
# GET /restaurants.json
def index
#restaurants = Restaurant.all
end
# GET /restaurants/1
# GET /restaurants/1.json
def show
end
# GET /restaurants/new
def new
#restaurant = Restaurant.new
end
# GET /restaurants/1/edit
def edit
end
# POST /restaurants
# POST /restaurants.json
def create
#restaurant = Restaurant.new(restaurant_params)
respond_to do |format|
if #restaurant.save
format.html { redirect_to #restaurant, notice: 'Restaurant was successfully created.' }
format.json { render action: 'show', status: :created, location: #restaurant }
else
format.html { render action: 'new' }
format.json { render json: #restaurant.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /restaurants/1
# PATCH/PUT /restaurants/1.json
def update
respond_to do |format|
if #restaurant.update(restaurant_params)
format.html { redirect_to #restaurant, notice: 'Restaurant was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #restaurant.errors, status: :unprocessable_entity }
end
end
end
# DELETE /restaurants/1
# DELETE /restaurants/1.json
def destroy
#restaurant.destroy
respond_to do |format|
format.html { redirect_to restaurants_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_restaurant
#restaurant = Restaurant.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def restaurant_params
params.require(:restaurant).permit(:name, :loc_id, :type)
end
end
Related
I'm creating a task manager using Ruby on Rails.
My task manager has a Task model table that includes a column called duedate.
I need to be able to find all tasks that are overdue.
How can I write a function that automatically allows me to mark all overdue tasks to True?
This is my Schema.
create_table "tasks", force: :cascade do |t|
t.string "title"
t.string "description"
t.datetime "duedate"
t.boolean "completed"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
my tasks controller looks like this
class TasksController < ApplicationController
before_action :set_task, only: %i[ show edit update destroy ]
# before_action :due_today, only: %i[ index show]
# GET /tasks or /tasks.json
def index
#tasks = Task.all.overdue.order(duedate: :asc)
end
# GET /tasks/1 or /tasks/1.json
def show
end
# GET /tasks/new
def new
#task = Task.new
end
# GET /tasks/1/edit
def edit
end
# POST /tasks or /tasks.json
def create
#task = Task.new(task_params)
respond_to do |format|
if #task.save
format.html { redirect_to #task, notice: "Task was successfully created." }
format.json { render :show, status: :created, location: #task }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tasks/1 or /tasks/1.json
def update
respond_to do |format|
if #task.update(task_params)
format.html { redirect_to #task, notice: "Task was successfully updated." }
format.json { render :show, status: :ok, location: #task }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tasks/1 or /tasks/1.json
def destroy
#task.destroy
respond_to do |format|
format.html { redirect_to tasks_url, notice: "Task was successfully destroyed." }
format.json { head :no_content }
end
end
def overdue?
overdue < Time.now
end
# def overdue
# #tasks = Task.where(overdue: true)
# end
# def due_today
# #tasks = Task.where(duedate: = Date.now)
# end
private
# Use callbacks to share common setup or constraints between actions.
# def overdue?
# if(DateTime.now > duedate)
# #task.overdue = true
# else
# #task.overdue = false
# end
# end
# def overdue
# #tasks = Task.where(overdue: true)
# end
def set_task
#task = Task.find(params[:id])
end
# Only allow a list of trusted parameters through.
def task_params
params.require(:task).permit(:title, :description, :duedate, :completed, :overdue)
end
end
my model looks like this
class Task < ApplicationRecord
scope :overdue, -> { where("duedate < ?", Time.now) }
def overdue?
overdue < Time.now
end
end
I suggest removing the overdue column from your database. It is not useful because the comparison for the duedate column to the current time is more precise and doesn't depend on the manual setting of a boolean column.
You can use a scope to find all records that are overdue:
# in your model at app/models/task.rb
scope :overdue, -> { where("duedate < ?", Time.now) }
# in your controller
def index
#tasks = Task.overdue.order(:duedate)
end
When you want to have an overdue? method on your model, for example, to show an overdue flag in the view then add an overdue? method to your model:
# in your model at app/models/task.rb
def overdue?
duedate < Time.now
end
So I have locker model which is just a basic scaffold
def change
create_table :lockers do |t|
t.references :user
t.integer :lockerNo
t.string :wing
t.string :sttatus
t.string :comment
t.timestamps
end
end
end
and also a user model which is just basic devise.
I want users to be able to select a locker they want,
So I want to list all the lockers and then users click on Select button and then the locker gets assigned to the user i.e the locker_id updates to the current user
This is my controller
before_action :set_locker, only: [:show, :edit, :update, :destroy]
# GET /lockers
# GET /lockers.json
def index
#lockers = Locker.all
end
# POST /lockers
# POST /lockers.json
def create
#locker = Locker.new(locker_params)
respond_to do |format|
if #locker.save
format.html { redirect_to #locker, notice: 'Locker was successfully created.' }
format.json { render :show, status: :created, location: #locker }
else
format.html { render :new }
format.json { render json: #locker.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /lockers/1
# PATCH/PUT /lockers/1.json
def update
respond_to do |format|
if #locker.update(locker_params)
format.html { redirect_to #locker, notice: 'Locker was successfully updated.' }
format.json { render :show, status: :ok, location: #locker }
else
format.html { render :edit }
format.json { render json: #locker.errors, status: :unprocessable_entity }
end
end
end
def assign_locker
if #locker.sttatus = Available
#locker = Locker.find(params[:id])
#locker.user_id = current_user.id
#locker.save
end
redirect_to root_path
end
private
def set_locker
#locker = Locker.find(params[:id])
end
def locker_params
params.require(:locker).permit(:user_id, :lockerNo, :wing, :sttatus, :comment)
end
end
Make a controller method for updating the locker with the current user.
Put /lockers/[locker_id]
def update
locker = Locker.findById(params[:locker_id])
locker.user = current_user
if locker.save
head 200
else
head 422
end
end
That's really ugly code that makes a few assumptions... but is it not what you're trying to do?
Running Rails:
Rails 4.2.5
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
I'm trying to return a value from one model (eds) within a view of another model (clients).
I believe I I'm very close to a solution. However, I'm at a road block. I'm want return the name of the school (eds) in the client view.
As of now, I'm defining this in my client controller:
def index
#eds_school = Ed.name
end
I thought that Ed.name should work but it doesn't.
Then I'm calling the statement in the view:
<tb><%= #eds_school %></td>
I think my issue is within the client controller.
Here is the db schema:
create_table "eds", force: :cascade do |t|
t.string "name"
t.string "grade"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "school"
t.text "JtText"
end
Eds Controller:
class EdsController < ApplicationController
before_action :set_ed, only: [:show, :edit, :update, :destroy]
# GET /eds
# GET /eds.json
def index
#eds = Ed.all
end
# GET /eds/1
# GET /eds/1.json
def show
end
# GET /eds/new
def new
#ed = Ed.new
end
# GET /eds/1/edit
def edit
end
# POST /eds
# POST /eds.json
def create
#ed = Ed.new(ed_params)
respond_to do |format|
if #ed.save
format.html { redirect_to #ed, notice: 'Ed was successfully created.' }
format.json { render :show, status: :created, location: #ed }
else
format.html { render :new }
format.json { render json: #ed.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /eds/1
# PATCH/PUT /eds/1.json
def update
respond_to do |format|
if #ed.update(ed_params)
format.html { redirect_to #ed, notice: 'Ed was successfully updated.' }
format.json { render :show, status: :ok, location: #ed }
else
format.html { render :edit }
format.json { render json: #ed.errors, status: :unprocessable_entity }
end
end
end
# DELETE /eds/1
# DELETE /eds/1.json
def destroy
#ed.destroy
respond_to do |format|
format.html { redirect_to eds_url, notice: 'Ed was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_ed
#ed = Ed.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def ed_params
params.require(:ed).permit(:name)
end
end
Client Controller:
class ClientsController < ApplicationController
before_action :authenticate_user!
before_action :set_client, only: [:show, :edit, :update, :destroy]
# GET /clients
# GET /clients.json
def index
#clients = Client.all.uniq.order("created_at DESC")
#clients_count = Client.uniq.count
#eds_school = Ed.name
end
# GET /clients/1
# GET /clients/1.json
def show
##notes = Note.all.uniq.order("created_at DESC")
#notes = Note.where(client_id: #client.id) #Where a note belong to the current user
end
# GET /clients/new
def new
#client = Client.new
end
# GET /clients/1/edit
def edit
end
# POST /clients
# POST /clients.json
def create
#client = Client.new(client_params)
respond_to do |format|
if #client.save
format.html { redirect_to #client, notice: 'Client was successfully created.' }
format.json { render :show, status: :created, location: #client }
else
format.html { render :new }
format.json { render json: #client.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /clients/1
# PATCH/PUT /clients/1.json
def update
#if params[:remove_image]
##client.remove_image!
#client.save
#end
respond_to do |format|
if #client.update(client_params)
format.html { redirect_to #client, notice: 'Client was successfully updated.' }
format.json { render :show, status: :ok, location: #client }
else
format.html { render :edit }
format.json { render json: #client.errors, status: :unprocessable_entity }
end
end
end
# DELETE /clients/1
# DELETE /clients/1.json
def destroy
#client.destroy
respond_to do |format|
format.html { redirect_to clients_url, notice: 'Client was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_client
#client = Client.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def client_params
params.require(:client).permit(:firstName, :lastName,:dob, :name, :gender_id, :RefbText, :JtText, :text_rs, :msub, :text_id, :ged_id, :mj_id, :od_id, :otc_id, :cigarette_id, :alcohol_id, :grad, :remove_image, :rh_options, :insurance_id, :state_id, :ed_id, :wk_id, :grade_id, :rsource_id, :image, :race_id, :employment_id, :comments, :clientemail, :phone, :truma_id, :college_id, :enrolled, :address, :city, :state, :zipcode, rhealth_ids:[], mhealth_ids:[], cparent_ids:[], preg_ids:[], referral_ids:[], refa_ids:[], refb_ids:[])
#params.require(:client).permit(:name, mhealth_ids:[])
end
end
Code Sample of Client Model:
class Client < ActiveRecord::Base
belongs_to :ed
end
Code Sample of Client DB Schema:
create_table "clients", force: :cascade do |t|
t.integer "ed_id"
end
Ed Controller:
class Ed < ActiveRecord::Base
has_many :clients
end
Its not completely clear what you're trying to achieve. Your code isnt working bcause name is an attribute and therefore a method on an instance of the class Eds. Look up the difference between class methods and instance methods. You need to find a particular instance of Eds and then call the name method on it e.g.
eds_school = Eds.find(1).name
where 1 is the database record in the eds table with an id of 1.
Because a client belongs to an Eds, you can access the eds name through a client instance. To display the eds.name for each client, you will need to loop through the clients variable. In the view
<% #clients.each do |client| %>
<%= client.eds.name %>
<% end %>
My issue is, when I am under the camper show page
Current Camper URL:
campers/1
and I go to click on to view the appointment it uses the camper_id for the appointment_id which is wrong so say if the camper_id is 1 it will use the appointment_id as 1 and actually the appointment id is 3, so then it says Couldn't find appointment with id of 1.
Table Header
<% #appointments.each do |app| %>
<%= link_to app.camper.camperName, appointment_path(#camper, #appointment) %>
Campers Controller Show Action
#appointments = #camper.appointments
Camper Model
has_many :appointments, dependent: :destroy
Appointment Model
belongs_to :camper
Shallow Nested Routes File
resources :customers, shallow: :true do
resources :campers do
resources :appointments do
resources :orders do
member do
patch :complete
end
end
end
end
end
Camper Controller
class CampersController < ApplicationController
before_action :set_camper, only: [:show, :edit, :update, :destroy]
# before_action :set_customer, only: [:index, :new, :edit, :create, :update]
load_and_authorize_resource
# GET /campers
# GET /campers.json
def index
#campers = #customer.campers
end
def list
query = params[:q].presence || ""
#campers = Camper.search(query, page: params[:page], per_page: 20, order: {created_at: :desc} )
end
# GET /campers/1
# GET /campers/1.js
def show
#appointments = #camper.appointments
respond_to do |format|
format.html
format.json
end
end
# GET /campers/new
def new
#customer = Customer.find(params[:customer_id])
#camper = #customer.campers.build
end
# GET /campers/1/edit
def edit
end
def page_name
"Campers"
end
# POST /campers
# POST /campers.json
def create
#camper = Camper.new(camper_params)
respond_to do |format|
if #camper.save
format.html { redirect_to camper_path(#camper), notice: 'Camper was successfully created.' }
format.json { render :show, status: :created, location: #camper }
else
format.html { render :new }
format.json { render json: #camper.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /campers/1
# PATCH/PUT /campers/1.json
def update
respond_to do |format|
if #camper.update(camper_params)
format.html { redirect_to camper_path(#camper), notice: 'Camper was successfully updated.' }
format.json { render :show, status: :ok, location: #camper }
else
format.html { render :edit }
format.json { render json: #camper.errors, status: :unprocessable_entity }
end
end
end
# DELETE /campers/1
# DELETE /campers/1.json
def destroy
#camper.destroy
respond_to do |format|
format.html { redirect_to root_path, notice: 'Camper was successfully deleted.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_camper
#camper = Camper.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def camper_params
params.require(:camper).permit(:order_id, :customer_id, :year, :manufacturer, :modelName, :camperClass, :vin, :mileage, :notes, :user_id)
end
end
Appointments Controller
class AppointmentsController < ApplicationController
before_action :set_appointment, only: [:show, :edit, :update, :destroy]
# GET /appointments
# GET /appointments.json
def index
#camper = Camper.find(params[:camper_id])
#appointments = #camper.appointments
end
# GET /appointments/1
# GET /appointments/1.json
def show
#orders = #appointment.orders
end
# GET /appointments/newå
def new
#camper = Camper.find(params[:camper_id])
#appointment = #camper.appointments.build
end
# GET /appointments/1/edit
def edit
end
# POST /appointments
# POST /appointments.json
def create
#appointment = Appointment.new(appointment_params)
respond_to do |format|
if #appointment.save
format.html { redirect_to appointment_path(#appointment), notice: 'Appointment was successfully created.' }
format.json { render :show, status: :created, location: #appointment }
else
format.html { render :new }
format.json { render json: #appointment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /appointments/1
# PATCH/PUT /appointments/1.json
def update
respond_to do |format|
if #appointment.update(appointment_params)
format.html { redirect_to #appointment, notice: 'Appointment was successfully updated.' }
format.json { render :show, status: :ok, location: #appointment }
else
format.html { render :edit }
format.json { render json: #appointment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /appointments/1
# DELETE /appointments/1.json
def destroy
#appointment.destroy
respond_to do |format|
format.html { redirect_to camper_appointments_path(#appointment), notice: 'Appointment was successfully deleted.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_appointment
#appointment = Appointment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def appointment_params
params.require(:appointment).permit(:customer_id, :camper_id, :order_id, :title, :description, :date_in, :date_out)
end
end
appointment_path only takes a single appointment argument. Remove the #camper argument:
appointment_path(#appointment)
I am getting NoMethodError in SitesController#index undefined method `name' for nil:NilClass in my Salesman index view and cannot find the culprit.
I have a simple rails app with the following tables: Customer, Salesman and Invoice.
In the index view for the customer table I have the following call:
<% #customers.each do |customer| %>
<%= customer.name %></td>
<%= customer.address %>
<%= customer.salesman.name %>
<% end %>
This is the call that results in the undefined method ´name´ listed above. I made sure the salesman table has a salesman_id foreign key in the customer table. Also, I made the respective association in the models:
class Customer < ActiveRecord::Base
belongs_to :salesman
has_many :invoices
end
class Salesman < ActiveRecord::Base
has_many :customers
end
I tested the customer.salesman.name call in the console and it works flawlessly. The show view for the customer has an exact call like this one and it also works. The only way I can make the customer index pass is by replacing customer.salesman.name to customer.salesman_id returning only the id.
Thank you for your time here.
***schema.rb***
ActiveRecord::Schema.define(version: 20150325172212) do
create_table "customers", force: :cascade do |t|
t.string "name"
t.string "address"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "salesman_id"
end
create_table "invoices", force: :cascade do |t|
t.datetime "date"
t.string "fid"
t.integer "salesman_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "address"
end
create_table "salesmen", force: :cascade do |t|
t.datetime "name"
t.string "address""
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
***controllers***
**customer**
Class CustomersController < ApplicationController
before_action :set_customer, only: [:show, :edit, :update, :destroy]
# GET /customers
# GET /customers.json
def index
#customers = Customer.all
end
# GET /customers/1
# GET /customers/1.json
def show
end
# GET /customers/new
def new
#customer = Customer.new
end
# GET /customers/1/edit
def edit
end
# POST /customers
# POST /customers.json
def create
#customer = Customer.new(customer_params)
respond_to do |format|
if #customer.save
format.html { redirect_to #customer, notice: 'Customer was successfully created.' }
format.json { render :show, status: :created, location: #customer }
else
format.html { render :new }
format.json { render json: #customer.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /customers/1
# PATCH/PUT /customers/1.json
def update
respond_to do |format|
if #customer.update(customer_params)
format.html { redirect_to #customer, notice: 'Customer was successfully updated.' }
format.json { render :show, status: :ok, location: #customer }
else
format.html { render :edit }
format.json { render json: #customer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /customers/1
# DELETE /customers/1.json
def destroy
#customer.destroy
respond_to do |format|
format.html { redirect_to customers_url, notice: 'Customer was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_cliente
#cliente = Cliente.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def customer_params
params.require(:customer).permit(:name, :address, :salesman_id)
end
end
***salesman***
class SalesmenController < ApplicationController
before_action :set_salesman, only: [:show, :edit, :update, :destroy]
# GET /salesmans
# GET /salesmans.json
def index
#salesmen = Salesman.all
end
# GET /salesmans/1
# GET /salesmans/1.json
def show
end
# GET /salesmans/new
def new
#salesman = Salesman.new
end
# GET /salesmans/1/edit
def edit
end
# POST /salesmans
# POST /salesmans.json
def create
#salesman = Salesman.new(salesman_params)
respond_to do |format|
if #salesman.save
format.html { redirect_to #salesman, notice: 'Salesman was successfully created.' }
format.json { render :show, status: :created, location: #salesman }
else
format.html { render :new }
format.json { render json: #salesman.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /salesmans/1
# PATCH/PUT /salesmans/1.json
def update
respond_to do |format|
if #salesman.update(salesman_params)
format.html { redirect_to #salesman, notice: 'Salesman was successfully updated.' }
format.json { render :show, status: :ok, location: #salesman }
else
format.html { render :edit }
format.json { render json: #salesman.errors, status: :unprocessable_entity }
end
end
end
# DELETE /salesmans/1
# DELETE /salesmans/1.json
def destroy
#salesman.destroy
respond_to do |format|
format.html { redirect_to salesmans_url, notice: 'Salesman was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_salesman
#salesman = Salesman.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def salesman_params
params.require(:salesman).permit(:name, :address)
end
**invoice**
class InvoicesController < ApplicationController
before_action :set_invoice, only: [:show, :edit, :update, :destroy]
# GET /invoices
# GET /invoices.json
def index
#invoices = Invoice.all
end
# GET /invoices/1
# GET /invoices/1.json
def show
#invoice = Invoice.find(params[:id])
#ordenes = #invoice.ordenes
end
# GET /invoices/new
def new
#invoice = Invoice.new
end
# GET /invoices/1/edit
def edit
end
# POST /invoices
# POST /invoices.json
def create
#invoice = Invoice.new(invoice_params)
respond_to do |format|
if #invoice.save
format.html { redirect_to #invoice, notice: 'Invoice was successfully created.' }
format.json { render :show, status: :created, location: #invoice }
else
format.html { render :new }
format.json { render json: #invoice.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /invoices/1
# PATCH/PUT /invoices/1.json
def update
respond_to do |format|
if #invoice.update(invoice_params)
format.html { redirect_to #invoice, notice: 'Invoice was successfully updated.' }
format.json { render :show, status: :ok, location: #invoice }
else
format.html { render :edit }
format.json { render json: #invoice.errors, status: :unprocessable_entity }
end
end
end
# DELETE /invoices/1
# DELETE /invoices/1.json
def destroy
#invoice.destroy
respond_to do |format|
format.html { redirect_to invoices_url, notice: 'Invoice was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_invoice
#invoice = Invoice.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def invoice_params
params.require(:invoice).permit(:date, :fid, :name, :address, :salesman_id)
end
end
-davefogo
Add the following to your views instead of what you currently have<%= customer.salesman.name if customer.salesman %>
This will ensure that you're not calling name when customer doesn't have a salesman.
Try this:
<% #customers.each do |customer| %>
<%= customer.name %>
<%= customer.address %>
<%= customer.salesman.try(:name) %>
<% end %>
Do this:
<% #customers.each do |customer| %>
<% Rails.logger.debug "\n\n#{#customers} has a nil customer in it\n\n" if customer.nil? %>
<% Rails.logger.debug "\n\nCustomer #{customer.id} has no salesman for [#{customer.salesman_id}]\n\n" if customer.salesman.nil? %>
<%= customer.name %>
<%= customer.address %>
<%= customer.salesman.name %>
<% end %>
...and then check your logs after hitting the index.