爲您的上下文」的零NilClass,無法與發現的決定「:這是創建一個應用程序我第一次嘗試。我剛開始編碼:-)。初學者@Rails:未定義的方法`標題:ID =編輯
我想要一個簡單的CRUD設置工作。
現在我有兩個問題,我不能左右我的頭:
我的條目不我的索引頁面上顯示出來。它給出了以下錯誤:'未定義的方法'標題'爲零:NilClass'。該模型包含以下列: 字符串:標題,文本:預測,日期:review_date
如果我去決定/編輯它會給我以下錯誤:'找不到決定與'id'=編輯'
這是我的代碼:
控制器:
class DecisionsController < ApplicationController
before_action :find_decision, only: [:show, :edit, :update]
def index
# gets all rows from decision table and puts it in @decision variable
@decisions = Decision.all
end
def show
# find only the decision entry that has the id defined in params[:id]
@decision = Decision.find(params["id"])
end
# shows the form for creating a entry
def new
@decision = Decision.new
end
# creates the entry
def create
@decision = Decision.new(decision_params)
if @decision.save
redirect_to @decision
else
render 'new'
end
end
# shows the form for editing a entry
def edit
@decision = Decision.find(params["id"])
end
# updates the entry
def update
end
def destroy
end
private
def find_decision
@decision = Decision.find(params["id"])
end
def decision_params
params.require(:decision).permit(:title, :forecast, :review_date)
end
end
索引視圖
<h1>Hello World ^^</h1>
<% @decisions.each do |descision| %>
<p><%= @decision.title %></p>
<% end %>
的routes.rb
Rails.application.routes.draw do
resources :decisions
root 'decisions#index'
end
我已經整個上午一直在這兩個,但我不能弄明白。如果你們可以看看我,我會很有幫助的。
'決定/編輯'匹配'決定/ ID',所以它去的表演動作。網址應該是「決定/ some_id /編輯」 – apneadiving
替換<%@ decisions.each do | descision | %>'用'<%@ decisions.each do | decision | %>'和'
<%= @ decision.title%>
' 與'<%= decision.title%>
' – apneadivingFacepalm .. Thx @apneadiving。這兩個問題都解決了。 我不明白爲什麼我不應該使用@符號作爲
<%= decision.title%>
部分。在節目視圖中,似乎需要。你能解釋一下我的不同嗎? – Hiltsje