我创建了一个应用程序,它有几个与Comment模型多态关联的模型(比如A,B)。当查看与A控制器关联的页面时,显示操作,与A对象关联的注释将显示为创建新对象的表单。所有这些都有效,类似于在铁路网站上发布的Ryan Bates的15分钟博客。但是,如果我添加验证以确保用户不提交空白注释,我不确定如何呈现它。这是我在评论控制器中的内容:
before_filter :load_resources, :only => [:create]
def create
if @comment.save
redirect_to @back
else
render @action
end
end
private
def load_resources
@comment = Comment.new(params[:comment])
case @comment.commentable_type
when 'A'
@a = A.find(params[:a_id]
@comments = @a.comments
@back = a_url(@comment.commentable_id)
@resource = @a
@action = 'as/show'
when 'B'
...
end
end
查看注释和表单的部分内容(使用Haml):
=render :partial => 'comments/comment', :collection => @comments
%h3 Leave a comment:
-form_for [@resource, Comment.new] do |f|
=f.error_messages
=f.hidden_field :commentable_type, :value => params[:controller].singularize.titleize
=f.hidden_field :commentable_id, :value => params[:id]
=f.hidden_field :editor_id, :value => @current_user.id
=f.hidden_field :creator_id, :value => @current_user.id
%fieldset
=f.label :subject, 'Subject', :class => 'block'
=f.text_field :subject, :class => 'block'
=f.label :text, 'Comment', :class => 'block'
=f.text_area :text, :class => 'block'
.clear_thick
=f.submit 'Submit', :id => 'submit'
我似乎能够弄清楚如何处理验证错误。当触发验证错误时,它似乎不会触发f.error_messages。此外,当触发渲染时,它会将用户带到具有以下URL的页面:a / 2 / comments,当我希望它呈现/ 2时。
最新解决方案:
def create
subject = ""
if !@comment.save
subject = "?subject=#{@comment.subject}"
end
redirect_to @back + subject
end
然后在A控制器中显示动作:
if params.has_key?('subject')
@comment = Comment.create(:subject => params[:subject])
else
@comment = Comment.new
end
这有效,但感觉有点难看......
答案 0 :(得分:3)
由于你不知道你将在评论控制器中接收什么样的对象,所以很难绕过它。
当它不是多态关系时,它会简单得多。在我们了解如何做到这一点之前,我们需要了解执行单数版本的最佳方法。
我应该注意,这假设您正确定义了资源/路由:
map.resources:posts,:has_many => [ :评论 ] map.resources:pages,:has_many => [:评论]
让我们说我们有一个简单的帖子有很多评论。以下是执行此操作的示例方法:
class CommentsController < ApplicationController
before_filter => :fetch_post
def create
@comment = @post.comments.new(params[:comment])
if @comment.save
success_message_here
redirect post_path(@post)
else
error_message_here
redirect_to post_path(@post)
end
end
protected
def fetch_post
@post = Post.find(params[:post_id])
end
end
现在我们想在多态关系中使用它,所以我们必须设置一些东西。假设我们现在有页面和帖子有评论。以下是执行此操作的示例方法:
来自你的帖子和页面显示页面:
<%= render 'comments/new' %>
在帖子控制器中:
before_filter :fetch_post
def show
@comment = @commentable.comments.build
end
protected
def fetch_post
@post = @commentable = Post.find(params[:id])
end
这使您的表单变得简单: &lt;%error_messsages_for:comment%&gt;
<% form_for [ @commentable, @comment ] do |f| %>
#Your form fields here (DO NOT include commentable_type and or commentable_id also don't include editor and creator id's here either. They will created in the controller.)
<% end %>
在你的评论控制器中:
def create
@commentable = find_commentable
# Not sure what the relationship between the base parent and the creator and editor are so I'm going to merge in params in a hacky way
@comment = @commentable.comments.build(params[:comment]).merge({:creator => current_user, :editor => current_user})
if @comment.save
success message here
redirect_to url_for(@commentable)
else
failure message here
render :controller => @commentable.class.downcase.pluralize, :action => :show
end
end
protected
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end