我正在将一个功能应用程序从 Ruby 1.8.7 Rails 3 升级到 Ruby 3 Rails 7:这是一个相当长的旅程,我即将完成。但我有一个订单流程,升级后没有运行,我很难调试。订单流程采用多步骤形式,通过部分和我的
Order controller
中的创建函数呈现。
在多步骤表格的第一步中,您必须输入运输详细信息。当尝试进入下一步时,我在服务器日志中收到以下错误消息:
Unpermitted parameters: :authenticity_token, :order, :commit. Context: ... etc
,并且所有验证都失败的注释显示在我的网站中。
Started POST "/orders" for ::1 at 2022-02-22 17:24:01 +0100
Processing by OrdersController#create as HTML
Parameters: {"authenticity_token"=>"[FILTERED]", "order"=>{"email"=>"[email protected]", "phone_number"=>"1234567", "ship_to_first_name"=>"John", "ship_to_last_name"=>"Doe", "ship_to_address"=>"Pennsylvania Avenue 12", "ship_to_city"=>"Houston", "ship_to_postal_code"=>"12345", "land_id"=>"112", "shipping_service_id"=>"50"}, "commit"=>"Continue"}
Cart Load (0.3ms) SELECT `carts`.* FROM `carts` WHERE `carts`.`id` = 4 LIMIT 1
↳ app/controllers/application_controller.rb:66:in `initialize_cart'
Unpermitted parameters: :authenticity_token, :order, :commit. Context: {controller: OrdersController, action: create, request: #<ActionDispatch::Request:0x00007fee489e8e30>, params: {"authenticity_token"=>"[FILTERED]", "order"=>{"email"=>"[email protected]", "phone_number"=>"1234567", "ship_to_first_name"=>"John", "ship_to_last_name"=>"Doe", "ship_to_address"=>"Pennsylvania Avenue 12", "ship_to_city"=>"Houston", "ship_to_postal_code"=>"12345", "land_id"=>"112", "shipping_service_id"=>"50"}, "commit"=>"Continue", "controller"=>"orders", "action"=>"create"} }
CartItem Load (0.4ms) SELECT `cart_items`.* FROM `cart_items` WHERE `cart_items`.`cart_id` = 4
↳ app/models/cart.rb:86:in `inject'
....
这与旧应用程序中的过程相同。
Started POST "/orders" for 127.0.0.1 at Tue Feb 22 10:02:12 +0100 2022
Processing by OrdersController#create as HTML
Parameters: {"authenticity_token"=>"sometoken", "order"=>{"email"=>"[email protected]", "ship_to_first_name"=>"John", "ship_to_address"=>"Pennsylvania Avenue 12", "ship_to_city"=>"Houston", "land_id"=>"112", "ship_to_last_name"=>"Doe", "ship_to_postal_code"=>"12345", "phone_number"=>"1234567", "shipping_service_id"=>"1"}, "commit"=>"Continue", "utf8"=>"✓"}
Cart Load (0.3ms) SELECT `carts`.* FROM `carts` WHERE `carts`.`id` = ? LIMIT 1 [["id", 6255]]
CartItem Load (0.8ms) SELECT `cart_items`.* FROM `cart_items` WHERE `cart_items`.`cart_id` = 6255
ActiveShippingHub Load (0.3ms) SELECT `active_shipping_hubs`.* FROM `active_shipping_hubs` LIMIT 1
(0.5ms) SELECT MAX(`cart_items`.`length`) AS max_id FROM `cart_items` WHERE `cart_items`.`cart_id` = 6255
(0.5ms) SELECT MAX(`cart_items`.`width`) AS max_id FROM `cart_items` WHERE `cart_items`.`cart_id` = 6255
Rendered shared/_error_messages.html.erb (0.1ms)
Land Load (0.6ms) SELECT `lands`.* FROM `lands` WHERE `lands`.`id` = 112 LIMIT 1
ShippingService Load (0.5ms) SELECT `shipping_services`.* FROM `shipping_services` WHERE `shipping_services`.`id` = 1 LIMIT 1
ProductVariant Load (0.3ms) SELECT `product_variants`.* FROM `product_variants` WHERE `product_variants`.`id` = 14 LIMIT 1
Image Load (0.3ms) SELECT `images`.* FROM `images` WHERE `images`.`id` = 174 LIMIT 1
Rendered orders/_paymentoptions_step.html.erb (10.6ms)
Rendered orders/new.html.erb within layouts/application (14.0ms)
Rendered layouts/_header.html.erb (0.1ms)
Rendered layouts/_footer.html.erb (0.5ms)
Completed 200 OK in 68ms (Views: 20.9ms | ActiveRecord: 26.9ms)
我的订单创建操作始于
def create
session[:order_params].deep_merge!(order_params) if params[:order]
@order = Order.new(session[:order_params])
@shipping_services = @cart.available_shipping_services.joins(:lands).where(lands: {id: @order.land_id})
@order.customer_ip = request.remote_ip
populate_order
@order.current_step = session[:order_step]
...
我已在同一控制器中将 order_params 设置为强参数:
...
private
def order_params
params.permit(:bill_to_address, :bill_to_city, :bill_to_first_name, :bill_to_last_name, :bill_to_land, :bill_to_land_id, :bill_to_postal_code, :date_payment_reminder, :email, :EULA, :express_token, :land_id, :payment, :date_payment_reminder, :phone_number, :signupnewsletter, :ship_to_address, :ship_to_city, :ship_to_first_name, :ship_to_last_name, :ship_to_postal_code, :shipping_service, :shipping_service_id, :shipping_date, :tracking_number, :order_status, :order_status_id, :stripe_card_token, :TOS)
end
end
我不确定为什么会出现此错误。在较新版本的 Rails 中使用 Strong_parameters 或现在验证关联的方式?
当我关闭
Order model
中的所有验证时,我的日志中仍然收到相同的消息,但收到的消息是,shipping_land、shipping_service 和 bill_to_land 字段存在问题:我的 Order model
的三个关联。
我的
Order model
中的关联设置如下:
# Associations
belongs_to :bill_to_land, class_name: "Land", foreign_key: :bill_to_land_id
belongs_to :land, foreign_key: :land_id
belongs_to :order_status
belongs_to :shipping_service
多步形式设置于
orders/new.html.erb
<%= form_for @order do |f| %>
<%= render "#{@order.current_step}_step", :f => f %>
<%= f.submit "Continue" unless @order.payment_options_step? || @order.billing_step? || @order.creditcard_options_step? || @order.last_step? %>
在多步骤形式中,我仅收集数据并在会话中的步骤之间踢出这些数据。订单条目仅在最终提交后创建。
为什么我会收到此错误消息?如何调试订单会话?写入了什么数据...
我希望有人能让我走上正确的方向。
private
def order_params
params.require(:order).permit(:bill_to_address, :bill_to_city, :bill_to_first_name, :bill_to_last_name, :bill_to_land,
:bill_to_land_id, :bill_to_postal_code, :date_payment_reminder, :email, :EULA, :express_token, :land_id, :payment, :date_payment_reminder, :phone_number, :signupnewsletter, :ship_to_address, :ship_to_city, :ship_to_first_name, :ship_to_last_name, :ship_to_postal_code, :shipping_service, :shipping_service_id, :shipping_date, :tracking_number, :order_status, :order_status_id, :stripe_card_token, :TOS)
end
end
就我而言,我收到了相同的消息
Unpermitted parameters: :authenticity_token, :commit
,但它被误导了,问题实际上出在其中一个表单参数中。
关键是在控制台/调试器/puts 中运行
params.permit
命令并查找 permitted: false
。
在我的例子中,是一个
hash
值(表情符号)触发了permitted: false
:
#<ActionController::Parameters {"authenticity_token"=>"XXX", "text"=>"aadsfd", "emojis"=>#<ActionController::Parameters {"star"=>0, "heart"=>0, "emoji-smile"=>0, "hand-thumbs-up"=>0} permitted: false>, "is_accepted"=>"1", "satisfaction_level"=>"4", "feedback"=>"", "hire_again"=>"1", "commit"=>"Send Reaction", "controller"=>"front/developer_reactions", "action"=>"create", "play_session_id"=>"1b280074-9ac8-4390-8f6c-fa3a6e3a2338"} permitted: false>