gpt4 book ai didi

ruby-on-rails - 多态评论,评论验证失败时如何渲染?

转载 作者:行者123 更新时间:2023-12-04 07:04:04 26 4
gpt4 key购买 nike

我创建了一个应用程序,该应用程序具有多个与 Comment 模型多态关联的模型(例如 A、B)。当查看与 A Controller 关联的页面时,显示操作,与 A 对象关联的评论显示为创建新对象的表单。所有这些都有效,类似于 Ryan Bates 在 Rails 网站上发布的 15 分钟博客。但是,如果我添加验证以确保用户不会提交空白评论,我不确定如何呈现。这是我的评论 Controller 中的内容:

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,当我希望它渲染 a/2 时。

最新解决方案:
def create
subject = ""
if !@comment.save
subject = "?subject=#{@comment.subject}"
end
redirect_to @back + subject
end

然后在 A Controller 中显示 Action :
if params.has_key?('subject')
@comment = Comment.create(:subject => params[:subject])
else
@comment = Comment.new
end

这有效,但感觉有点丑陋......

最佳答案

很难理解它,因为您不知道将在评论 Controller 中接收什么样的对象。

当它不是多态关系时,它会简单得多。在我们了解如何做到这一点之前,我们需要了解进行单数版本的最佳方法。

我应该注意,这假设您正确定义了资源/路由:

map.resources :posts, :has_many => [:comments ]
map.resources :pages, :has_many => [:comments ]

假设我们有一个简单的例子,一个帖子有很多评论。这是执行此操作的示例方法:

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

在帖子 Controller 中:
before_filter :fetch_post

def show
@comment = @commentable.comments.build
end

protected
def fetch_post
@post = @commentable = Post.find(params[:id])
end

这将您的表单设置为简单:
<% error_messages_for :comment %>
<% 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 %>

在您的评论 Controller 中:
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

关于ruby-on-rails - 多态评论,评论验证失败时如何渲染?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1386581/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com