I have a Rails controller whose actions all share a similar view layout. Rather than duplicating the code in several different views I’d like all of the actions to share the same view (and perhaps even actions from another controller).
I realize this can be accomplished by adding a render ... call to every action and telling it to use the same view file, but is there any way I can hook this into a group of actions and avoid being explicit inside every single one?
Can you better explain why layouts don’t work? I believe this code should solve your problem.
Let’s say you have
UsersControllerand you want all the actions, except new and create to use the same views. Then you can easily do:Then you can create a layout which say what needs to be render in the shared. In
app/views/layouts/shared_layout.html.erbThis will render partials 1, 2, and 3 in the same manner for all the actions of the controller. What is wrong with that?