关联未识别,未定义方法错误

时间:2014-06-05 07:42:06

标签: ruby-on-rails methods associations one-to-one

我创建了2个脚手架'mepager'和'pimp'并将这些模型链接起来:

class Mepager < ActiveRecord::Base
    belongs_to :pimp
end

class Pimp < ActiveRecord::Base
    has_one :mepager
end

我在路由中添加了以下行:

resources :pimps do  
    resources :mepagers
end

我的mepager创建动作如下所示:

def create
    @mepager = @pimp.build_mepager(mepager_params)

    respond_to do |format|
      if @mepager.save
        format.html { redirect_to @mepager, notice: 'Mepager was successfully created.' }
        format.json { render action: 'show', status: :created, location: @mepager }
      else
        format.html { render action: 'new' }
        format.json { render json: @mepager.errors, status: :unprocessable_entity }
      end
    end
end

并为@pimp设置

def setPimp
    @pimp = Pimp.find_by_id(:pimp_id)
end 

但是如果我尝试在pimps / 1 / mepagers / new上创建一个新的mepager,我会得到一个未定义的方法build_mepager错误。

undefined method `build_mepager' for nil:NilClass

我在rails控制台中尝试了相同的操作,它只使用了该方法。所以我想有些东西仍然没有让它在我的网络应用程序上运行。

顺便说一句,我使用的是rails 4.0.0

问候!

路由

Prefix Verb   URI Pattern                                 Controller#Action
pimps GET    /pimps(.:format)                            pimps#index
POST   /pimps(.:format)                            pimps#create
new_pimp GET    /pimps/new(.:format)                        pimps#new
edit_pimp GET    /pimps/:id/edit(.:format)                   pimps#edit
pimp GET    /pimps/:id(.:format)                        pimps#show
PATCH  /pimps/:id(.:format)                        pimps#update
PUT    /pimps/:id(.:format)                        pimps#update
DELETE /pimps/:id(.:format)                        pimps#destroy
root GET    /                                           pimps#index
pimp_mepagers GET    /pimps/:pimp_id/mepagers(.:format)          mepagers#index
POST   /pimps/:pimp_id/mepagers(.:format)          mepagers#create
new_pimp_mepager GET    /pimps/:pimp_id/mepagers/new(.:format)      mepagers#new
edit_pimp_mepager GET    /pimps/:pimp_id/mepagers/:id/edit(.:format) mepagers#edit
pimp_mepager GET    /pimps/:pimp_id/mepagers/:id(.:format)      mepagers#show
PATCH  /pimps/:pimp_id/mepagers/:id(.:format)      mepagers#update
PUT    /pimps/:pimp_id/mepagers/:id(.:format)      mepagers#update
DELETE /pimps/:pimp_id/mepagers/:id(.:format)      mepagers#destroy
GET    /pimps(.:format)                            pimps#index
POST   /pimps(.:format)                            pimps#create
GET    /pimps/new(.:format)                        pimps#new
GET    /pimps/:id/edit(.:format)                   pimps#edit
GET    /pimps/:id(.:format)                        pimps#show
PATCH  /pimps/:id(.:format)                        pimps#update
PUT    /pimps/:id(.:format)                        pimps#update
DELETE /pimps/:id(.:format)                        pimps#destroy

皮条客控制器中的改变线

def create
    @pimp = Pimp.new(pimp_params)
      respond_to do |format|
      if @pimp.save
               format.html { redirect_to new_pimp_mepager_path, notice: 'Product Improvement was successfully created.' }
            format.json { render action: 'show', status: :created, location: @pimp }
      else
            format.html { render action: 'new' }
            format.json { render json: @pimp.errors, status: :unprocessable_entity }
      end
    end
end

mepager controller

class MepagersController < ApplicationController
  before_action :set_mepager, only: [:show, :edit, :update, :destroy]

  def setPimp
    @pimp = Pimp.find(params[:pimp_id])
  end

  # GET /mepagers
  # GET /mepagers.json
  def index
    @mepagers = Mepager.all
  end

  # GET /mepagers/1
  # GET /mepagers/1.json
  def show
  end

  # GET /mepagers/new
  def new
    @mepager = Mepager.new
  end

  # GET /mepagers/1/edit
  def edit
  end

  # POST /mepagers
  # POST /mepagers.json
  def create
    raise params[:pimp_id].inspect
    @mepager = @pimp.build_mepager(mepager_params)

    respond_to do |format|
      if @mepager.save
        format.html { redirect_to @mepager, notice: 'Mepager was successfully created.' }
        format.json { render action: 'show', status: :created, location: @mepager }
      else
        format.html { render action: 'new' }
        format.json { render json: @mepager.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /mepagers/1
  # PATCH/PUT /mepagers/1.json
  def update
    respond_to do |format|
      if @mepager.update(mepager_params)
        format.html { redirect_to @mepager, notice: 'Mepager was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @mepager.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /mepagers/1
  # DELETE /mepagers/1.json
  def destroy
    @mepager.destroy
    respond_to do |format|
      format.html { redirect_to mepagers_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_mepager
      @mepager = Mepager.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def mepager_params
      params.require(:mepager).permit(:pre, :post, :comment, :save_h, :save_c, :save_other, :save_otherv, :affect_design, :affect_stress, :affect_me, :affect_other, :affect_dno, :affect_mid, :affect_otherdoc, :owner, :pps, :reference)
    end
end

1 个答案:

答案 0 :(得分:4)

问题可能在这里:

@pimp = Pimp.find_by_id(:pimp_id)

你正在做的是找到一个具有文字符号:pimp_id的id的pimp(这种情况极不可能存在)

您可能需要的是参数:params[:pimp_id]

编辑:

和第二部分。

create操作使用真实的@pimp:pimp_id可以找到),必须在{ {1}}行动也是! 否则创建将无效。

我可以推荐像......

new