添加评论后将用户转发回帖子

发布于 2024-12-21 19:31:13 字数 392 浏览 3 评论 0原文

这可能是一件相当简单的事情 - 我有一个带有新闻源的网站,显示各种帖子。还有其他页面类似地显示帖子。可以直接从这些页面中的任何一个页面向这些帖子添加评论。

我需要做的是将用户重定向回他们添加评论后的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百思不得你姐 2024-12-28 19:31:13

您可以使用 redirect :back ,这会将用户带回到发出的页面请求

redirect_to :back

You could use redirect :back which will send the user back to the page that issued the request

redirect_to :back
牵你的手,一向走下去 2024-12-28 19:31:13

假设 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

Assuming Comment is a resource nested under Post in your routing (/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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文