禁用rails中的自动渲染

时间:2014-10-02 01:25:51

标签: ruby-on-rails ruby

我需要的是禁用rails中的自动页面(HTML)呈现并使用after_action方法覆盖它。我想要达到的目标是CakePHP $ this-> autoRender = false;

application_controller.rb

class ApplicationController < ActionController::Base

  after_action :custom_render

  layout nil # Tried this but didn't worked

  def custom_render
      render #[...]
  end


end

some_controller.rb

class SomeController < ApplicationController

    def index
        # No rendering here
    end

end

如代码中所示,我尝试添加布局nil以防止所有操作都呈现,但这似乎不会影响操作的行为。

2 个答案:

答案 0 :(得分:0)

禁用渲染(没有返回任何内容)问题。

def index
  render :nothing
end

但是做任何事都为时已晚,因为它会以空身回复。

要禁用布局:

def index
  render layout: false
end

这将使您在没有布局的情况下进行查看,发出问题(render layout: 'my_custom_layout')以呈现默认视图但布局不同。

我们不知道你想要什么,但最简单的解决方案就是渲染一个特定的视图,f.i。:

def index
  render 'my_custom_file.'
end

有很多选择:http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

编辑 - 根据评论中的要求

class ApplicationController < ActionController::Base
  before_action :set_user_template

  # ...

  def set_user_template
    template_name = current_user.template_name
    self.class.layout "#{template_name}/application"
  end
end

答案 1 :(得分:0)

尚未检查它是否适用于Rails 4,但是此补丁适用于Rails 5。

根据BasicImplicitRenderImplicitRender的代码,其中- (void)loadImage:(LeafletURL*)leafletURLInput isThumbnail:(BOOL)isThumbnailInput isBatchDownload:(BOOL)isBatchDownload isRetina:(BOOL)isRetina { isRetina_ = isRetina; if (session) { /*is this the right call here? */ [session invalidateAndCancel]; [session release]; session = nil; } if (mImageData) { [mImageData release]; mImageData = nil; } self.leafletURL = leafletURLInput; self.isThumbnail = isThumbnailInput; NSString* location = (self.isThumbnail) ?leafletURL.thumbnailLocation :leafletURL.hiResImageLocation; //// Check if the image needs to be downloaded from server. If it is a batch download, then override the local resources//// if ( ([location isEqualToString:kLocationServer] || (isBatchDownload && [location isEqualToString:kLocationResource])) && self.leafletURL.rawURL != nil ) { //NSLog(@"final loadimage called server"); //// tell the delegate to get ride of the old image while waiting. //// if([delegate respondsToSelector:@selector(leafletImageLoaderWillBeginLoadingImage:)]) { [delegate leafletImageLoaderWillBeginLoadingImage:self]; } mImageData = [[NSMutableData alloc] init]; /*download tasks have their data written to a local temp file. It’s the responsibility of the completion handler to move the file from its temporary location to a permanent location.*/ NSURL* url = [NSURL URLWithString:[leafletURL pathForImageOnServerUsingThumbnail:self.isThumbnail isRetina:isRetina]]; NSURLRequest* request = [NSURLRequest requestWithURL:url]; session = [NSURLSession sharedSession]; dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { // do something with the data }]; [dataTask resume]; } //// if not, tell the delegate that the image is already cached. //// else { if([delegate respondsToSelector:@selector(leafletImageLoaderDidFinishLoadingImage:)]) { [delegate leafletImageLoaderDidFinishLoadingImage:self]; } } } 的{​​{1}}负责调用send_action

文档说:

  

对于API控制器,隐式响应始终为204无内容。

     

对于所有其他控制器,我们使用...启发式方法来决定是否   渲染模板,对缺少的模板提出错误或使用   204没有内容...

因此,我想重新定义BasicImplicitRender方法将为您服务。

在您的控制器中:

default_render

您也可以像it is done in Rails一样入侵default_render,甚至完全跳过def a # uses `default_render` unless you call `render` method explicitly end def b render plain: 'Custom text for b' # `default_render` won't be called end private # This does the trick # def default_render render plain: 'Text' end 通话:

send_action