Rails undefined method `each' for nil:NilClass...but it is defined -


i have searched hours, tried every possible fix. cannot make work. error is:

  *nomethoderror in articles#index   showing /users/myname/blog/app/views/articles/showall.html.erb     line #21 raised: undefined method `each' nil:nilclass* 

showall.html.erb view. rendered 'article' controller. (both posted below). there route showall, , works fine. route configured as:

get 'article/showall' 

but, have tried as:

resources :articles   'showall' resources :comments 

both routes worked, neither had effect on issue.

there method in controller, not private:

def showall    @articles = article.all end 

the offending piece of code in view is:

 <% @articles.each |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 %> 

i cut , pasted, piece of code index.html.erb view, works perfectly. have tried every nuance of pluralization can think of. appreciated.

this applicable parts of controller:

class articlescontroller < applicationcontroller  skip_before_action :authorize, only: [:index, :show, :showall]   #filter used catch nonlogged in users   before_filter :require_user, :only => [:index]  #method checks if logged in, sends them showall if not. def require_user unless user.find_by(id: session[:user_id])   render 'showall', :notice => "please log in read articles." end end  def index  @articles = current_user.articles  end  #should list articles, throws undefined method 'each' error def showall @articles = article.all end 

here entire view:

 <%= 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 |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>    articles property of respective owners.   </body> 

you calling render 'showall', renders view. different 'redirect_to', calls controller action. since setting value of @articles nil value (current_user not set), error.

to clarify, need either redirect_to 'showall' action or redefine @articles equal article.all before rendering view. i'd redirect.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -