1

我对 Rails 很陌生,我有一个基于论坛怪物的简单论坛应用程序。我目前正在将它从 rails 3.0.9 更新到 3.2.11。在解决了这个自我造成的问题后,我遇到了另一个问题。

似乎当我尝试显示一个主题时,用户(来自 post.user)对象不应该是 nil。

这是错误

Extracted source (around line #17):
14:       <% @topic.posts.each do |post| %>
15:       <tr>
16:         <td class="post_author" rowspan="2">
17:           <span class="name"><%= post.user.username %></span>
18:           <span class="avatar"><%= image_tag post.user.gravatar_url %></span>
19:           <span class="info smaller">
20:             <p><strong><%= "Administrator" if post.user.admin? %></strong></p>

这是我的观点 app/views/topics/show.html.erb

<div class="right controls"><%= link_to "Back to Forum", forum_path(@topic.forum) %></div>
<div class="module">
  <div class="module_header">
    <%= @topic.title %>
    <span class="right controls">
      <%= link_to "Edit", edit_topic_path(@topic) if can? :manage, @topic %>
      <%= link_to "Delete", @topic, :confirm => "Are you sure?", :method => :delete if can? :manage, @topic %>
      <%= link_to @topic.sticky ? "Unstick" : "Sticky", {:controller => 'topics', :action => 'update', :topic => {:sticky => @topic.sticky ? "false" : "true" }}, :method => :put if can? :moderate, @topic %>
      <%= link_to @topic.locked ? "Unlock" : "Lock", {:controller => 'topics', :action => 'update', :topic => {:locked => @topic.locked ? "false" : "true" }}, :method => :put if can? :moderate, @topic %>
    </span>
  </div>
  <div>
    <table>
      <% @topic.posts.each do |post| %>
      <tr>
        <td class="post_author" rowspan="2">
          <span class="name"><%= post.user.username %></span>
          <span class="avatar"><%= image_tag post.user.gravatar_url %></span>
          <span class="info smaller">
            <p><strong><%= "Administrator" if post.user.admin? %></strong></p>
            Posts <%= post.user.posts.size %><br />
            Registered <%=l post.user.created_at %><br />
          </span>
        </td>
        <td class="post_header">
          <span class="left post_date smaller">Posted <%=l post.created_at %></span>
          <span class="right controls">
            <%= link_to "Reply", new_topic_post_path(@topic) if can? :create, @topic.posts.new %>
            <%= link_to "Quote", new_topic_post_path(@topic, :quote => post) if can? :create, @topic.posts.new %>
            <%= link_to "Edit", edit_post_path(post) if can? :update, post %>
            <%= link_to "Delete", post, :confirm => "Are you sure?", :method => :delete if can? :destroy, post %>
          </span>
        </td>
      </tr>
      <tr>
        <td class="post_body">
          <%= post.body.bbcode_to_html().html_safe %>
        </td>
      </tr>
      <% end %>
    </table>
  </div>
</div>
<div class="right controls"><p><%= link_to "Back to Forum", forum_path(@topic.forum) %></p></div>

这是控制器 app/controlers/topics_controller.rb

class TopicsController < ApplicationController
  load_and_authorize_resource :forum
  load_and_authorize_resource :topic, :through => :forum, :shallow => true

  def show
    @topic.hit! if @topic
  end

  def create
    @topic.user ||= current_user

    if @topic.save
      flash[:notice] = "Topic was successfully created."
      redirect_to topic_url(@topic)
    else
      render :action => 'new'
    end
  end

  def update
    if @topic.update_attributes(params[:topic])
      flash[:notice] = "Topic was updated successfully."
      redirect_to topic_url(@topic)
    end
  end

  def destroy
    if @topic.destroy
      flash[:notice] = "Topic was deleted successfully."
      redirect_to forum_url(@topic.forum)
    end
  end

end

Rails 控制台输出确认视图中的代码应该可以工作,并且帖子确实有用户。

ricky ~/forum $ rails c
Loading development environment (Rails 3.2.11)
irb(main):001:0> Topic.find(1).posts.each do |p|
irb(main):002:1* puts p.user.username
irb(main):003:1> end
  Topic Load (0.5ms)  SELECT `topics`.* FROM `topics` WHERE `topics`.`id` = 1 ORDER BY sticky DESC, updated_at DESC LIMIT 1
  Post Load (0.4ms)  SELECT `posts`.* FROM `posts` WHERE `posts`.`topic_id` = 1 ORDER BY created_at ASC
  User Load (0.5ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
admin
=> [#<Post id: 1, body: "Forum Monster is a simple forum generator written i...", forum_id: 1, topic_id: 1, user_id: 1, created_at: "2013-01-19 06:57:23", updated_at: "2013-01-19 06:57:23">]
irb(main):004:0>

我想我可能需要为 cancan can :read User 添加一个功能,但我不太确定。

在此先感谢您帮助这个困惑的 android 开发人员 0.o

编辑

在这里查看完整的源代码

正如您在下面看到的那样,post 模型确实验证了用户的存在

应用程序/模型/post.rb

class Post < ActiveRecord::Base

  # Associations
  belongs_to :forum, :counter_cache => true
  belongs_to :topic, :counter_cache => true, :touch => true 
  belongs_to :user, :class_name => "User", :counter_cache => true

  # Accessors
  attr_accessible :body

  # Validations
  validates :body, :presence => true
  validates :user, :presence => true

  # Default Scope
  default_scope :order => 'created_at ASC'

  # Scope to display only the last n posts. Used for "Recent Posts" display
  scope :recent, lambda {
    |c| reorder('created_at desc').limit(c)
  }

  # Callbacks
  before_save :topic_locked?

  # Methods
  private
    def topic_locked?
      if topic.locked?
        errors.add(:base, "That topic is locked")
        false
      end
    end
end

是什么让我知道在已播种的数据库中仅存在 1 个帖子,并且它是通过

@current_user = User.find_by_username("admin")
@current_user.topics.create!(...)
4

4 回答 4

2

用。。。来代替:

<%= post.user.username unless post.user.blank? %>
于 2013-01-19T08:21:21.697 回答
2

我真的不明白为什么这解决了问题,因为数据库中只有一篇文章;但我找到了解决方案。

<% @topic.posts.each do |post| %>
  <% if !post.user.blank? %>
    ...display post here...
  <%end%>
<%end%>
于 2013-01-20T16:01:40.277 回答
0

在您看来,该集合@topic.posts至少包含一个没有用户的帖子,并且您视图中的其余代码没有预料到这一点。

  • 可能您的应用程序不应允许在没有用户的情况下创建帖子,您应该添加验证以检查保存帖子时用户是否存在,并防止在没有用户的情况下创建帖子。

    validates :user_id, :presence => true
    
  • 如果您的应用程序应该允许没有用户的帖子,if post.user.present?请在您的视图中添加:

    <% @topic.posts.each do |post| %>
      <tr>
        <td class="post_author" rowspan="2">
          <% if post.user.present? %>
            <span class="name"><%= post.user.username %></span>
            <span class="avatar"><%= image_tag post.user.gravatar_url %></span>
            <span class="info smaller">
              <p><strong><%= "Administrator" if post.user.admin? %></strong></p>
              Posts <%= post.user.posts.size %><br />
              Registered <%=l post.user.created_at %><br />
            </span>
          <% end %>
        </td>
        ... 
    
于 2013-01-19T08:48:24.197 回答
0

根据您是否希望帖子用户始终为空白或根本不为空白,您可以采取两种方法。

1)如果帖子用户可以为空:

检查 post 用户是否存在,这样您将不会获得“nil”用户。

'@userpost = post.user.blank?" ": post.user.username'

2)如果post用户不能为空:

在这种情况下,您可以像在 post 模型中那样强加关联,“belongs_to :user”和在用户模型中,“has_many :posts”和像“validates_presence_of :post”这样的约束。

于 2013-01-19T10:12:41.717 回答