0

Category.find(:all , :order => 'name ASC') Railsコンソールで同じコマンドが機能するのに、テンプレートとコントローラーで何も返さない理由を理解できません助けてください

_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')  

レールコンソール

@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">] 

エラー

undefined method `each' for nil:NilClass

投稿コントローラー

  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

インデックス ビュー

<h1><% @page_title = 'Listing posts' %></h1>
<%= content_tag('p',link_to('&laquo 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 (エラーが発生している部分)

<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('&laquo; 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')%>
4

1 に答える 1

0

投稿したコードから推測するだけですが、

@all_categories = Category.find(:all , :order => 'name ASC')

変更を保持するために呼び出されるメソッドeditだけでなく、ビューを表示するために呼び出されるupdateメソッドでも。

newindexおよびshowメソッドにも最終的に同じことが適用されます

HTH

于 2012-07-18T12:19:29.723 に答える