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'
------- 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?