2014-03-25 85 views
0

我正在研究一個reddit的模型,我在表單中提交url &標題,然後數據庫應該被填充,我們應該被帶到一個顯示頁面來查看我們提交的鏈接和標題。爲什麼我必須在我的Show動作中包含@link = Link.find(params [:id])?

我的問題是爲什麼我必須將其包含在我的控制器中。如果我刪除了@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 

我比較另一項目,我爲類似的東西生成了腳手架,我只有私人方法,並且不需要f或@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

你錯過控制器類生成的支架中的第一行:

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

你需要展示的動作設定的instance variable @link值作爲

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

因爲show觀的link,你會像

<%= @link.title%> 
顯示特定鏈接對象(@link)的細節

爲了做到這一點,@link變量應該被設置爲Link對象,否則你會得到一個錯誤。

由於控制器中已定義了set_link方法,因此您可以在使用before_action的各種操作中重複使用該方法。

class LinksController < ApplicationController 

    before_action :set_link, only: [:show, :edit, :update, :destroy] 
    .... 
    .... 
end 

並從show, edit, update and destroy動作(如果有的話)@link = Link.find(params[:id])的調用。

使用only選項設置before_action將導致set_link方法在列出的操作執行之前被調用。

對於例如:如果你訪問顯示的URL鏈接的話,首先set_link將被調用,並且表演動作後(如表演動作是在:只列出)

注意,同樣的事情(before_action :set_high_score)設置爲HighScoresController,這就是爲什麼要避免對@highscore = HighScore.find(params[:id]的冗餘呼叫。

相關問題