Reputation: 8079
I was wondering if I could execute / display some stuff differently in partials depending on the context in which it appears.
For example, I have a _user_info partial that appears in a sidebar, and also in the user page, and I want to display some extra info in the second case. How can I express that kind of conditions?
Upvotes: 2
Views: 900
Reputation: 3174
You can use controller_name
and action_name
methods.
if controller_name == 'user' && action_name == 'show'
details
end
Upvotes: 4
Reputation: 160191
Sure, it just depends on how you want to discover/expose that context. The simple way is conditionals, which can be set in any number of ways, or derived from a set of conditions. That can happen in a filter, an action, etc.
If the changes are large enough, better to just encapsulate them in separate partials.
Another option is to wrap the render
tag in a helper that calculates/grabs those conditions.
It kind of depends on the nature of the conditions, where/how you want to deal with them, etc.
Upvotes: 1
Reputation: 10564
What you're asking for is something this:
if params[:controller].eql?('users')
view code
However I would split the partial into two separate partials and display the distinct parts from whichever view you need.
Upvotes: 2