Note that there are some explanatory texts on larger screens.

plurals
  1. POrails instance variable not working in the template
    text
    copied!<p>Not able to understand why the same command <code>Category.find(:all , :order =&gt; 'name ASC')</code> works in the rails console but not returning anything in the template and controller please help</p> <p><code>_form.html.erb</code></p> <pre><code>&lt;% @all_categories.each do |cat| -%&gt; &lt;li&gt;&lt;%= check_box_tag('categories[]' , cat.id , @post.categories.collect{|c| c.id}.include?(cat.id))%&gt; &lt;%= cat.name %&gt;&lt;/li&gt; &lt;% end -%&gt; </code></pre> <p><code>posts_controller.rb</code></p> <pre><code>@all_categories = Category.find(:all , :order =&gt; 'name ASC') </code></pre> <h3>rails console</h3> <pre><code>@all_categories = Category.find(:all , :order =&gt; 'name ASC') Category Load (0.2ms) SELECT `categories`.* FROM `categories` ORDER BY name ASC =&gt; [#&lt;Category id: 1, name: "General", short_name: "general", description: "This is a general category"&gt;] </code></pre> <h3>error</h3> <pre><code>undefined method `each' for nil:NilClass </code></pre> <h1>posts_controller</h1> <pre><code> 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 =&gt; '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 =&gt; 'show' , :id =&gt; @post else @user_list = get_user_list render :action =&gt; 'edit' end end </code></pre> <h1>index view</h1> <pre><code>&lt;h1&gt;&lt;% @page_title = 'Listing posts' %&gt;&lt;/h1&gt; &lt;%= content_tag('p',link_to('&amp;laquo Back', :controller =&gt; 'staff',:action =&gt;'menu')) %&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;Created at&lt;/th&gt; &lt;th&gt;Title&lt;/th&gt; &lt;th&gt;Author&lt;/th&gt; &lt;th&gt;Categories&lt;/th&gt; &lt;th&gt;Status&lt;/th&gt; &lt;th&gt;Comments&lt;/th&gt; &lt;/tr&gt; &lt;% @posts.each do |post| %&gt; &lt;tr class="&lt;%= cycle('row1','row2') %&gt;"&gt; &lt;td&gt;&lt;%= post.created_at.strftime('%m/%d/%y %I:%m %p')%&gt;&lt;/td&gt; &lt;td&gt;&lt;%= h(post.title)%&gt;&lt;/td&gt; &lt;td&gt;&lt;%= h(post.author.display_name) if post.author%&gt;&lt;/td&gt; &lt;td&gt;&lt;%= post.categories.collect {|cat| cat.name}.join(", ")%&gt;&lt;/td&gt; &lt;td&gt;&lt;%= h(post.status)%&gt;&lt;/td&gt; &lt;td&gt;&lt;%= post.comments_count %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to('Preview',{:action =&gt;'show',:id=&gt;post,:target =&gt; '_blank'})%&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Edit', edit_post_path(post) %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Delete', post, :method =&gt; :delete, :data =&gt; { :confirm =&gt; 'Are you sure you want to permanently remove this post?' } %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;/table&gt; &lt;%= will_paginate(@posts) %&gt; &lt;br/&gt; &lt;%= link_to 'New Post' , :action=&gt; 'new' %&gt; </code></pre> <h1>_form.html.erb (the part where i am getting error)</h1> <pre><code>&lt;table&gt; &lt;tr&gt; &lt;th&gt;Title&lt;/th&gt; &lt;td&gt;&lt;%= text_field(:post,:title,:size =&gt; 40 , :style =&gt; "font-size: 1.5em;")%&gt;&lt;/td&gt; &lt;td rowspan="2"&gt; &lt;div class="categorylist"&gt; &lt;h2&gt;Categories:&lt;/h2&gt; &lt;ul&gt; &lt;% @all_categories.each do |cat| -%&gt; &lt;li&gt;&lt;%= check_box_tag('categories[]' , cat.id , @post.categories.collect{|c| c.id}.include?(cat.id))%&gt; &lt;%= cat.name %&gt;&lt;/li&gt; &lt;% end -%&gt; &lt;/ul&gt; </code></pre> <h1>edit.html.erb</h1> <pre><code>&lt;% @page_title = 'Edit Post' -%&gt; &lt;%= content_tag('p',link_to('&amp;laquo; Back', :action =&gt; 'index'))%&gt; &lt;%= form_tag(:action =&gt; 'update') do -%&gt; &lt;%= render(:partial =&gt; 'form')%&gt; &lt;%= submit_tag('Create', :style =&gt; 'margin: 1.5em 0 0 100px;')%&gt; &lt;% end %&gt; &lt;%= link_to('Preview Post' , {:action=&gt;'show',:id =&gt; @post} ,:target =&gt; '_blank')%&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload