[英]Get referrer URL with parameters in Rails
I have a site with the basic rails scaffold, when a user deletes a record the default action is to redirect to the home page.我有一个带有基本 Rails 脚手架的站点,当用户删除记录时,默认操作是重定向到主页。 However I would like it to return to the list the user was just looking at.
但是我希望它返回到用户刚刚查看的列表。 Right now I'm using
request.referrer
which is technically getting the referral URL but the parameters are not included...现在我正在使用
request.referrer
从技术上讲它正在获取推荐 URL 但不包括参数......
In my rails logs I can see "Started GET /books/book_preview?name=Hunger+Games"在我的 Rails 日志中,我可以看到“Started GET /books/book_preview?name=Hunger+Games”
But request.referrer
only shows " https://xxxx/books "但
request.referrer
只显示“ https://xxxx/books ”
I have also tried .original_url
and .original_fullpath
but those return the path of the current page for the record "/books/HungerGames".我也尝试过
.original_url
和.original_fullpath
但它们返回记录“/books/HungerGames”的当前页面的路径。 I also tried URI(request.referrer).query
to at least just get the parameters but that threw an error.我还尝试了
URI(request.referrer).query
以至少获取参数,但引发了错误。
I would like to get the previous path with the parameters like: /books/books_preview?name=Hunger+Games
我想使用以下参数获取以前的路径:
/books/books_preview?name=Hunger+Games
Also this list is a remote partial that is rendered through JS.此列表也是通过 JS 呈现的远程部分。 You can't see the URL in the browser URL bar only when highlight over it or look in the rails logs.
只有在突出显示它或查看 rails 日志时,您才能在浏览器 URL 栏中看到 URL。 It doesn't even show up in the request when i looked through it using
request.inspect
.当我使用
request.inspect
查看它时,它甚至没有出现在请求中。
Thanks in advance for any help!在此先感谢您的帮助! Have been stuck on this all day!
一整天都被困在这上面!
request.referrer
It will give you referral url with query params as well.它还会为您提供带有查询参数的推荐网址。
You're not getting the query string
from the URL, according to this answer (& docs ), you need:根据此答案(& docs ),您没有从 URL 获取
query string
,您需要:
request.fullpath #-> book_preview?name=Hunger+Games
-- --
As an aside, if you're using this as a lists page, it should be okay.顺便说一句,如果您将其用作列表页面,应该没问题。 However, if you're wanting to pull specific
book
records, you'd be better using a member
route:但是,如果你想提取特定的
book
记录,你最好使用member
路由:
#config/routes.rb
scope "/books" do
resources :book_preview, only: :show, param: :name #-> url.com/books/book_preview/:name
end
This would give you the ability to return a specific object for that book, negating any need to hack the request path etc.这将使您能够返回该书的特定对象,从而无需破解请求路径等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.