2012-12-06 27 views
10

Khi bao gồmBắt ray-api và strong_parameters làm việc cùng nhau

gem 'strong_parameters' 
gem 'rails-api' 

với nhau trong Gemfile tôi, gọi params.require như

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

không thành công với các lỗi sau trên require() gọi.

TypeError: 
    can't convert Symbol into String 

Các vết lùi lãm strong_parameters 'ActionController::StrongParameters' require() phương pháp không bao giờ đạt.

Trả lời

32

Tôi đã dành quá nhiều thời gian cho vấn đề này, vì vậy tôi nghĩ mình sẽ chia sẻ ở đây để hy vọng cứu người khác một chút thời gian.

Lỗi trên xuất phát từ phương pháp require() trong ActiveSupport::Dependencies::Loadable được thực thi khi gọi

params.require(:user)... 

strong_parameters tiêm ActionController::StrongParameters vào ActionController::Base ở dưới cùng của this file với

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

Các rails-api đá quý đòi hỏi ứng dụng của bạn của ApplicationController mở rộng ActionController::API có lợi cho ActionController::Base

Bộ điều khiển ứng dụng không biết gì về ActionController::StrongParameters vì chúng không mở rộng lớp ActionController::StrongParameters được bao gồm trong đó. Đây là lý do tại sao cuộc gọi phương thức require() không gọi thực hiện trong ActionController::StrongParameters.

Để nói ActionController::API về ActionController::StrongParameters cũng đơn giản như thêm thông tin sau vào tệp trong config/initializers.

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

Tôi thực sự cố gắng yêu cầu tệp mã và hy vọng tên tệp là một String, do đó lỗi. – amoebe

1

Tôi có một số pull request (hiện đang mở) để khắc phục hành vi này. Thay vì gọi ActionController::API.send, điều này nên được bao gồm với ...

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

Vấn đề này có thể được giải quyết bằng cách bao gồm các rails_api chủ git branch trong Gemfile của bạn như sau:

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

rails_api đá quý có đã sửa số này issue bằng cách bao gồm các dòng dưới đây tại api.rb

if Rails::VERSION::MAJOR == 4 
    include StrongParameters 
end 
Các vấn đề liên quan