Rails 4 Controller Test “Undefined Method Permit”

匿名 (未验证) 提交于 2019-12-03 02:18:01

问题:

I have a simple controller test based on the Rails 4 docs (using Test::Unit):

test "should create user" do   assert_difference('User.count') do     post :create, user: users(:sample_user)   end   assert_redirected_to user_path(assigns(:user)) end

And, despite the actual user being created just fine, the test fails, saying:

1) Error: UsersControllerTest#test_should_create_user: NoMethodError: undefined method permit' for "832959492":String app/controllers/users_controller.rb:23:inuser_params' app/controllers/users_controller.rb:8:in create' test/controllers/users_controller_test.rb:11:inblock (2 levels) in ' test/controllers/users_controller_test.rb:10:in `block in '

For some reason, the test isn't recognizing the new Rails 4 permit method. Right now, I have the user_params as a private method in the controller. But, I tried moving it up to the actual create action and received the same error. The user creation is also 100% standard (nothing crazy at all- just like the Rails docs).

Does anyone know how I can rewrite or otherwise make this test pass?

回答1:

It seems like the data that is being POSTed to your create route is just a string, instead of a hash of attributes. What does users(:sample_user) return? Is it just an ID, or maybe an ActiveRecord object that is not being serialized correctly? If it's AR, maybe you need to send users(:sample_user).attributes as the payload?



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!