Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails关联错误_Ruby On Rails_Ruby_Activerecord_Associations_Model Associations - Fatal编程技术网

Ruby on rails Rails关联错误

Ruby on rails Rails关联错误,ruby-on-rails,ruby,activerecord,associations,model-associations,Ruby On Rails,Ruby,Activerecord,Associations,Model Associations,我在SitesController#索引未定义的方法“name”中遇到了NoMethodError,在我的Salesor index视图中为nil:NilClass找到了罪魁祸首 我有一个简单的rails应用程序,包含以下表格:客户、销售人员和发票 在customer表的索引视图中,我有以下调用: 这是导致上面列出的未定义方法“name”的调用。我确保saller表在customer表中有saller\u id外键。此外,我在模型中分别进行了关联: class客户

我在SitesController#索引未定义的方法“name”中遇到了NoMethodError,在我的Salesor index视图中为nil:NilClass找到了罪魁祸首

我有一个简单的rails应用程序,包含以下表格:客户、销售人员和发票

在customer表的索引视图中,我有以下调用:


这是导致上面列出的未定义方法“name”的调用。我确保saller表在customer表中有saller\u id外键。此外,我在模型中分别进行了关联:

class客户

类销售人员

我在控制台中测试了
customer.saller.name
调用,它工作正常。客户的show view有一个与此类似的精确调用,而且它也可以正常工作。我可以通过客户索引的唯一方法是将
customer.saller.name
替换为
customer.saller\u id
只返回id

谢谢你抽出时间来这里

***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
***schema.rb**
ActiveRecord::Schema.define(版本:2015032517212)do
创建表“客户”,强制::级联do | t|
t、 字符串“name”
t、 字符串“地址”
t、 datetime“created_at”,null:false
t、 datetime“更新时间”,null:false
t、 整数“推销员id”
结束
创建表格“发票”,强制::级联do | t|
t、 日期时间“日期”
t、 字符串“fid”
t、 整数“推销员id”
t、 datetime“created_at”,null:false
t、 datetime“更新时间”,null:false
t、 字符串“地址”
结束
创建表“销售人员”,force::cascade do | t|
t、 日期时间“名称”
t、 字符串“地址”
t、 datetime“created_at”,null:false
t、 datetime“更新时间”,null:false
结束
***控制器***
**顾客**
类CustomersController<% @customers.each do |customer| %> 
  <%= customer.name %> 
  <%= customer.address %> 
  <%= customer.salesman.try(:name) %> 
<% end %>
<% @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 %>