ActionDispatch :: Request的未定义方法`flash’

我正在尝试在Rails 4中编写一个Ember应用程序,并决定使用rails-api作为api控制器,同时保持应用程序控制器完好无损的几页不属于单页面应用程序。 换句话说,这是我的控制器:

app/controllers/application_controller.rb

 class ApplicationController < ActionController::Base protect_from_forgery end 

app/controllers/sample_controller.rb

 class SampleController < ApplicationController # my methods end 

app/controllers/api/v1/api_controller.rb

 class Api::V1::ApiController < ActionController::Api include ActionController::MimeResponds end 

app/controllers/api/v1/sample_controller.rb

 module Api::V1 class SampleController < ApiController respond_to :json # my methods end end 

我的application.html.slim包含以下行:

 == render partial: "flash_msgs" unless flash.blank? 

包含哪个会导致以下错误:

未定义的方法’flash’用于#

根据这个线程的讨论,似乎罪魁祸首可能是rails-api ,但鉴于我已经建立的inheritance,我并不完全相信。 有什么建议?

不确定但是你可能需要包含ActionDispatch::Flash中间件来支持flash。 使用:

 config.middleware.use ActionDispatch::Flash 

文档说:

ActionDispatch :: Flash:支持ActionController中的flash机制。

我希望它有所帮助

请参阅: https : //github.com/plataformatec/devise/issues/2775

在devise.rb里面改变

 config.navigational_formats = ['*/*', :html] 

至:

 config.navigational_formats = [:json] 

要不就 [ ]

如果您像我一样在现有应用程序之上创建API,则可以将其添加到config / application.rb文件中:

config.api_only = false