Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8432711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:12:11+00:00 2026-06-10T06:12:11+00:00

I have a Rails controller whose actions all share a similar view layout. Rather

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:12:13+00:00Added an answer on June 10, 2026 at 6:12 am

    Can you better explain why layouts don’t work? I believe this code should solve your problem.

    Let’s say you have UsersController and you want all the actions, except new and create to use the same views. Then you can easily do:

    def UsersController < ApplicationController
      layout "shared_layout", except: [:new, :create]
      layout "new_user_layout", only: [:new, :create]
    
      def show
        # Renders in app/views/layouts/shared_layout.html.erb
      end
    
      def index
        # Renders in app/views/layouts/shared_layout.html.erb
      end
    
      def new
        # Renders in app/views/layouts/new_user_layout.html.erb
      end
    
      def create
        # Renders in app/views/layouts/new_user_layout.html.erb
      end
    end
    

    Then you can create a layout which say what needs to be render in the shared. In app/views/layouts/shared_layout.html.erb

    <html>
      <body>
        <%= render "shared/shared_partial_1.html.erb" %>
        <%= render "shared/shared_partial_2.html.erb" %>
        <%= render "shared/shared_partial_3.html.erb" %>
        <%= yield %>
      </body>
    </html>
    

    This will render partials 1, 2, and 3 in the same manner for all the actions of the controller. What is wrong with that?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created model, controller and view with rails scaffold generator: rails g scaffold
I have a rails controller with two actions defined: index and show . I
I have the code sequence below in a rails controller action. Before the IF,
I currently have a .find method in one of my rails controller actions -
I have a rails controller handling requests to www.mydomain/foo, now I want all traffic
I have a Rails controller action to test. In that action, a method User.can?
In my Rails controller I have something like: def authenticate_user! if not current_user #
I have this command in a Rails controller open(source) { |s| content = s.read
I have a simple problem regarding a loop in a Rails controller. Here's the
I have a Rails app with the usual application_controller, and a controller (and model)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.