【发布时间】:2011-05-02 06:07:41
【问题描述】:
我正在 Rails 中创建一个常规论坛来练习。我有一个Topic 模型和一个嵌套的Post 模型。主题可以有很多帖子。
Topics#Show 有一个 @topic.posts 列表,然后是一个新的 Post 表单。
# Topics#Show
def show
@topic = Topic.find(params[:id])
@post = @topic.posts.new
end
提交新帖子发送至Posts#Create
# Posts#Create
def create
@topic = Topic.find(params[:topic_id])
@post = @topic.posts.new(params[:post])
@post.user = current_user
if @post.save
redirect_to @topic, :notice => "Successfully created post."
else
render :action => 'new' # <-- Unsure what to do here
end
end
如果帖子保存失败,我希望它呈现Topics#Show 并在那里显示验证错误。
据我了解,参数不会通过 redirect_to 持续存在,因为 302 重定向会启动新请求。
【问题讨论】:
标签: ruby-on-rails-3