Rails:获取当前请求的控制器和动作 [英] Rails: Get the controller and action of the current request
问题描述
在我的 Rails 应用程序中,我在视图/布局/应用程序中使用 <html id=<%= params[:controller] + "_" + params[:action] %>
.html.erb.
In my Rails application, I use <html id=<%= params[:controller] + "_" + params[:action] %>
in views/layouts/application.html.erb.
奇怪的是,params[:controller]
和 params[:action]
的值总是滞后 1 个请求 如果它高于打开 标签.
The strange thing is, the values of params[:controller]
and params[:action]
always lag by 1 request if it is anywhere above the opening <body>
tag.
因此,如果我在 users/1
上,但我来自 users/
,则上面打开 的值将是控制器:用户"和动作:索引".
params[:action]
不应该是show"吗?
So if I'm on users/1
, but I came from users/
, the values above opening <body>
will be controller: "users" and action: "index". Shouldn't params[:action]
be "show"?.
然后,如果我刷新页面,它会赶上"并正确地具有控制器:用户"和操作:显示".
Then if I refresh the page, it 'catches up' and correctly has controller: "users" and action: "show".
为什么会这样?如何获取当前请求控制器和操作?在第一次调用 yield
之前,这些参数不会更新吗?
Why is this happening? How can I get the current requests controller and action? Will these params not be updated until the first time yield
is called?
推荐答案
这是 turbolinks.关闭 turbolinks,你就可以开始了.http://blog.steveklabnik.com/posts/2013-06-25-removing-turbolinks-from-rails-4
It was turbolinks. Turn off turbolinks and you're good to go. http://blog.steveklabnik.com/posts/2013-06-25-removing-turbolinks-from-rails-4
这篇关于Rails:获取当前请求的控制器和动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!