我有一个带有标准 CRUD 方法的用户 Controller 。其中一种方法是更新。
def update
if @user.update(user_params)
redirect_to @user, notice: "Account Successfully updated"
else
render :edit
end
end
然后我有了 rails 4 strong params 方法:
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, :role_ids => [])
end
我的观点是这样的:
<% if is_admin?(current_user) %>
<%= hidden_field_tag "user[role_ids][]", nil %>
<% Role.all.each do |role|%>
<%= check_box_tag "user[role_ids][]", role.id, @user.role_ids.include?(role.id) %>
<%= role.name %><br>
<% end %>
<% end%>
</br></br>
<footer>
<nav>
<%= link_to "Update User", user_path(@user), :method => :put%>
</nav>
</footer>
当我单击 View 中的更新用户链接时。我收到此错误:
ActionController::ParameterMissing in UsersController#update
param not found: user
错误的突出显示行是:
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, :role_ids => []) end
我似乎无法解释为什么我会收到此错误,因为强参数条件已满足。
有什么想法吗?
请求参数:
Started PUT "/users/4" for 127.0.0.1 at 2014-06-18 16:33:58 -0400
Processing by UsersController#update as HTML
Parameters: {"authenticity_token"=>"1JONmLnMcf2A/2y9boXmcPG5UiwahyR0loLfw+lshco=", "id"=>"4"}
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1
User Load (0.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1
CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1 [["id", 4]]
Completed 400 Bad Request in 2ms
ActionController::ParameterMissing (param not found: user):
app/controllers/users_controller.rb:49:in `user_params'
app/controllers/users_controller.rb:32:in `update'
最佳答案
这是因为您没有将参数嵌套在“user”键中。
.require(:user) 正在寻找这样的散列:
{"user"=>{params}}
表单不正确,发送前必须添加用户 key 。
<%= form_for @user, url: {action: "update"} do |f| %>
#form items.
<%= f.submit "Create" %>
<% end %>
编辑:
您可以将此操作的参数重新定义为:
private
def user_params
params.permit(:name, :email, :password, :password_confirmation, :role_ids => [])
end
关于ruby-on-rails - Rails 4 错误:ActionController::ParameterMissing in UsersController#update,找不到参数:用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24293649/