Ruby on rails 链路控制器中的命名错误#新设计

Ruby on rails 链路控制器中的命名错误#新设计,ruby-on-rails,ruby,devise,controller,Ruby On Rails,Ruby,Devise,Controller,在我的应用程序中,当我尝试将current_userdesign helper添加到控制器中的new操作时,出现以下错误: undefined method `val' for #<Arel::Nodes::BindParam:0x007fe650498200> 控制器: class LinksController < ApplicationController before_action :set_link, only: [:show, :edit, :update,

在我的应用程序中,当我尝试将
current_user
design helper添加到控制器中的
new
操作时,出现以下错误:

undefined method `val' for #<Arel::Nodes::BindParam:0x007fe650498200> 
控制器:

class LinksController < ApplicationController
  before_action :set_link, only: [:show, :edit, :update, :destroy]

  # GET /links
  # GET /links.json
  def index
    @links = Link.all
  end

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

  # GET /links/new
  def new
    @link = current_user.links.build
  end

  # GET /links/1/edit
  def edit
  end

  # POST /links
  # POST /links.json
  def create
    @link = current_user.links.build(link_params)

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

  # PATCH/PUT /links/1
  # PATCH/PUT /links/1.json
  def update
    respond_to do |format|
      if @link.update(link_params)
        format.html { redirect_to @link, notice: 'Link was successfully updated.' }
        format.json { render :show, status: :ok, location: @link }
      else
        format.html { render :edit }
        format.json { render json: @link.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /links/1
  # DELETE /links/1.json
  def destroy
    @link.destroy
    respond_to do |format|
      format.html { redirect_to links_url, notice: 'Link was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def link_params
      params.require(:link).permit(:title, :url)
    end
end
类链接控制器
您似乎忘记将
外键
用户id
添加到
链接
表中

创建迁移以将其添加到此链接:

$ rails g migration add_user_id_to_links
然后在您的迁移中:

class AddUserIdToLinks < ActiveRecord::Migration
  def change
    add_column :links, :user_id, :integer, null: false
    add_index :links, :user_id
  end
end
class AddUserIdToLinks

错误消息不是很清楚或详细,但正如您所看到的,最新版本的Rails已经纠正了这一点。

您使用的是哪个Rails版本?尝试升级它。如果没有帮助,请在用户模型中添加一部分,其中定义了与链接的关联。
在\u操作之前:验证\u用户!,只:[:新建,:创建]
您错过了用户模型=>
中有许多:链接,rails更新(4.2.0)@RajarshiDas我在操作之前添加了这个,但仍然是相同的错误:(Ping!接受有效答案在这里很有礼貌;)回答得好@dgilperez。就在现场。谢谢,我在使用
null:false时遇到了一些麻烦。它生成一个SQL错误,我用
nil:false
替换它,因为我们处理的是整数
class AddUserIdToLinks < ActiveRecord::Migration
  def change
    add_column :links, :user_id, :integer, null: false
    add_index :links, :user_id
  end
end