【问题标题】:Ruby on Rails cartRuby on Rails 购物车
【发布时间】:2020-06-05 11:21:33
【问题描述】:

嗨,我是 ruby​​ on rails 的新手,我正在尝试为我的事件应用程序创建一个购物车,我已经完成了大部分工作,但我无法完全弄清楚这部分

  def add_item
    @event = Event.find(params[:cart][:event_id])
    @ticket_types = @event.ticket_types.where(id: params[:cart][:ticket_type_id])

    params[:cart][:ticket_type_id].each_with_index do |ticket_type_id, index|
      ticket_type = @event.ticket_types.find(ticket_type_id)
      if params[:quantity] === 0
      else
        current_cart.line_items.create(
          ticket_type_id: ticket_type.id,
          quantity: params[:cart][:quantity][index]
        )
      end
    end

    redirect_to cart_url
  end

这是我的购物车控制器和用于添加订单项的定义。 预期结果:当用户看到所有各种票种的值预设为“0”时,他们可以选择其中任何一种票种的数量并将其提交到购物车页面。

实际结果:所有票种的值都提交到购物车页面,包括值为“0”的票种。

它们将不会被应用于值为 0 的票证类型,但我不需要查看它们

<% @ticket_types.each do |ticket_type| %>
                     <div class="pb-2">
                       <h5>
                         <%= ticket_type.name %> <%= number_to_currency(ticket_type.cost_ec) %> per ticket
                       </h5>
                       <div class="px-5 ">
                         <div class="input-group mb-3">
                           <%= form.hidden_field :ticket_type_id, value: ticket_type.id, multiple: true %>
                           <%= select_tag('cart[quantity][]', options_for_select(0..ticket_type.limit), { class: 'form-control' }) %>
                           <div class="input-group-append">
                             <button  type="button" class="btn btn-outline-success" data-toggle="modal" data-target="#details">Ticket Details</button>
                             <!-- Modal -->
                             <div class="modal fade" id="details" tabindex="-1" role="dialog" aria-labelledby="details">
                               <div class="modal-dialog modal-dialog-centered" role="document">
                                 <div class="modal-content">
                                   <div class="p-5">
                                     <p>Description <br>
                                     <%= ticket_type.description %></p><br>
                                     <p>Ticket closes at:  <%= ticket_type.end_at.stamp('Friday, 31/12/00 12:59 ') %></p><br>
                                   </div>
                                 </div>
                               </div>
                             </div>
                             <!--end of modal -->
                           </div>
                         </div>
                       </div> 
                     </div>
                   <% end %>

谁能告诉我我在 mt 控制器中的 if 函数做错了什么?

这是我的参数哈希

Started POST "/cart" for ::1 at 2020-06-05 13:45:11 -0400
Processing by CartsController#add_item as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"hl1C56tE6DJIyjJme8wK8Bak5l2icVymCwTe5SA8TCqt3TdtqlCoNzgEj0c2+URpzytG/LF4Xh6fBLfKJhHNTQ==", "cart"=>{"event_id"=>"3", "ticket_type_id"=>["2", "5", "6", "7"], "quantity"=>["5", "4", "3", "0"]}}
  Event Load (0.5ms)  SELECT  "events".* FROM "events" WHERE "events"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
  ↳ app/controllers/carts_controller.rb:21
  TicketType Load (0.4ms)  SELECT  "ticket_types".* FROM "ticket_types" WHERE "ticket_types"."event_id" = $1 AND "ticket_types"."id" = $2 LIMIT $3  [["event_id", 3], ["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/carts_controller.rb:25
  TicketType Load (0.8ms)  SELECT  "ticket_types".* FROM "ticket_types" WHERE "ticket_types"."event_id" = $1 AND "ticket_types"."id" = $2 LIMIT $3  [["event_id", 3], ["id", 5], ["LIMIT", 1]]
  ↳ app/controllers/carts_controller.rb:25
  TicketType Load (0.5ms)  SELECT  "ticket_types".* FROM "ticket_types" WHERE "ticket_types"."event_id" = $1 AND "ticket_types"."id" = $2 LIMIT $3  [["event_id", 3], ["id", 6], ["LIMIT", 1]]
  ↳ app/controllers/carts_controller.rb:25
  TicketType Load (0.5ms)  SELECT  "ticket_types".* FROM "ticket_types" WHERE "ticket_types"."event_id" = $1 AND "ticket_types"."id" = $2 LIMIT $3  [["event_id", 3], ["id", 7], ["LIMIT", 1]]
  ↳ app/controllers/carts_controller.rb:25
Redirected to http://localhost:3000/cart
Completed 302 Found in 18ms (ActiveRecord: 2.6ms)


Started GET "/cart" for ::1 at 2020-06-05 13:45:11 -0400
Processing by CartsController#show as HTML
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb:21
  Cart Load (0.8ms)  SELECT  "carts".* FROM "carts" WHERE "carts"."user_id" = $1 LIMIT $2  [["user_id", 2], ["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb:21
  Rendering carts/show.html.erb within layouts/application
  LineItem Load (0.7ms)  SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = $1  [["cart_id", 1]]
  ↳ app/views/carts/show.html.erb:25
  TicketType Load (0.4ms)  SELECT  "ticket_types".* FROM "ticket_types" WHERE "ticket_types"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ app/views/carts/show.html.erb:27
  Event Load (0.6ms)  SELECT  "events".* FROM "events" WHERE "events"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
  ↳ app/views/carts/show.html.erb:27
  Rendered carts/show.html.erb within layouts/application (39.4ms)
  Rendered layouts/_navbar.html.erb (4.0ms)
  Rendered layouts/_alerts.html.erb (0.4ms)
Completed 200 OK in 297ms (Views: 185.2ms | ActiveRecord: 10.2ms)


Started GET "/cart?reload" for ::1 at 2020-06-05 13:45:12 -0400
Processing by CartsController#show as HTML
  Parameters: {"reload"=>nil}
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb:21
  Cart Load (0.4ms)  SELECT  "carts".* FROM "carts" WHERE "carts"."user_id" = $1 LIMIT $2  [["user_id", 2], ["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb:21
  Rendering carts/show.html.erb within layouts/application
  LineItem Load (0.5ms)  SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = $1  [["cart_id", 1]]
  ↳ app/views/carts/show.html.erb:25
  TicketType Load (0.4ms)  SELECT  "ticket_types".* FROM "ticket_types" WHERE "ticket_types"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ app/views/carts/show.html.erb:27
  Event Load (0.5ms)  SELECT  "events".* FROM "events" WHERE "events"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
  ↳ app/views/carts/show.html.erb:27
  Rendered carts/show.html.erb within layouts/application (27.6ms)
  Rendered layouts/_navbar.html.erb (3.2ms)
  Rendered layouts/_alerts.html.erb (0.5ms)
Completed 200 OK in 270ms (Views: 181.5ms | ActiveRecord: 2.2ms)

【问题讨论】:

    标签: html ruby-on-rails ruby


    【解决方案1】:

    你有几个问题,所以,这是参数哈希:

      Parameters: {"utf8"=>"✓", "authenticity_token"=>"hl1C56tE6DJIyjJme8wK8Bak5l2icVymCwTe5SA8TCqt3TdtqlCoNzgEj0c2+URpzytG/LF4Xh6fBLfKJhHNTQ==", "cart"=>{"event_id"=>"3", "ticket_type_id"=>["2", "5", "6", "7"], "quantity"=>["5", "4", "3", "0"]}}
    

    您可能需要在控制器中允许这些参数,例如:

    def cart_params
      params.require(:cart).permit(:event_id, :ticket_type_id, :quantity)
    end
    

    那么,你的情况:

    if params[:quantity] === 0
    

    正在比较该值的值和对象的类型,因此,它正在这样做:

    string === integer
    

    但您没有使用正确的密钥,应该是:

    if params[:cart][quantity][index] === 0
    

    因为quantity 是一个数组

    您可以通过以下方式解决此问题:

      if cart_params[:cart][quantity][index] === 0
    

    另外,您可以改用unless,并且您可能想要转换该值:

      unless cart_params[:cart][:quantity][index].to_i < 1
    

    更好的是,您还可以在表单的 js 部分进行此验证,这样您就可以避免返回控制器进行此验证

    【讨论】:

    • 我尝试使用您提出的前 2 个建议,但它仍在提交数量为“0”的票证,因为在 js 中使用它我不太确定如何去做。 @mr_sudaca
    • 对不起,我在我的例子中弄错了,应该是unless params[:quantity].to_i &lt; 1而不是unless params[:quantity].to_i &gt; 0
    • unless params[:quantity].to_i &lt; 1dosent 提交任何东西到购物车页面
    • 你能在问题中粘贴参数哈希吗?
    • 我刚刚添加了它们
    猜你喜欢
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 2013-08-19
    相关资源
    最近更新 更多