为什么我必须在我的Show动作中包含@ link = Link.find(params [:id])?

时间:2014-03-25 02:51:53

标签: ruby-on-rails controller scaffolding

我正在制作reddit的模型,我提交了url&表单中的标题然后数据库应该填充,我们应该进入显示页面以查看我们提交的链接和标题。

我的问题是为什么我必须将其包含在我的控制器中。

  def show  
    @link = Link.find(params[:id])
  end

如果我删除@link = Link.find(params[:id])我的节目无效。

我收到以下错误:

NoMethodError in Links#show

在这一行

<%= @link.title %>

除此之外,我已经有了这个私人方法:

private
  def set_link
    @link = Link.find(params[:id])
  end
  def link_params
    params.require(:link).permit(:title, :url)
  end

我正在比较另一个项目,我在那里为类似的东西生成了一个脚手架,我只有私有方法,在我的show动作中不需要@link = Link.find(params[:id])

这是我的完整控制器代码:

class LinksController < ApplicationController

  def index
    @link = Link.all
  end

  def new
    @link = Link.new
  end

  def create
    @link = Link.new(link_params)

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

  def show  
    @link = Link.find(params[:id])
  end


private
  def set_link
    @link = Link.find(params[:id])
  end
  def link_params
    params.require(:link).permit(:title, :url)
  end


end

这是我生成的脚手架的完整控制器:

class HighScoresController < ApplicationController
  before_action :set_high_score, only: [:show, :edit, :update, :destroy]

  # GET /high_scores
  # GET /high_scores.json
  def index
    @high_scores = HighScore.all
  end

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

  # GET /high_scores/new
  def new
    @high_score = HighScore.new
  end

  # GET /high_scores/1/edit
  def edit
  end

  # POST /high_scores
  # POST /high_scores.json
  def create
    @high_score = HighScore.new(high_score_params)

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

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

  # DELETE /high_scores/1
  # DELETE /high_scores/1.json
  def destroy
    @high_score.destroy
    respond_to do |format|
      format.html { redirect_to high_scores_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def high_score_params
      params.require(:high_score).permit(:game, :score)
    end
end

为什么生成的脚手架在我的展示动作中没有@link = Link.find(params[:id])的情况下工作,而我自己的项目却没有?

2 个答案:

答案 0 :(得分:2)

您错过了生成的脚手架的控制器类中的第一行:

class HighScoresController < ApplicationController
  before_action :set_high_score, only: [:show, :edit, :update, :destroy]

您需要向控制器添加before_action才能运行set_link功能:

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

show操作之前,请调用set_link功能。

答案 1 :(得分:1)

您需要将instance variable @link的值设置为

@link = Link.find(params[:id])

在show动作中,因为在show的{​​{1}}视图中,您将显示特定链接对象(@link)的详细信息,如

link

为了做到这一点,应将@link变量设置为<%= @link.title%> 对象,否则会出错。

由于您已在控制器中定义了Link方法,因此可以使用set_link在各种操作中重复使用该方法。

before_action

并从class LinksController < ApplicationController before_action :set_link, only: [:show, :edit, :update, :destroy] .... .... end 操作(如果有)中删除对@link = Link.find(params[:id])的调用。

使用show, edit, update and destroy选项设置before_action将导致在执行列出的操作之前调用only方法。

例如:如果您访问show url获取链接,那么首先调用set_link并在该show动作之后(因为show action位于:only列表中)

请注意,为set_link设置了相同的内容(before_action :set_high_score),这就是为什么可以避免对HighScoresController进行冗余调用的原因。