Rails 3, how to add a view that does not use the same layout as other applications?

I could not find any documents or examples on how to structure my application so that different views in the same controller could use completely different layouts and style sheets.

Our application was tinted, and then we used nifty generators to create views, and then added for authentication. We have views and controllers for two models: widgets and companies.

I currently have one layout: layouts / application.html.haml, I don't see the link wherever you are, so I assume (new to rails) that it is always used in the naming convention.

Now I need to add a couple of views (for mobile browsers) that have a different stylesheet and layout (for example, without login / logout links in the upper right corner), inside the same controllers.

How can I do that?

+48
ruby-on-rails layout
Feb 23 '11 at 11:10
source share
5 answers

By default layouts/application.html.haml ( .erb if you are not using haml).

In fact, a layout file can be set for each controller or for each action, and not for each view for each view folder.

There are several cases:

To change the default layout file for the entire controller (i.e. use another.html.haml instead of application.html.haml )

 class ApplicationController < ActionController::Base layout "another" # another way layout :another_by_method private def another_by_method if current_user.nil? "normal_layout" else "member_layout" end end end 

To change all actions in a specific controller to use a different layout file

 class SessionsController < ActionController::Base layout "sessions_layout" # similar to the case in application controller, you could assign a method instead end 

To change an action to use a different layout file

 def my_action if current_user.nil? render :layout => "normal_layout" else render :action => "could_like_this", :layout => "member_layout" end end 
+123
Feb 23 '11 at 11:24
source share

If you don't want too complicated, you can just do this:

 layout 'layout_one' def new @user= User.new render layout: 'landing_page' end 

it will be done.

+3
Oct 28 '13 at 3:46 on
source share

I am sure there are many answers to this, but here is another one where you can use different layouts for controllers or per action.

 class ListingsController < ApplicationController # every action will use the layout template app/views/layouts/listing_single.html.erb layout 'listing_single' # the 'list' action will use the layout set in the 'alternative_layout' method # you can also add multiple actions to use a different layout,just do like layout :alternative_layout, only: [:list,:another_action] layout :alternative_layout, :only => :list def show end def list end private def alternative_layout if current_user.is_super_admin? #if current use is super admin then use the layout found in app/views/layouts/admin.html.erb otherwise use the layout template in app/views/layouts/listing_list.html.erb 'admin' else 'listing_list' end end end 
+3
Aug 27 '15 at 9:55
source share

Yes, you can use different layouts and style sheets inside the same controllers.

Guidelines for guides on layouts are a good place to start. See Section 3 - Structuring Layouts

There are several ways to use a different layout, but one of the easiest is to simply add a file with the same name as your controller in the layouts/ folder. Therefore, if your controller is a PostsController , then adding layouts/post.html.haml will cause the rails to use this layout. If no such layout is found and no other layouts are specified, the rails will use the default layouts/application.html.haml

+2
Feb 23 '11 at 11:23
source share

Well, if this is a different view for mobile, but all desktop versions are the same, you can use JQtouch.

http://railscasts.com/episodes/199-mobile-devices

 # config/initializers/mime_types.rb Mime::Type.register_alias "text/html", :mobile # application_controller.rb before_filter :prepare_for_mobile private def mobile_device? if session[:mobile_param] session[:mobile_param] == "1" else request.user_agent =~ /Mobile|webOS/ end end helper_method :mobile_device? def prepare_for_mobile session[:mobile_param] = params[:mobile] if params[:mobile] request.format = :mobile if mobile_device? end 

The above code is taken from the Railscasts example.

+1
Feb 23 '11 at 11:26
source share



All Articles