2016-01-01 170 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一样只能为每个请求呈现一个模板