Not able to understand why the same command Category.find(:all , :order => 'name ASC') works in the rails console but not returning anything in the template and controller
please help
_form.html.erb
<% @all_categories.each do |cat| -%>
<li><%= check_box_tag('categories[]' , cat.id , @post.categories.collect{|c| c.id}.include?(cat.id))%>
<%= cat.name %></li>
<% end -%>
posts_controller.rb
@all_categories = Category.find(:all , :order => 'name ASC')
rails console
@all_categories = Category.find(:all , :order => 'name ASC')
Category Load (0.2ms) SELECT `categories`.* FROM `categories` ORDER BY name ASC
=> [#<Category id: 1, name: "General", short_name: "general", description: "This is a general category">]
error
undefined method `each' for nil:NilClass
posts_controller
def edit
@user_list = get_user_list
@post = Post.find(params[:id])
end
def update
post_params = params[:post]
author_id = post_params.delete(:author_id)
#@all_categories = get_all_categories
@all_categories = Category.find(:all , :order => 'name ASC')
checked_categories = get_categories_from(params[:categories])
removed_categories = @all_categories - checked_categories
@post = Post.find(params[:id])
@post.author = User.find(author_id) if @post.author_id != author_id
if @post.update_attributes(post_params)
perform_operation_on_categories
flash[:notice] = "Post was successfully updated."
redirect_to :action => 'show' , :id => @post
else
@user_list = get_user_list
render :action => 'edit'
end
end
index view
<h1><% @page_title = 'Listing posts' %></h1>
<%= content_tag('p',link_to('« Back', :controller => 'staff',:action =>'menu')) %>
<table>
<tr>
<th>Created at</th>
<th>Title</th>
<th>Author</th>
<th>Categories</th>
<th>Status</th>
<th>Comments</th>
</tr>
<% @posts.each do |post| %>
<tr class="<%= cycle('row1','row2') %>">
<td><%= post.created_at.strftime('%m/%d/%y %I:%m %p')%></td>
<td><%= h(post.title)%></td>
<td><%= h(post.author.display_name) if post.author%></td>
<td><%= post.categories.collect {|cat| cat.name}.join(", ")%></td>
<td><%= h(post.status)%></td>
<td><%= post.comments_count %></td>
<td><%= link_to('Preview',{:action =>'show',:id=>post,:target => '_blank'})%></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Delete', post, :method => :delete, :data => { :confirm => 'Are you sure you want to permanently remove this post?' } %></td>
</tr>
<% end %>
</table>
<%= will_paginate(@posts) %>
<br/>
<%= link_to 'New Post' , :action=> 'new' %>
_form.html.erb (the part where i am getting error)
<table>
<tr>
<th>Title</th>
<td><%= text_field(:post,:title,:size => 40 , :style => "font-size: 1.5em;")%></td>
<td rowspan="2">
<div class="categorylist">
<h2>Categories:</h2>
<ul>
<% @all_categories.each do |cat| -%>
<li><%= check_box_tag('categories[]' , cat.id , @post.categories.collect{|c| c.id}.include?(cat.id))%>
<%= cat.name %></li>
<% end -%>
</ul>
edit.html.erb
<% @page_title = 'Edit Post' -%>
<%= content_tag('p',link_to('« Back', :action => 'index'))%>
<%= form_tag(:action => 'update') do -%>
<%= render(:partial => 'form')%>
<%= submit_tag('Create', :style => 'margin: 1.5em 0 0 100px;')%>
<% end %>
<%= link_to('Preview Post' , {:action=>'show',:id => @post} ,:target => '_blank')%>
Just guessing from the code you posted but it seems to me that you should put the
@all_categories = Category.find(:all , :order => 'name ASC')
in the
editmethod that will be called to display your view and not only in theupdatemethod that will be called to persist your changes.The same eventually applies to
new,indexandshowmethodsHTH