In my Rails application, I use <html id=<%= params[:controller] + "_" + params[:action] %>
in views/layouts/application.html.erb.
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.
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".
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?
For the controller, you have access to the local instance controller
.
If you want the full name of it, you'll need to use something like controller.class.name.split("::").first
which will give you UsersController
in your example. If you just wanted users
to be returned you could use controller.controller_name
For the action you can use controller.action_name
UPDATE:
Please try these in your view and see if they're also lagging:
<p><%= controller_name %></p>
<p><%= action_name %></p>
I suspect they both delegate to the controller mentioned earlier in my answer, but it's worth trying.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With