视图,控制器和变量

时间:2013-12-08 04:48:19

标签: ruby-on-rails ruby-on-rails-3 variables view

我编写了下面的代码并且它有效。但我知道这对于视图文件来说太多了,而且大部分代码都应该用控制器编写。 然后,我尝试在控制器上重写它,但收到错误信息

"undefined method `group' for nil:NilClass". 
你可以给我一些建议吗?

【重写前】

☆show.html.erb(#members)

  <div class="message_area">
                <% posts_in_groups = GroupMessage.where(:member_id => params[:id]).order("created_at desc").paginate(:page => params[:page], :per_page => 3) %>
                <div class="each_message"> 
                <% posts_in_groups.each do |post_in_group|%>
                    <a href="/groups/<%= post_in_group.group.id %>" ><%= image_tag post_in_group.group.imageurl, :width => '20', :height => '25' %><%= "(" + post_in_group.group.name + ")" %></a>
                    <%= 'Page:' + post_in_group.page.to_s + '&' %><%= 'Line:' + post_in_group.line.to_s %>
                    (<%= post_in_group.created_at.strftime'%Y-%m-%d %H:%M' %>)
                  <div class="group_message">
                    <p class="message_content"><a href="/group_messages/<%= post_in_group.id%>" ><%= truncate(post_in_group.content, { :length => 50} ) %></a></p>
                  </div><!--group_message-->
                  <br>
                    <% end %>
                     <%= will_paginate(posts_in_groups) %> 
                </div><!--each message-- >   

            </div><!--message area-->

【改写后】

☆show.html.erb(#members)

<div class="message_area">
              <div class="each_message">
                    <a href="/groups/<%= @post_in_group.group.id %>" ><%= image_tag @post_in_group.group.imageurl, :width => '20', :height => '25' %><%= "(" + @post_in_group.group.name + ")" %></a>
                    <%= 'Page:' + @post_in_group.page.to_s + '&' %><%= 'Line:' + @post_in_group.line.to_s %>
                    (<%= @post_in_group.created_at.strftime'%Y-%m-%d %H:%M' %>)
                  <div class="group_message">
                    <p class="message_content"><a href="/group_messages/<%= @post_in_group.id%>" ><%= truncate(@post_in_group.content, { :length => 50} ) %></a></p>
                  </div><!--group_message-->
                  <br>
                     <%= will_paginate(@posts_in_groups) %> 
                </div><!--each message-- >   
            </div><!--message area-->

☆members_controller

@posts_in_groups = GroupMessage.where(:member_id => params[:id]).order("created_at desc").paginate(:page => params[:page], :per_page => 3) 
@posts_in_groups.each do |post_in_group| 
@post_in_group = post_in_group
end

☆GroupMessage模型

class GroupMessage < ActiveRecord::Base
  attr_accessible :content, :member_id, :group_id, :page, :line

  belongs_to :member
  belongs_to :group
  has_many :group_message_comments, :dependent => :destroy

end

1 个答案:

答案 0 :(得分:1)

首先,从控制器中删除它:

@posts_in_groups.each do |post_in_group| 
  @post_in_group = post_in_group
end

它不属于那里,即使它已经存在,对于集合中的每个项目,您都会将其设置为@post_in_group实例变量,并且这不是您想要的,只会设置最后一个值。

以下是您的视图代码的样子:

<div class="message_area">
  <% @posts_in_groups.each do |post| %>  
    <div class="each_message">
      <%= link_to post.group do %>
        <%= image_tag post.group.imageurl, :width => '20', :height => '25' %>
        <%= "(" + post.group.name + ")" %>
      <% end %>

      Page: <%= post.page %> & 
      Line: <%= post.line %>
      (<%= post.created_at.strftime'%Y-%m-%d %H:%M' %>)

      <div class="group_message">
        <p class="message_content">
          <%= link_to truncate(post.content, :length => 50), post %>
        </p>
      </div><!--group_message-->

      <br>      
    </div><!--each message-- >   

    <%= will_paginate(@posts_in_groups) %> 
  <% end %>  
</div><!--message area-->

您真正必须从视图中删除的唯一内容是数据库查询,其他部分可以在那里。