2015-10-20 35 views
2

爲您的上下文」的零NilClass,無法與發現的決定「:這是創建一個應用程序我第一次嘗試。我剛開始編碼:-)。初學者@Rails:未定義的方法`標題:ID =編輯

我想要一個簡單的CRUD設置工作。

現在我有兩個問題,我不能左右我的頭:

  1. 我的條目不我的索引頁面上顯示出來。它給出了以下錯誤:'未定義的方法'標題'爲零:NilClass'。該模型包含以下列: 字符串:標題,文本:預測,日期:review_date

  2. 如果我去決定/編輯它會給我以下錯誤:'找不到決定與'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 

我已經整個上午一直在這兩個,但我不能弄明白。如果你們可以看看我,我會很有幫助的。

+1

'決定/編輯'匹配'決定/ ID',所以它去的表演動作。網址應該是「決定/ some_id /編輯」 – apneadiving

+1

替換<%@ decisions.each do | descision | %>'用'<%@ decisions.each do | decision | %>'和'

<%= @ decision.title%>

' 與'

<%= decision.title%>

' – apneadiving

+0

Facepalm .. Thx @apneadiving。這兩個問題都解決了。 我不明白爲什麼我不應該使用@符號作爲

<%= decision.title%>

部分。在節目視圖中,似乎需要。你能解釋一下我的不同嗎? – Hiltsje

回答

2

I have just started coding

歡迎!


My entries don't show up on my index page.

我敢肯定,你的意思是decisions,對不對?

如果是這樣,你要記住,如果你在Ruby中調用循環,你需要一些條件邏輯,以確定它是否有任何數據在試圖調用之前實際上填充:

#app/views/decisions/index.html.erb 
<% if @decisions.any? %> 
    <% @decisions.each do |decision| %> 
     <%= content_tag :p, decision.title %> 
    <% end %> 
<% end %> 

這必須由相應的控制器代碼相匹配:

#app/controllers/decisions_controller.rb 
class DecisionsController < ApplicationController 
    before_action :find_decision, only: [:show, :edit, :update, :destroy] 

    def index 
     @decisions = Decision.all 
    end 

    def show 
    end 

    def new 
     @decision = Decision.new 
    end 

    def create 
     @decision = Decision.new decision_params 
     @decision.save ? redirect_to(@decision) : render('new') 
    end 

    def edit 
    end 

    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 

這會給你打電話給你的意見@decisions@decision這取決於糅的能力你正在訪問。

重要的一點是,當你說...

decisions/edit it gives me the following error: Couldn't find Decision with 'id'=edit'

...的問題是由在其中Rails routing的處理方式引起的:

enter image description here

由於Ruby/Rails是object orientated,每一組的路由對應於任一個集合的對象,或一個構件對象。這就是爲什麼諸如edit這樣的路線需要傳遞一個「id」 - 它們被設計用於成員對象

因此,當您訪問任何「成員」路線(decisions/:iddecisions/:id/edit),你必須提供一個id讓Rails可以從數據庫中拉相應的記錄:

#app/views/decisions/index.html.erb 
<% if @decisions.any? %> 
    <% @decisions.each do |descision| %> 
     <%= link_to "Edit #{decision.title}", decision_edit_path(decision) %> 
    <% end %> 
<% end %> 

我可以解釋更多 - 上面應該現在爲你工作。

+1

中創建一個局部變量很好的答案,Rich。當我開始學習編寫像你這樣的答案時,激勵我繼續下去。 – Mohamad

+1

很好的答案!但是你關心......每個循環的實例是「| descision |」而不是「|決定|」 ! – rfellons

+0

謝謝Mohamad,很高興聽到!希望你做得很好! –