Rails 4:无法检索“belongs_to”两个模型的模型的特定属性?

时间:2013-12-25 21:21:24

标签: ruby-on-rails has-many belongs-to

所以我有一个使用Devise进行身份验证的Active模型(将其视为用户)。我还有一个Rushee模型(您可以将它们视为产品),并且在每个Rushee的个人资料页面上,我向Actives提供了选择Rusheepost(将其视为产品评论)。< / p>

我会先发布一些代码,然后再描述问题。

以下是我的模特:

class Rushee < ActiveRecord::Base
    has_many :rusheeposts, dependent: :destroy

class Active < ActiveRecord::Base
    has_many :rusheeposts, dependent: :destroy

class Rusheepost < ActiveRecord::Base
    belongs_to :active
    belongs_to :rushee

的routes.rb

devise_for :actives, :path_prefix => 'my'
resources :actives, only: [:index, :show]

resources :rushees, only: [:index, :show] do
    resources :rusheeposts, only: [:create, :destroy]
end

RusheepostsController

before_action :authenticate_active!

def create
    @rushee = Rushee.find(params[:rushee_id])
    @rusheepost = @rushee.rusheeposts.build(rusheepost_params)
    @rusheepost.active = current_active

    if @rusheepost.save
        flash[:success] = "Comment created!"
        redirect_to @rushee

    else
        flash[:error] = "There was an error with your comment; please try again."
        redirect_to @rushee
    end
end

private

def rusheepost_params
    params.require(:rusheepost).permit(:content)
end

RusheesController(我只想登录活动以便能够查看rushees)

class RusheesController < ApplicationController

    before_action :authenticate_active!

    def show
        @rushee = Rushee.find(params[:id])
        @rusheeposts = @rushee.rusheeposts
        @rusheepost = @rushee.rusheeposts.build if active_signed_in?
    end

    def index
        @rushees = Rushee.all
    end

end

show查看Rushees

<% provide(:title, @rushee.name) %>
<div class="row">
  <aside class="span4">
    <section>
      <h1>
        <%= @rushee.name %>
        <%= @rushee.email %>
            <%= @rushee.grade %>
            <%= @rushee.major %>
      </h1>

      <section>
        <%= render 'shared/rusheepost_form' %>
      </section>

    </section>
  </aside>

  <!-- Displays the rusheeposts that are associated with the current rushee -->
  <div class="span8">
    <% if @rushee.rusheeposts.any? %>
      <h3>Comments (<%= @rusheeposts.count %>)</h3>
      <ol class="rusheeposts">
         <%= render @rusheeposts %>
      </ol>
    <% end %>
  </div>

</div>

_rusheepost_form.html.erb

<%= form_for([@rushee, @rusheepost]) do |f| %>

  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_area :content, placeholder: "Compose new comment..." %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

_rusheepost.html.erb

<li>
  <span class="content"><%= rusheepost.content %></span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(rusheepost.created_at) %> ago by: <%= rusheepost.active.name %>
  </span>
</li>

问题

_rusheepost.html.erb部分不会呈现。 content每次都会正确呈现。但是,如果我添加Posted <%= rusheepost.created_at %>,则会将其呈现为:Posted 2013-12-25 21:08:19 UTC。但是,Posted <%= time_ago_in_words(rusheepost.created_at) %>给我一个undefined method '>' for nil:NilClass错误。

此外,只需添加Posted by: <%= rusheepost.active.name %>就可以获得相同的nilclass错误。但是,放Posted by: <%= rusheepost.active %>会让我:Posted by: #<Active:0x007ff3466108a0>

奇怪的是,如果我尝试检索rusheepost的rushee属性,一切都会有效。例如,Posted by: <%= rusheepost.rushee %>会产生Posted by: #<Rushee:0x007fa6681a56d8>。同样地,Posted by: <%= rusheepost.rushee.name %>会产生Posted by: Theodora Willms III,这是与帖子相关联的rushee的名称(以及我所在的页面。)显然,这不是我想要的功能 - 我想要每个发布以显示发布的人,而不是发布帖子的人。

有没有人对于为什么会发生这种情况有任何想法?也许我没有正确初始化一些东西?我没有看到为什么我无法检索rusheepost的活动属性,但我可以检索rusheepost的rushee的属性。我还必须注意,在我添加表单以创建新的rusheeposts之前,我将routes.rb中的资源列在彼此之上(不是嵌套的),并且一切正确呈现,包括这个:Posted <%= time_ago_in_words(rusheepost.created_at) %> by: <%= rusheepost.active.name %>。在我嵌套资源并更改我的控制器以反映嵌套之后,我描述的问题出现了。

谢谢,对(非常)长篇帖子感到抱歉。如果有任何我可以发布的信息/代码可以帮助我,请告诉我。

编辑:我还必须注意,如果我重置没有rusheeposts的数据库,请登录我的网站,在rushee的页面上发帖,然后进入rails console并输入Rusheepost.first.active.name,我实际上得到了我登录的Active的名称,这让这更令人费解......

2 个答案:

答案 0 :(得分:0)

您的=>文件中有%>而不是_rusheepost.html.erb终止您的Ruby代码。

答案 1 :(得分:0)

解决。需要在@rusheeposts = @rushee.rusheeposts.all中放置@rusheeposts = @rushee.rusheeposts而不是RusheesController,这样才能解决问题。