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 6983141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:22:47+00:00 2026-05-27T18:22:47+00:00

Suppose you want a Blog with two different layouts. One layout should look like

  • 0

Suppose you want a Blog with two different layouts. One layout should look like a conventional Blog with a header, a footer, a menu and so on. The other layout should only contain the blog posts and nothing more. How would you do that without losing the connection to the model, forcing the execution and rendering of only one action and prevent to repeat yourself (DRY)?

posts_controller.rb

class PostsController < ApplicationController
  layout :choose_layout

  # chooses the layout by action name
  # problem: it forces us to use more than one action
  def choose_layout
    if action_name == 'diashow'
      return 'diashow'
    else
      return 'application'
    end
  end

  # the one and only action
  def index
    @posts = Post.all
    @number_posts = Post.count
    @timer_sec = 5

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

  # the unwanted action
  # it should execute and render the index action
  def diashow
    index # no sense cuz of no index-view rendering
    #render :action => "index" # doesn't get the model information
  end

  [..]
end

Possibly I want to go the wrong way, but I can’t find the right one.

Update:

My solution looks like this:

posts_controller.rb

class PostsController < ApplicationController
  layout :choose_layout

  def choose_layout
    current_uri = request.env['PATH_INFO']
    if current_uri.include?('diashow')
      return 'diashow'
    else
      return 'application'
    end
  end

  def index
    @posts = Post.all
    @number_posts = Post.count
    @timer_sec = 5

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

  [..]
end

config/routes.rb

Wpr::Application.routes.draw do
  root :to => 'posts#index'

  match 'diashow' => 'posts#index'

  [..]
end

Two different routes are pointing at the same location (controller/action).
current_uri = request.env['PATH_INFO'] saves the url into a variable and the following if current_uri.include?('diashow') checks if it is the route we configured in our routes.rb.

  • 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-05-27T18:22:48+00:00Added an answer on May 27, 2026 at 6:22 pm

    You would select which layout to render depending on a certain condition. For example, a parameter in the URL, the device in which the page is being rendered etc.

    Just use that condition in your choose_layout function, instead of deciding the layout on the basis of action_name. The diashow action is unnecessary.

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

Sidebar

Related Questions

Suppose I want to update two columns in one ON DUPLICATE STATEMENT: INSERT INTO
Suppose I have two tables, one with blog posts and another with readers and
For example suppose in designing a blog application I want something like domain.com/post/729 Instead
Suppose I want to have a blog with Rails 3 on my website and
Suppose I want to add two buffers and store the result. Both buffers are
Suppose you want to dynamically add to a HTML menu, where the menu HTML
Suppose I want to implement an interpreter for a functional language. I would like
Suppose we want two constructors for a class representing complex numbers: Complex (double re,
Suppose I want to redirect all GET queries from one site to another, using
i'm having this problem: Suppose you've a blog application and you want to display

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.