2015-06-13 62 views
0

我已經搜索了幾個小時,嘗試了所有可能的修復方法。我無法完成這項工作。錯誤是:Rails未定義的方法'each'for nil:NilClass ...但它被定義

*NoMethodError in Articles#index 
    Showing /Users/myname/blog/app/views/articles/showall.html.erb where  line #21 raised: 
undefined method `each' for nil:NilClass* 

showall.html.erb是一個視圖。它是從'文章'控制器呈現的。 (都在下面發佈)。有一條路線可以展示,而且工作正常。目前,該航線配置爲:

get 'article/showall' 

但是,我也嘗試過爲:

resources :articles do 
    get 'showall' 
resources :comments 

兩條路線的工作,但沒有對這一問題的影響。

有一個在控制器的方法,其不私人:

def showall 
    @articles = Article.all 
end 

視圖中的違規一段代碼是:

<% @articles.each do |article| %> 
<tr> 
    <td><%= article.title.truncate(30) %></td> 
    <td><%= article.author %></td> 
    <td><%= article.manufacturer %></td> 
    <td><%= article.model %></td> 
    <td><%= article.displacement %></td>` 

<% end %> 

我實際剪切和粘貼,所述代碼段從index.html.erb視圖中,它完美地工作。我已經嘗試了所有我能想到的多元化的細微差別。任何幫助將不勝感激。

控制器的這種適用部分:

class ArticlesController < ApplicationController 
skip_before_action :authorize, only: [:index, :show, :showall] 

#Filter used to catch nonlogged in users 
before_filter :require_user, :only => [:index] 

#method that checks if logged in, sends them to showall if not. 
def require_user 
unless User.find_by(id: session[:user_id]) 
    render 'showall', :notice => "Please log in to read articles." 
end 
end 

def index 

@articles = current_user.articles 

end 

#should list articles, but throws undefined method 'each' error 
def showall 
@articles = Article.all 
end 

這裏是整個視圖:

<%= render "menu" %> 

<body> 
<font color="yellow"><%= flash[:notice] %></font> 
<br> 
<font color="grey">Motorcycle Articles</font> 
<%= link_to 'Post New Article', new_article_path %> 
<br> 

<table> 
<tr> 
<th>Title</th> 

<th>Author</th> 
<th>Brand</th> 
<th>Model</th> 
<th>Displacment</th> 
<th>Last Edited On:</th> 
<th>Article</th> 
</tr> 
<% @articles.each do |article| %> 
    <tr> 
    <td><%= article.title.truncate(30) %></td> 
    <td><%= article.author %></td> 
    <td><%= article.manufacturer %></td> 
    <td><%= article.model %></td> 
    <td><%= article.displacement %></td> 

    <% end %> 
    </table> 
    <br> 
    All articles are property of their respective owners. 

</body> 
+0

你確定你已經保存在數據庫中的一些文章? –

+0

是的,我可以用index.html列出它們。erb – SeattleDucati

回答

0

要調用渲染 'SHOWALL',這使得視圖。這與調用控制器操作的'redirect_to'不同。由於您將@articles的值設置爲零值(current_user未設置),因此會出現此錯誤。

爲了闡明,在渲染視圖之前,您需要重定向到'showall'動作或重新定義@articles以等於Article.all。我個人會重定向。

+0

如果我將'render'更改爲'redirect_to',它將返回'Routing Error,uninitialized constant ArticleController',並顯示路由表。如果我將其更改爲'redirect_to article_showall_path',我會得到相同的結果。 – SeattleDucati

+0

如果我將路線更改爲'resources:articles do resources:comments get'showall' end',我得到一個不同的錯誤:沒有路線匹配[GET]「/ article/showall」。然而,在路由表中的錯誤正下方是'article_showall_path \t GET \t /articles/:article_id/showall(.:format)\t articles#showall' – SeattleDucati

+0

指定具有複數「articles」的路由,即GET「articles/showall」,到:調用之前文章#showall:資源應該糾正路由問題。從那裏你可以用redirect_to調用路徑,或者簡單地調用控制器動作(例如,redirect_to action:「showall」)就可以實現。順便提一下,如果索引中的視圖完全相同,則可以完全跳過新操作,並簡單地在那裏重定向,爲任何警報/通知添加容器。 – Ethan

1

路線是觸發指標動作,請參見:

NoMethodError in Articles#index 

你得到的錯誤,因爲current_user.articles爲零。

您需要確保Articles#showall出現在日誌中,這意味着將調用showall方法。

做,創建路線:

get '/articles', to: 'Articles#showall' 
resources :articles 

不建議這樣做。有幾個部分需要改進。但它應該使錯誤消失。

0

修改您的routes文件

的routes.rb

resources :articles do 
    collection do 
    get 'showall' 
    end 
end 
+0

這會導致路由錯誤'無路由匹配[GET]「/ article/showall」'。令人困惑,因爲耙路線顯示'article_showall_path \t GET \t /articles/:article_id/showall(.:format)\t articles#showall' – SeattleDucati

相關問題