这可能是一件相当简单的事情 - 我的网站上有一个显示各种帖子的新闻Feed。还有其他页面同样显示帖子。可以直接从这些页面的任何页面添加评论。
我需要做的是将用户重新定向到他们来自的网址,一旦他们添加了评论,以及他们评论的特定帖子(每个帖子的ID都为post-#{post.id}
,所以我只需要将#post-2
或其他任何内容粘贴到网址上。
如果无法保存帖子,无论出于何种原因,我还希望在重定向后将用户提交的内容预先填入评论框。
我该怎么做这两件事?第一个是更重要的一个.. 我必须在新操作的会话中存储用户来自的URL,并在创建操作上重定向到此?我怎样才能获得网址?
顺便说一句,我在Rails 3.1上。
感谢您的帮助!
答案 0 :(得分:1)
假设Comment是一个嵌套在路由中的Post下的资源(/ posts / 123 / comment / new):
def create
comment = Comment.new(params[:comment])
if comment.save
redirect_to post_path(comment.post)
else
redirect_to new_post_comment_path(params)
# or maybe you have the comment form on the Post#show page
# redirect_to post_path(params)
end
end
答案 1 :(得分:1)
您可以使用redirect :back将用户发送回发出请求的页面
redirect_to :back