添加评论后将用户转发回帖子
这可能是一件相当简单的事情 - 我有一个带有新闻源的网站,显示各种帖子。还有其他页面类似地显示帖子。可以直接从这些页面中的任何一个页面向这些帖子添加评论。
我需要做的是将用户重定向回他们添加评论后的 URL,以及他们评论的特定帖子(每个帖子的 id 为 post-#{post.id }
,所以我只需将 #post-2
或其他内容粘贴到 URL 上。
如果由于某种原因无法保存帖子,我也希望有用户提交的内容预先填写在评论框之后重定向。
我该如何做这两件事?第一件事是更重要的。 我必须在新操作的会话中存储用户来自的 URL,并在创建操作时重定向到该 URL?我怎样才能获得网址?
顺便说一下,我使用的是 Rails 3.1。
感谢您的帮助!
This is probably a fairly simple thing to do - I have a site with a news feed that shows various posts. There are also other pages that similarly show posts. Comments can be added to these posts directly from any of these pages.
What I need to do is redirect the user back to the URL they came from, once they've added a comment, and to the particular post they commented on (each post has an id of post-#{post.id}
, so I'd just have to stick #post-2
or whatever to the URL.
If the post could not be saved, for whatever reason, I'd also like to have the content that the user had submitted pre-filled into the comment box after the redirect.
How can I do these two things? The first is the more important one..
I'd have to store the URL that the user is coming from in the session on the new action and redirect to this on the create action? How can I get the URL?
I'm on Rails 3.1 by the way.
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 redirect :back ,这会将用户带回到发出的页面请求
You could use redirect :back which will send the user back to the page that issued the request
假设 Comment 是嵌套在路由中的 Post 下的资源 (/posts/123/comment/new):
Assuming Comment is a resource nested under Post in your routing (/posts/123/comment/new):