重构用于多态关联中注释的Form_for创建方法 [英] Refactoring Form_for Create Method for Comments in a Polymorphic Association

查看:76
本文介绍了重构用于多态关联中注释的Form_for创建方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理我的第一个多态关联关系,但是在重构form_for创建注释时遇到了麻烦.

I am working on my first polymorphic association relationship and I am having trouble refactoring my form_for create comments.

我尝试通过多态协会RailsCasts http://railscasts.com/情节/154-polymorphic-association?view = asciicast ,但似乎过时了.

I tried going through the Polymorphic Association RailsCasts http://railscasts.com/episodes/154-polymorphic-association?view=asciicast, but it seems dated.

我有两个问题:

  1. 如何重写我的comment_form部分,以便它适用于任何可评论的内容?我现在所拥有的方式,仅适用于(:commentable_id => @traveldeal.id)之后的traveldeal.

  1. How do I rewrite my comment_form partial so that it would work for anything commentable? The way I have it right now, it would only work for traveldeals since (:commentable_id => @traveldeal.id).

当我创建评论时,commentable_type为空.什么是commentable_type,我需要以表单形式传递它吗?

When I create the comment, the commentable_type is empty. What is commentable_type and do I need to pass it in the form?

谢谢!

user.rb

class User < ActiveRecord::Base
  has_many :comments, :dependent => :destroy
end

traveldeal.rb

traveldeal.rb

class Traveldeal < ActiveRecord::Base
  has_many :comments, :as => :commentable, :dependent => :destroy
end

comment.rb

comment.rb

class Comment < ActiveRecord::Base
  belongs_to :user
  belongs_to :commentable, :polymorphic => true

  validates :user_id, :presence => true
  validates :commentable_id, :presence => true
  validates :content, :presence => true
end

traveldeal_show.html.erb

traveldeal_show.html.erb

<%= render 'shared/comment_form'  %>

_comment_form.html.erb

_comment_form.html.erb

<%= form_for current_user.comments.build(:commentable_id => @traveldeal.id) do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>

<div>
  <%= f.text_area :content %>
</div>

<%= f.hidden_field :user_id %>
<%= f.hidden_field :commentable_id %>

<div>
  <%= f.submit "Add Comment" %>
</div>
<% end %>

comments_controller.rb

comments_controller.rb

class CommentsController < ApplicationController
  before_filter :authenticate, :only => [:create, :destroy]

  def create
    @comment = Comment.new(params[:comment])
    @comment.save
    redirect_to root_path
  end

end

推荐答案

在Railscast中注明日期的唯一部分是路线.

The only part that is dated in the Railscast are the routes.

要回答第一个问题:像在Railscast中一样创建表单:

To answer your first question: create your form like it is done in the Railscast:

<%= form_for [@commentable, Comment.new] do |f| %>
  <p>
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </p>
  <p><%= f.submit "Submit" %></p>
<% end %>

如果您这样做,将自动设置commentable_type.您需要类型,以便知道注释属于哪个模型.请注意,您必须在使用注释表单的方法中设置@commentable.

If you do it like this commentable_type will be set automatically. You need the type so you know to what model a comment belongs. Note that you have to set @commentable in the method where you use the comment form.

例如

class TraveldealsController < ApplicationController
  def show
    @traveldeal = @commentable = Traveldeal.find(params[:id])
  end
end

这篇关于重构用于多态关联中注释的Form_for创建方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆