Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3: Two different layouts with the same controller and action?
    text
    copied!<p>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)?</p> <p>posts_controller.rb</p> <pre><code>class PostsController &lt; 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 =&gt; "index" # doesn't get the model information end [..] end </code></pre> <p>Possibly I want to go the wrong way, but I can't find the right one.</p> <p><strong>Update:</strong></p> <p>My solution looks like this:</p> <p>posts_controller.rb</p> <pre><code>class PostsController &lt; 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 </code></pre> <p>config/routes.rb</p> <pre><code>Wpr::Application.routes.draw do root :to =&gt; 'posts#index' match 'diashow' =&gt; 'posts#index' [..] end </code></pre> <p>Two different routes are pointing at the same location (controller/action). <code>current_uri = request.env['PATH_INFO']</code> saves the url into a variable and the following <code>if current_uri.include?('diashow')</code> checks if it is the route we configured in our <em>routes.rb</em>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload