2014-06-19 36 views
0

幾天前我問了類似的問題,但我仍然不知道該怎麼做。我所做的是我在我的博客上的帖子下創建了評論部分,所有評論都始終可見,並且我希望我的應用程序在新評論未通過驗證時(當出現錯誤時)也顯示所有評論以及我也想在所有數據庫記錄下面看到這個錯誤。我只是沒有做到這一點。Rails 4 - 錯誤消息和同一頁面上的所有數據庫記錄

的問題是,當我不希望我的應用程序,以顯示 在同一頁上的所有評論(我暫時刪除這部分從我learn_webdeb文件),

<% @mains.each do |a| %> 
      <tr> 
       <td><%= a.title %></td> 
       <td><%= a.body %></td> 
      </tr> 
<% end %> 

的東西很好地工作,當新評論未通過驗證,顯示錯誤。但是,當我想也顯示在同一頁面上先前保存的所有數據庫記錄我得到這個錯誤:

Showing /home/mateusz/Pulpit/Aptana3_Workspace/My_webpage/app/views/mains/learn_webdeb.html.erb where line #33 raised: 

undefined method `each' for nil:NilClass 

Extracted source (around line #33): 

</thead> 
<tbody> 
<% @mains.each do |a| %> 
<tr> 
<td><%= a.title %></td> 
<td><%= a.body %></td> 

Rails.root: /home/mateusz/Pulpit/Aptana3_Workspace/My_webpage 
Application Trace | Framework Trace | Full Trace 

app/views/mains/learn_webdeb.html.erb:33:in `_app_views_mains_learn_webdeb_html_erb__2599098263892148643_20921680' 
app/controllers/mains_controller.rb:26:in `block (2 levels) in create' 
app/controllers/mains_controller.rb:22:in `create' 

這裏有我的控制器:

class MainsController < ApplicationController 
    def learn_webdeb 
    @main = Main.new 
    @mains = Main.all 
    end 

    def motivation 
    end 

    def about 
    end 

    def books_courses 
    end 

    def contact 
    end 

    def create 
    @main = Main.new(main_params) 

    respond_to do |format| 
     if @main.save 
     format.html { redirect_to learn_webdeb_path } 
     else 
     format.html { render controller: "mains", action: "learn_webdeb" } 
     end 
    end 
    end 

    def show 
    redirect_to :action => "learn_webdeb" 
    end 

    private 

    def main_params 
     params.require(:main).permit(:title, :body) 
    end 

end 

而且我learn_webdeb觀的重要組成部分文件:

... 

<table class="table"> 
    <thead> 
     <tr> 
      <th>Name:</th> 
      <th>Comment:</th> 
     </tr> 
    </thead> 
    <tbody> 

<% @mains.each do |a| %> 
      <tr> 
       <td><%= a.title %></td> 
       <td><%= a.body %></td> 
      </tr> 
<% end %> 

    </tbody> 
</table> 
<%= form_for(@main) do |main| %> 
    <div> 
     <%= main.label :title %> 
     <%= main.text_field :title %> 
    </div> 
    <div> 
     <%= main.label :body %> 
     <%= main.text_area :body %> 
    </div> 
    <div> 
     <%= main.submit "Submit" %> 
    </div> 

    <% if @main.errors.any? %> 
     <div> 
      <% @main.errors.full_messages.each do |msg| %> 
       <p><%= msg %></p> 
      <% end %> 
     </div> 
    <% end %> 
<% end %> 

誰能給我解釋一下: 1.How擺脫這種錯誤的,並顯示在同一個PA的所有數據庫記錄和驗證錯誤GE? 2.爲什麼發生這種錯誤,代碼背後發生了什麼?因爲你把這個在你的控制器

render controller: "mains", action: "learn_webdeb"

render <action>使用實例變量這是在行動可在那裏它被稱爲發生

回答

0

你的錯誤(在你的例子create動作)和@mains實例變量不可用在你的看法從這個行動(因爲它只在​​行動定義)。

要修正這個錯誤,你需要調用redirect而不是render

def create 
    @main = Main.new(main_params) 

    respond_to do |format| 
    if @main.save 
     format.html { redirect_to @main } 
    else 
     format.html { redirect_to learn_webdeb_path } 
    end 
    end 
end 

如果您需要在同一頁面上顯示驗證錯誤,你應該在你創建操作定義@mains,並呼籲render代替redirect

​​
相關問題