2016-01-01 173 views
0

我正在嘗試製作Rails 4應用程序。Rails 4 - 多態關聯 - 多種關聯

我有一個多態的評論模型。我也有用戶,配置文件和文章模型。

的關聯是:

article.rb

belongs_to :user 
has_many :comments, as: :commentable 
accepts_nested_attributes_for :comments 

comments.rb

belongs_to :user 
belongs_to :commentable, :polymorphic => true 

profile.rb

belongs_to :user 
has_many :addresses, as: :addressable 

user.rb

has_many :articles 
has_many :comments 
has_one :profile 

我很努力去理解它在實踐中是如何工作的。

當我用我的控制檯,如下創建註釋:

Comment.create(commentable: Article.first, user_id: "1", opinion: "test") Article Load (15.5ms) SELECT "articles".* FROM "articles" ORDER BY "articles"."created_at" DESC LIMIT 1 (0.2ms) BEGIN SQL (6.3ms) INSERT INTO "comments" ("commentable_id", "commentable_type", "user_id", "opinion", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["commentable_id", 3], ["commentable_type", "Article"], ["user_id", 1], ["opinion", "test"], ["created_at", "2016-01-01 01:51:20.711415"], ["updated_at", "2016-01-01 01:51:20.711415"]] (1.3ms) COMMIT => #

工程。

然而,當我去查看並嘗試使用該表單來創建一個新的評論,我得到:

SELECT "articles".* FROM "articles" WHERE "articles"."id" = $1 ORDER BY "articles"."created_at" DESC LIMIT 1 [["id", 3]] 
    User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] 
Unpermitted parameter: comment 
    (0.2ms) BEGIN 
    (0.1ms) COMMIT 
[ActiveJob] Enqueued Searchkick::ReindexV2Job (Job ID: 4fd1ceb5-7882-4381-a2df-5913082af621) to Inline(searchkick) with arguments: "Article", "3" 

什麼也沒有發生。

此外,我想下面的例子在這個視頻教程(約7米:30秒):

https://gorails.com/episodes/comments-with-polymorphic-associations?autoplay=1

我改變了我的意見的部分,這是工作,以顯示在控制檯中創建的意見(但不從視圖形式):

<% @article.comments.each do | comment | %> 
    <div class="well"> 
    <%= comment.opinion %> 
    <div class="commentattribution"> 
     <%= comment.user.formal_name_and_title %> 
    </div> 
    </div> 
<% end %> 

在我的文章中添加一個局部變量顯示在哪裏呈現局部的評論頁面:

<%= render 'comments/display', locals: {commentable: @article} %> 

,並與@commentable開行替換@article:

<% @commentable.comments.each do | comment | %> 
    <div class="well"> 
    <%= comment.opinion %> 
    <div class="commentattribution"> 
     <%= comment.user.formal_name_and_title %> 
    </div> 
    </div> 
<% end %> 

現在,當我保存並嘗試使文章顯示頁面,我得到這個錯誤:

undefined method `comments' for nil:NilClass 

我不明白這個錯誤信息是什麼意思,但它一直工作正常,直到我試圖引用值得稱道的文章而不是文章。錯誤消息中引用的特定行項目是:

<% @commentable.comments.each do | comment | %> 

任何人都可以看到這裏出了什麼問題嗎?

當我嘗試:

<% commentable.comments.each do | comment | %> 

我得到這個錯誤:

undefined local variable or method `commentable' for #<#<Class:0x007fd13b93dac8>:0x007fd134530270> 

我看過一些其他職位,人們曾與多態關聯屬於多個資源的麻煩。例如,我的評論屬於用戶以及文章。我無法按照該解決方案進行解決,也無法正確理解該安排的問題。

Joe Half Face的解決方案適用於評論表單,但唯一不適用的部分是獲取寫入文章的用戶的姓名以顯示在頁面上。

在我的文章顯示頁面,我有:

<div class="articletitle"> 
        <%= @article.title %> 
       </div>  
       <div class="commentattributionname"> 
        <%= @article.user.try(:formal_name) %> 
       </div> 
       <div class="commentattributiontitle"> 
        <%= @article.user.try(:formal_title) %> 
       </div> 
       <div class="commentattributiondate"> 
        <%= @article.created_at.try(:strftime, '%e %B %Y') %> 
       </div> 

標題,表演和創建日期顯示,但無論是用戶屬性的出現。

這只是一個空白的div容器,顯示在代碼檢查器中。

+0

'{commentable:@article}'你通過本地變量,但嘗試獲取實例'@ commentable'而不是'commentable' –

+0

如果我將@commentable更改爲commentable,則會出現此錯誤:未定義的局部變量或方法'commentable'for#<#: 0x007fd134530270> – Mel

+0

'<%= render'comments/display',locals:{commentable:@article}%>'可能是此問題的來源,因爲您爲partials轉義默認模式:'<%= render:partial =>'評論/展示',當地人:{commentable:@article}%>' –

回答

0

1)如果你傳遞變量的部分,這是不言而喻的地方,不是實例:

<%= render :partial => 'comments/display', locals: {commentable: @article} %> 

2)另外請注意,從控制器渲染和視圖渲染是不同的東西。在控制器中,你不需要指定:partial=>作爲控制器在Rails中應該呈現整個模板,但是如果你在視圖文件中,你應該像rails一樣只能爲每個請求呈現一個模板