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

post_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

索引视图

<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>

编辑.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将被调用以保留您的更改的方法中。

这同样最终适用于new,indexshow方法

高温高压

于 2012-07-18T12:19:29.723 回答