我有一个与订单状态相关联的订单模型。 在编辑视图中,我在select标签上有一个带有订单状态选项的form_for。 渲染视图时,我可以看到select标签中预先选择的当前订单状态。如果我更改它并提交表单,我会收到此错误: ActionController :: OrdersController#update中的ParameterMissing 当我用text_field替换select标签时,我可以更改订单状态ID并成功提交表单。我尝试将其添加到表单:: method => :put但是给了我一个路由错误。
我研究了我的问题,但我无法让它发挥作用。 我研究过的链接:
rails 4 form_for with f.select isn't passing correct params, getting `param is missing` error
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
Rails ActionController::ParameterMissing in QuestionsController#update
订单编辑视图:
<%= form_for(@order) do |f| %>
<% if @order.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@order.errors.count, "error") %> prohibited this contact from being saved:</h2>
<ul>
<% @order.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<h3>Order Id <%= @order.id %> | <%= @order.uuid %></h3>
<h3>Order Total: <%= number_to_currency(@order.total) %></h3>
</div>
<br>
<div class="form-group">
<%= f.label :order_status_id %><br>
<%= select_tag :order_status_id, options_from_collection_for_select(OrderStatus.all, :id, :name, @order.order_status.id),:class => "form-control" %>
</div>
<div class="actions">
<%= f.submit "submit", :method => :put, :class => "btn btn-primary" %>
</div>
<% end %> <!-- Order details -->
orders_controller.rb
def edit
authorize! :read, @orders
@order = Order.find_by_uuid(params[:id])
@reservations = @order.reservations
end
def update
@order = Order.find_by_uuid(params[:id])
@order_status = @order.order_status_id
respond_to do |format|
if @order.update(order_params)
format.html { redirect_to @order, notice: 'Order was successfully updated.' }
format.json { render :show, status: :ok, location: @order }
else
format.html { render :edit }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
def order_params
params.require(:order).permit(:subtotal, :tax, :total, :order_status_id, :uuid, :id, :shipping)
end
order.rb belongs_to:order_status
order_status.rb has_many:订单
错误:
param is missing or the value is empty: order
Rails.root: C:/Users/Antonio/Desktop/RegiSportV01/regisports
Application Trace | Framework Trace | Full Trace
app/controllers/orders_controller.rb:114:in `order_params'
app/controllers/orders_controller.rb:86:in `block in update'
app/controllers/orders_controller.rb:85:in `update'
Request
Parameters:
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"DhUNbbAz/VBLoRmLN8sKf4Oq/SEWJt38HlWMQW/w+zjM3CZ0Sml4Ulv+dzwYDdv1fMN42sjF+Y4BcMwc5+sRyg==",
"order_status_id"=>"2",
"commit"=>"submit",
"id"=>"ebce4e1c-d81c-4171-aafa-a9e23b392525"}
答案 0 :(得分:1)
我建议您更改select_tag
帮助程序:
<%= f.select_tag :order_status_id, options_from_collection_for_select(OrderStatus.all, :id, :name, @order.order_status.id),:class => "form-control" %>
^^^^^^^^^^
这个技巧应该包括order_status_id
使用密钥order
的参数。
或者您可以更改select_tag
帮助程序的名称。
<%= select_tag 'order[order_status_id]', options_from_collection_for_select(OrderStatus.all, :id, :name, @order.order_status.id),:class => "form-control" %>
答案 1 :(得分:0)
我之前也确实在努力。
自从我开始使用SimpleForm以来,我没有遇到过这些问题。
它需要一些设置,但您的表单变得更加可读和清晰。
此字段如下所示:
<%= f.input :order_status_id, collection: OrderStatus.all %>
对于像这样的选择字段,我真的建议使用一些Javascript来使用户更容易。我有一个名为Select2的库的丰富经验,可以包含在gem中。