简体   繁体   中英

Ruby on Rails: Default template for actions in one controller

I have a controller that happens to use the same (rabl) template for each action. Is there a way to render this template by default without having to put call render 'name' in each action?

I think you can create another layout for your particular controller. For example:

class MyController < ApplicationController
  layout "name"
  #...
end

Please see "2.2.14 Finding Layouts" in the Rails Guides - Layouts and Rendering in Rails

UPDATE:

The default render behavior is to render the view which has the same name of the action if no render view is assigned. To change the default behavior, you need to override the render method, for example:

class MyController < ApplicationController
  #...
  def render *args
    args = ["the_view_name_which_you_want_to_render"] if args == []
    super
  end
  # ...
end

You can move the method to the application controller if you want to override the render method in all controllers, but I don't think it is a good idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM