无法从控制器访问强参数

时间:2019-01-11 21:15:45

标签: ruby-on-rails activerecord

我的王国为这个树桩的答案。我的应用程序从控制器发出错误,提示它找不到从视图传递的参数。我进一步研究了控制台,并添加了一些puts语句。视图正确传递了参数值,并且两个参数都在强参数中列出,但控制器找不到它们。这是代码和控制台日志输出。

查看

<%= form_for @order_item, class: "form-control", remote: false do |f| %>
<div class="artist-event-item">
    <div class="row">
        <div class="artist-event-item-info col-sm-8">
            <h3>Ticket</h3>
            <ul class="row">
                <li class="col-sm-6">
                    <span>Venue</span>
                    <%= product.section.venue.address_street_number %> <%= product.section.venue.address_street_name %> 
                    <span class="location"><%= product.section.venue.address_city %>, <%= product.section.venue.address_state_code %></span>
                 </li>
                <li class="col-sm-6">
                    <span>Section</span>Section <%= product.section.name%> - <%=product.section.description %>
                </li>
            </ul>
        </div>
        <div class="artist-event-item-price col-sm-4">
            <span>Ticket Price</span>
            <strong><%= number_to_currency(product.price) %>
            </strong>
            <div class="input-group col-xs-2 center">
                <%= f.number_field :quantity, value: 0, min: 0, class: "select.form-group-sm    form-control" %>
                <%= f.hidden_field :product_id, value: product.id %>
                <%= f.button "Add to Cart", data: { remote: true, disable_with: "<i class = 'fa fa-spinner fa-spin'></i> Updating cart..." }, class: "btn btn-add-to-cart" %>
            </div>
        </div>
    </div>
</div>
      

控制器

private

def set_product

  puts "quantity is #{params[:quantity]}"
  puts "product id is #{params[:product_id]}"
  @product = @account.products.find(params[:product_id])
end

def order_item_params
  params.require(:order_item).permit(:quantity, :product_id)
end

结束

从Rails控制台输出

Started POST "/order_items" for 127.0.0.1 at 2019-01-11 16:42:36 -0400
Processing by OrderItemsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "order_item"=>{"quantity"=>"1", "product_id"=>"13"}, "button"=>""}
Account Load (1.1ms)  SELECT  "accounts".* FROM "accounts" WHERE "accounts"."subdomain" = $1 LIMIT $2  [["subdomain", "[FILTERED]"], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:19
CACHE Account Load (0.1ms)  SELECT  "accounts".* FROM "accounts" WHERE "accounts"."subdomain" = $1 LIMIT $2  [["subdomain", "[FILTERED]"], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:23`

quantity is 
product id is 
Completed 404 Not Found in 9ms (ActiveRecord: 1.2ms)
ActiveRecord::RecordNotFound (Couldn't find Product without an ID):
app/controllers/order_items_controller.rb:51:in set_product'

1 个答案:

答案 0 :(得分:1)

您尝试在此处找到产品:

@product = @account.products.find(params[:product_id])

但是正如您所看到的,您的参数不包括params[:product_id]

Parameters: {
  "utf8"=>"✓", 
  "authenticity_token"=>"[FILTERED]", 
  "order_item"=>{"quantity"=>"1", "product_id"=>"13"}, 
  "button"=>""
}

您可以这样做:

@product = @account.products.find(params[:order_item][:product_id])

如果:product_id参数中的:order_item是您要寻找的参数。

您也可以这样做:

@product = @account.products.find(order_item_params[:product_id])