Rails 4 + Devise + Rails API: undefined 'flash' method

I have an existing Rails 4 application that uses Devise and Omniauth . I am trying to modify an existing application for developing an API (I mean that I will have my web application, and I also want to use the API for a mobile application), but it is difficult for me.

I follow the steps listed in the Rails API in the "For Existing Applications" section and now I get the error below

undefined method `flash' for #<Devise::SessionsController:0x000000098221d0> 

An error appears in this line <% unless flash.blank? %> <% unless flash.blank? %> in views/devise/sessions/new.html.erb

I tried putting config.middleware.use ActionDispatch::Flash in /config/application.rb , but there is an error .

I also have config.api_only = false in /config/application.rb

Below is my code

 #app/controllers/application_controller.rb class ApplicationController < ActionController::API # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. include ActionController::RequestForgeryProtection include Pundit include ActionController::Serialization include ActionView::Layouts include ActionController::ImplicitRender protect_from_forgery with: :null_session ----some other code------ end 

-------- xxxxxx -----------

 #/config/application.rb require File.expand_path('../boot', __FILE__) require 'rails/all' # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups, :default, :assets, Rails.env) module SampleApp class Application < Rails::Application # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. # config.time_zone = 'Central Time (US & Canada)' # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] # config.i18n.default_locale = :de # For Custom Error Handling config.exceptions_app = self.routes config.middleware.use ActionDispatch::Flash # To use rails default middleware for api config.api_only = false config.generators do |g| g.test_framework :rspec, :fixtures => true, :view_spec => false, :helper_specs => false, :routing_specs => false, :controller_specs => true, :request_specs => true g.fixture_replacement :factory_girl, :dir => "spec/factories" end end end 

------- xxxxxxxx -------

 #routes.rb Rails.application.routes.draw do devise_for :admin_users, ActiveAdmin::Devise.config ActiveAdmin.routes(self) # TODO add staging/dev password lock to site # if Rails.env.staging? # mount Lockup::Engine, at: '/lockup' # end # TODO come back and fix so admin user (active admin) and regular user can be logged in at same time get '/', to: 'users#index', constraints: lambda { |request| request.env['warden'].user && request.env['warden'].user.role == 'admin' } #root to: 'visitors#index' devise_for :users, :controllers => { :registrations => "registrations", :omniauth_callbacks => "users/omniauth_callbacks" } devise_scope :user do root to: "devise/sessions#new" end namespace :api do namespace :v1 do devise_for :users end end #some other unnecessary routes end 

Question:

The Rails API says it

If you want to use the default Rails middleware stack (avoid the shorthand that rails-api does), you can simply add config.api_only = false to the config / application.rb file.

Since I included config.api_only = false in config/application.rb and given that ActionDispatch::Flash comes with Rails by default , why am I getting this error? What do I need to get rid of this?

+5
source share
3 answers

I tried many suggestions before I pushed it to these steps. It worked for me. Hope this helps.

Add the following lines to config/routes.rb :

 devise_for :users, defaults: { format: :json } 

Add the following lines to config/initializers/devise.rb :

 config.navigational_formats = [:json] 

Add the following lines to config/application.rb :

 config.api_only = true config.middleware.use ActionDispatch::Flash 
+10
source

Devise will try to install Flash if it thinks you are expecting a response in HTML.

I suspect the only problem is that when you called the API you did not go through the following headers:

 "Accept": "application/json" 
0
source

If you try to answer above and still get the same error, try the following.

  • Reboot the rails server.

If the error still persists:

  1. In the application.rb file, change the line config.api_only = true that you just added to false:

     config.api_only = false 
-4
source

Source: https://habr.com/ru/post/1235341/


All Articles