我遇到此错误
param is missing or the value is empty: order
我有Admin::OrdersController
和Clients::OrdersController
用户从客户部分创建订单
管理员可以从管理部分更改订单状态
我的订单状态在订单模型中设置如下:
enum status: [:pending, :paid, :sent, :cancelled, :refunded]
要在我的 admin / orders / show.html.erb
中更新此状态我添加了一个表格:
<%= form_tag(@order , url: admin_order_path(@order) )do %>
<%= hidden_field_tag :status, 2 %>
<%= hidden_field_tag :sub_total, @order.sub_total %>
<%= hidden_field_tag :user_id, @order.user.id %>
<%= hidden_field_tag :token, @order.token %>
<%= submit_tag "Order sent" , class: "btn btn-success"%>
<% end %>
admin / orders_controller.rb
class Admin::OrdersController < ApplicationController
def index
@orders = Order.all
@orders = @orders.filter_by_status(params[:status]) if params[:status]
end
def show
@order = Order.find(params[:id])
end
def edit
@order = Order.find(params[:id])
end
def update
@order = Order.find(params[:id])
@order.update_attributes(order_params)
redirect_to @order
end
private
def order_params
params.require(:order).permit(:status, :user_id, :token , :sub_total)
end
end
请求参数:
Request
Parameters:
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"EmHbw3XMNr1hdhQKXGoMeoSQY82kxLty1M9pJZBHngJgnHY5T9I54gW+iwlww+v+WmmdSXSELLOeNRyCwIjgVQ==",
"status"=>"2",
"sub_total"=>"30",
"user_id"=>"1",
"token"=>"70717163067e1c9d",
"commit"=>"Order sent",
"id"=>"7"}
日志
Started PATCH "/admin/orders/7" for 127.0.0.1 at 2019-01-07 16:19:43 +0100
[1m[36mUser Load (0.4ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
↳ /Users/nellyduclos/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Processing by Admin::OrdersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"EmHbw3XMNr1hdhQKXGoMeoSQY82kxLty1M9pJZBHngJgnHY5T9I54gW+iwlww+v+WmmdSXSELLOeNRyCwIjgVQ==", "status"=>"2", "sub_total"=>"30", "user_id"=>"1", "token"=>"70717163067e1c9d", "commit"=>"Order sent", "id"=>"7"}
[1m[36mOrder Load (0.3ms)[0m [1m[34mSELECT "orders".* FROM "orders" WHERE "orders"."id" = $1 LIMIT $2[0m [["id", 7], ["LIMIT", 1]]
↳ app/controllers/admin/orders_controller.rb:17
Completed 400 Bad Request in 1ms (ActiveRecord: 0.3ms)
ActionController::ParameterMissing (param is missing or the value is empty: order):
app/controllers/admin/orders_controller.rb:25:in `order_params'
app/controllers/admin/orders_controller.rb:18:in `update'
答案 0 :(得分:1)
您使用的hidden_field_tag
不能正确地为参数加上order[...]
前缀,因为params[:order]
为空。
改为使用f.hidden_field
:
<%= form_tag(@order , url: admin_order_path(@order) )do |f| %>
<%= f.hidden_field :status, value: 2 %>
<%= f.hidden_field :sub_total, :sub_total %>
<%= f.hidden_field :user_id, :id %>
<%= f.hidden_field :token, :token %>
<%= f.submit "Order sent" , class: "btn btn-success"%>
<% end %>