簡體   English   中英

Rails 4-多態關聯-多個關聯

[英]Rails 4 - Polymorphic associations - multiple associations

我正在嘗試制作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(可注釋:Article.first,user_id:“ 1”,意見:“ test”)文章負載(15.5毫秒)選擇“ articles”。*從“ articles”開始按“ articles”排序。“ created_at” DESC LIMIT 1 (0.2ms)BEGIN SQL(6.3ms)插入到“ comments”(“ commentable_id”,“ commentable_type”,“ user_id”,“ opinion”,“ created_at”,“ updated_at”)值($ 1,$ 2,$ 3,$ 4, $ 5,$ 6)返回“ 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"

什么都沒發生。

此外,我嘗試按照此視頻教程中的示例進行操作(大約7m:30s):

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

並將開頭的@article替換為@commentable:

<% @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容器。

1)如果將變量傳遞給partial,它將作為局部變量而不是實例變量:

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

2)還要注意,從控制器渲染和從視圖渲染是不同的事情。 在控制器中,您不必指定:partial=>因為Rails中的控制器應呈現整個模板,但是如果您在視圖文件中,則應作為Rails每個請求僅呈現一個模板

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM