2015-11-06 82 views
5

我有一個現有導軌4應用程序,它採用設計Omniauth。我試圖修改我現有的應用程序,以開發一個API(我的意思是我的網絡應用程序將保持原樣,同時我也希望爲移動應用程序提供API),但我的運氣很不好。軌道4 + +設計的Rails API:未定義的方法 '閃速'

我下面一節中列出Rails-API下「對於已經存在的應用程序」中的步驟,現在我得到下面的錯誤

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

的錯誤views/devise/sessions/new.html.erb

在該行 <% unless flash.blank? %>彈出

我試過把config.middleware.use ActionDispatch::Flash放在/config/application.rb,但是錯誤存在

我也/config/application.rb

下面有config.api_only = false是我的代碼

#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 

------- ------- XXXXXXX

#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 

問:

Rails-API說這

如果你想使用Rails的默認中間件堆棧(避免 減少是Rails的API一樣),你可以添加config.api_only = false到config/application.rb文件。

因爲我已經包含config.api_only = falseconfig/application.rb,並在考慮到ActionDispatch::Flash附帶Rails的默認,爲什麼我收到錯誤?我需要擺脫那些?

+0

你已經嘗試此suggestiion? http://stackoverflow.com/a/21149689/981183 –

+0

@KMRakibulIslam我只是試了一下,結果是一樣的錯誤。 – Pavan

回答

10

在我把它煮到這些步驟之前,我嘗試了很多建議。它爲我工作。希望這可以幫助。

添加以下行config/routes.rb

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

添加以下行config/initializers/devise.rb

config.navigational_formats = [:json] 

添加以下行config/application.rb

config.api_only = true 
config.middleware.use ActionDispatch::Flash 
+4

其實,它是固定的我沒有'config.middleware.use ActionDispatch :: Flash' –

+1

但必須徹底刪除導航格式: config.navigational_formats = [] –

+2

我需要所有這些東西。我一直對我的頭撞了三個小時。我希望我能給你買一盒巧克力。 xoxoxo – nilatti

-4

如果你嘗試回答上面,仍然得到相同的錯誤,請嘗試fol降脂。

  1. 重新啓動軌服務器

如果錯誤仍然存​​在:

  • application.rb文件更改剛纔添加到假行config.api_only = true

    config.api_only = false 
    
  • +0

    關閉'api_only'可能會解決錯誤,但完全無視原始帖子的目的,即創建僅api應用。 – Micah

    0

    設計只會嘗試設置Flash,如果它認爲你期待你的答案在HTML中。

    我懷疑 - 唯一的問題是 - 當你調用API你沒有在下列頭傳:

    "Accept": "application/json" 
    
    相關問題