我正在研究一個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])
生成的腳手架工作我自己的項目不會?