ActionController::ParameterMissing - 参数缺失或值为空:用户:
ActionController::ParameterMissing - param is missing or the value is empty: user:
我正在尝试更新单击按钮阻止或取消阻止时的属性。但它没有在数据库中更新,我收到错误 param is missing or the value is empty: user
。即使传递了用户 ID,我也会收到此错误。我无法找到我的错误发生的地方
这是我的控制器代码:
class UserController < ApplicationController
before_action :set_user, only: [ :edit, :update]
# GET /users
# GET /users.json
def index
@users = User.all
end
def edit
end
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to user_index_path, notice: 'User was successfully blocked.' }
else
format.html { render :index }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:active)
end
end
这是我的查看代码
<h1>
User Details
</h1>
<table class="table">
<thead>
<tr>
<th>Users</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td>
<%= image_tag("/assets/image.jpg", style: "width: 60px; height: 60px;")%>
</td>
<td>
<p class="font-weight-bold"><%= user.name %></p>
<p><%= user.email %></p>
</td>
<td><%= user.active %></td>
<td>
<% if user.active == true %>
<%= button_to "Block", user_path(id: user.id, value: false), class: 'btn btn-outline-dark', :method => :patch %>
<%else%>
<%= button_to "Unblock", user_path(id: user.id, value: true), class: 'btn btn-outline-dark', :method => :patch %>
<%end%>
</td>
</tr>
<% end %>
</tbody>
</table>
我得到的终端日志
Started PATCH "/user/5?value=false" for 127.0.0.1 at 2018-04-12 14:49:57 +0530
Processing by UserController#update as HTML
Parameters: {"authenticity_token"=>"RyL/KkxIbVtZXojmERByarlN1qxXH0gK7moq2w5s4ULketOYGh1im+qPLvND2HkOlVC8gePlFyGCns0lf/Z/uQ==", "value"=>"false", "id"=>"5"}
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 5 ORDER BY `users`.`id` ASC LIMIT 1
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 5 LIMIT 1
Completed 401 Unauthorized in 3ms (ActiveRecord: 0.6ms)
ActionController::ParameterMissing - param is missing or the value is empty: user:
app/controllers/user_controller.rb:31:in `user_params'
app/controllers/user_controller.rb:15:in `block in update'
app/controllers/user_controller.rb:14:in `update'
Started POST "/__better_errors/7f240c65d5c4ef9a/variables" for 127.0.0.1 at 2018-04-12 14:49:57 +0530
您需要 user
参数和 active
字段:
def user_params
params.require(:user).permit(:active)
end
您只发送 value
参数。您需要将 button_to 代码更改为
之类的代码
button_to("Block", user_path(id: user.id),
class: 'btn btn-outline-dark', :method => :patch, params: { user: { active: true}}
(或分别为 active: false
)。
在您的方法 user_params
中,您需要参数 user
。按钮上的链接只有 get 参数 value
,但没有 user
.
get 参数应该叫做 sth。像 user[active]
.
您的阻止按钮应如下所示:
<%= button_to "Block", user_path(id: user.id, 'user[active]': false), class: 'btn btn-outline-dark', :method => :patch %>
当我们没有正确地从视图中传递参数时,就会出现这个错误。
在您的情况下,参数应位于 user
散列中。
在您的私人部分:-
def user_params
params.require(:user).permit(:active)
end
您实际上希望参数包含在 user
散列中。
正如您期望用户哈希中的 active
属性一样,它给您的错误是 param is missing or the value is empty: user
。
表示正在寻找active
属性,传入参数中缺少
例如,应该打你的控制器的参数应该是这样的:-
{"user"=>{"active"=>"true"}, "value"=>"false", "id"=>"5"}
在用户散列中从您的视图页面传递 active 参数,错误就会消失。此外,您可以随时了解有关强参数的更多信息。
我正在尝试更新单击按钮阻止或取消阻止时的属性。但它没有在数据库中更新,我收到错误 param is missing or the value is empty: user
。即使传递了用户 ID,我也会收到此错误。我无法找到我的错误发生的地方
这是我的控制器代码:
class UserController < ApplicationController
before_action :set_user, only: [ :edit, :update]
# GET /users
# GET /users.json
def index
@users = User.all
end
def edit
end
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to user_index_path, notice: 'User was successfully blocked.' }
else
format.html { render :index }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:active)
end
end
这是我的查看代码
<h1>
User Details
</h1>
<table class="table">
<thead>
<tr>
<th>Users</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td>
<%= image_tag("/assets/image.jpg", style: "width: 60px; height: 60px;")%>
</td>
<td>
<p class="font-weight-bold"><%= user.name %></p>
<p><%= user.email %></p>
</td>
<td><%= user.active %></td>
<td>
<% if user.active == true %>
<%= button_to "Block", user_path(id: user.id, value: false), class: 'btn btn-outline-dark', :method => :patch %>
<%else%>
<%= button_to "Unblock", user_path(id: user.id, value: true), class: 'btn btn-outline-dark', :method => :patch %>
<%end%>
</td>
</tr>
<% end %>
</tbody>
</table>
我得到的终端日志
Started PATCH "/user/5?value=false" for 127.0.0.1 at 2018-04-12 14:49:57 +0530
Processing by UserController#update as HTML
Parameters: {"authenticity_token"=>"RyL/KkxIbVtZXojmERByarlN1qxXH0gK7moq2w5s4ULketOYGh1im+qPLvND2HkOlVC8gePlFyGCns0lf/Z/uQ==", "value"=>"false", "id"=>"5"}
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 5 ORDER BY `users`.`id` ASC LIMIT 1
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 5 LIMIT 1
Completed 401 Unauthorized in 3ms (ActiveRecord: 0.6ms)
ActionController::ParameterMissing - param is missing or the value is empty: user:
app/controllers/user_controller.rb:31:in `user_params'
app/controllers/user_controller.rb:15:in `block in update'
app/controllers/user_controller.rb:14:in `update'
Started POST "/__better_errors/7f240c65d5c4ef9a/variables" for 127.0.0.1 at 2018-04-12 14:49:57 +0530
您需要 user
参数和 active
字段:
def user_params
params.require(:user).permit(:active)
end
您只发送 value
参数。您需要将 button_to 代码更改为
button_to("Block", user_path(id: user.id),
class: 'btn btn-outline-dark', :method => :patch, params: { user: { active: true}}
(或分别为 active: false
)。
在您的方法 user_params
中,您需要参数 user
。按钮上的链接只有 get 参数 value
,但没有 user
.
get 参数应该叫做 sth。像 user[active]
.
您的阻止按钮应如下所示:
<%= button_to "Block", user_path(id: user.id, 'user[active]': false), class: 'btn btn-outline-dark', :method => :patch %>
当我们没有正确地从视图中传递参数时,就会出现这个错误。
在您的情况下,参数应位于 user
散列中。
在您的私人部分:-
def user_params
params.require(:user).permit(:active)
end
您实际上希望参数包含在 user
散列中。
正如您期望用户哈希中的 active
属性一样,它给您的错误是 param is missing or the value is empty: user
。
表示正在寻找active
属性,传入参数中缺少
例如,应该打你的控制器的参数应该是这样的:-
{"user"=>{"active"=>"true"}, "value"=>"false", "id"=>"5"}
在用户散列中从您的视图页面传递 active 参数,错误就会消失。此外,您可以随时了解有关强参数的更多信息。