2012-12-06 17 views
10

Gemfileで一緒には一緒に

gem 'strong_parameters' 
gem 'rails-api' 

含め、params.require

よう
private 
    def user_params 
    params.require(:user).permit(:first_name, :last_name) 
    end 

を呼び出すとrequire()コールに次のエラーで失敗すると動作するようにレール-APIとstrong_parametersを取得します。

TypeError: 
    can't convert Symbol into String 

バックトレースはstrong_parameters 'ActionController::StrongParameters' require()方法がヒットされることはありません示しています。

答えて

32

私はこの時間が長すぎましたので、他の人に少しでも時間を節約するためにここで共有したいと思いました。

エラーが上記 ActiveSupport::Dependencies::Loadablerequire()方法から来て

params.require(:user)... 

strong_parametersを呼び出したときに実行

ActionController::Base.send :include, ActionController::StrongParameters 

this fileの下部にActionController::BaseActionController::StrongParametersを注入rails-api宝石アプリのApplicationController必要とされていますActionController::APIを延長してActionController::Base

ActionController::StrongParametersが含まれていたため、アプリケーションコントローラはActionController::StrongParametersについて何も知らない。このため、require()メソッド呼び出しで実装がActionController::StrongParametersに呼び出されないのはこのためです。

ActionController::APIについては、約ActionController::StrongParametersは、config/initializersのファイルに次のように追加するだけで簡単です。

ActionController::API.send :include, ActionController::StrongParameters 
+0

実際にファイルを要求します。コードとし、ファイル名がStringであることを想定しているため、エラーです。 – amoebe

1

この動作を修正する必要があるpull request(現在開かれています)があります。

gem 'rails-api', git: 'https://github.com/rails-api/rails-api.git', branch: 'master' 

rails_api宝石があります。その代わりActionController::API.send、このこの問題は、次のようにあなたのGemfileでrails_apiマスターgitのブランチを含むことによって解決することができます...

ActiveSupport.on_load(:action_controller) do 
    include ActionController::StrongParameters 
end 
5

に含まれるべきを呼び出しますこのissueを次の行を含めて修正しましたapi.rb

if Rails::VERSION::MAJOR == 4 
    include StrongParameters 
end