我正在尝试使用rails编写的beast论坛,并将其用作我一直面临的问题的一个示例。Rails通过重定向进行验证
论坛有一个主题/显示操作和视图,底部有一个表单以在主题内创建一个新帖子。
提交表单进入文章/创建,如果验证通过重定向回主题/表演,并且工作正常,但是如果验证失败(忽略正文字段),您将被重定向到相同的主题/回到表单,没有验证错误...通常如果验证失败,你将留在任何/使用render:action => new创建。
重定向中的验证是否丢失,以及获取它的最佳方法是什么?
见下面的代码:
PostsController.rb
def create
@post = current_user.reply @topic, params[:post][:body]
respond_to do |format|
if @post.new_record?
format.html { redirect_to forum_topic_path(@forum, @topic) }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
else
flash[:notice] = 'Post was successfully created.'
format.html { redirect_to(forum_topic_post_path(@forum, @topic, @post, :anchor => dom_id(@post))) }
format.xml { render :xml => @post, :status => :created, :location => forum_topic_post_url(@forum, @topic, @post) }
end
end
end
TopicsController.rb
def show
respond_to do |format|
format.html do
if logged_in?
current_user.seen!
(session[:topics] ||= {})[@topic.id] = Time.now.utc
end
@topic.hit! unless logged_in? && @topic.user_id == current_user.id
@posts = @topic.posts.paginate :page => current_page
@post = Post.new
end
format.xml { render :xml => @topic }
end
end
主题/显示视图
<% form_for :post, :url => forum_topic_posts_path(@forum, @topic, :page => @topic.last_page) do |f| %>
<%= f.error_messages %>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td rowspan="2" width="70%">
<%= f.text_area :body, :rows => 8 %>
</td>
<td valign="top">
<%= render :partial => "posts/formatting" %>
</td>
</tr>
<tr>
<td valign="bottom" style="padding-bottom:15px;">
<%= submit_tag I18n.t('txt.views_topics.save_reply', :default => 'Save reply') %>
</td>
</tr>
</table>
<% end %>
非常感谢。
工作流本身看起来很奇怪 – Chirantan 2009-10-08 09:06:16
你是什么意思?没有这个例子,我可能会更简单地问这个问题。基本上,如果您重定向而不是渲染,错误消息会发生什么,以及它们可以在哪里访问? – sebastyuiop 2009-10-08 09:14:06