无法在一个页面上呈现购物车

时间:2016-11-14 13:54:46

标签: ruby-on-rails ruby

所以我试图在我的门票页面上渲染我的购物车,但它不起作用。它正在处理其他每一页,但不是这一页。

_cart.html.erb:

<div class="modal-content">
  <% if current_page?(controller: 'orders', action: 'new') == false %>
  <h4>Your Cart</h4>
  <% end %>
  <ul class="collection">
    <%= render(cart.line_items) %>
    <li class="collection-item"><span style="float:right"></span>Total: <%= number_to_currency(cart.total_price) %></li>
  </ul>
</div>
<% if current_page?(controller: 'orders', action: 'new') == false %>
<div class="modal-footer">
  <a href="/checkout" class="modal-action modal-close waves-effect waves-green btn-flat">Checkout</a><%= button_to 'Empty cart', cart, method: :delete, class: "modal-action modal-close waves-effect waves-green btn-flat", data: { confirm: 'Are you sure?' } %>
</div>
<% end %>

_line_item.html.erb:

<li class="collection-item"><%= line_item.quantity %> &times; <%= TicketType.find(line_item.tickettype_id).name %> for <%= Movie.find(line_item.showtime.movie_id).title %> @ <%= line_item.showtime.time.strftime("%I:%M %p") %> - <%= Theater.find(line_item.showtime.theater_id).name %> - <%= number_to_currency(line_item.total_price) %></li>

当它尝试渲染line_item部分时,它表示line_item.tickettype_id为nil,由于某种原因在ticket.html.erb上呈现时,它是,但在其余页面上它可以正常工作。

tickets.html.erb:

  <div class="container">
  <br>
    <h3><%= @movie.title %> at <%= @showtime.time.strftime("%I:%M %p") %> on <%= @showtime.date.strftime('%a %b %d') %></h3>


    <div class="row">
        <div class="col s4 theater-info">
            <img src="<%= image_url(@movie.image_url) %>" height="350px" style="margin-left:50%;transform: translateX(-50%)" class="materialboxed">
          <h5><%= @movie.title %> <span class="new red badge" data-badge-caption=""><%= @movie.rating %></span><span class="new red badge" data-badge-caption=""><%= @movie.runtime %></span></h5>
            <p>Description: <%= @movie.description %></p>
            <p>Actors: <%= @movie.actors %></p>
            <p>Genre: <%= @movie.genre %></p>
        </div>
        <div class="col s5 more-info">
           <%= render "tickets" %>
        </div>
        <div class="col s3 showtime-info">
            <div id="cart">
              <%= render @cart %>
            </div>
        </div>

    </div>


    </div>
    <p id="notice"><%= notice %></p>

_tickets.html.erb(因为它在tickets.html.erb上呈现):

<% for ticketType in selectTickets(@showtime.theater_id)%>
<div class="card-panel red">
<div class="row">
<span><%= ticketType.name %> </span>
<span>(<%= ticketType.description %>) </span>
<span><%= number_to_currency(ticketType.price) %> </span>
<%= form_for @line_itemssss, :url => line_items_path, remote: true do |f| %>
    <%= f.select :quantity, options_for_select([[0,0], [1,1], [2,2], [3,3], [4,4], [5,5], [6,6], [7,7], [8.8], [9,9], [10,10]]) %>
    <%= f.hidden_field :tickettype_id, :value => ticketType.id %>
    <%= f.hidden_field :showtime_id, :value => @showtime.id %>
    <%= f.submit %>
<% end %>
<%= button_to('Add to Cart', line_items_path(tickettype_id: ticketType.id, showtime_id: @showtime.id, quantity: 1), remote: true, :class => "btn waves-effect waves-light") %>
</div></div>
<% end %>

pages_controller.rb(对于tickets.html.erb):

class PagesController < ApplicationController
  include SessionsHelper
  include CurrentCart
  before_action :set_cart

  def tickets
    @showtime = Showtime.find(params[:id])
    @movie = Movie.find(@showtime.movie_id)
    @line_itemssss = @cart.add_ticket(params[:showtime_id], params[:tickettype_id], params[:quantity])

  end
end

完整的错误跟踪:

activerecord (5.0.0.1) lib/active_record/core.rb:173:in `find'
app/views/line_items/_line_item.html.erb:1:in `_app_views_line_items__line_item_html_erb___1395406200177303248_69961294673900'
actionview (5.0.0.1) lib/action_view/template.rb:158:in `block in render'
activesupport (5.0.0.1) lib/active_support/notifications.rb:166:in `instrument'
actionview (5.0.0.1) lib/action_view/template.rb:348:in `instrument'
actionview (5.0.0.1) lib/action_view/template.rb:156:in `render'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:438:in `block in collection_with_template'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:434:in `map'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:434:in `collection_with_template'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:327:in `block (2 levels) in render_collection'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer/collection_caching.rb:13:in `cache_collection_render'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:326:in `block in render_collection'
actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:42:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:41:in `instrument'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:319:in `render_collection'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:308:in `render'
actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:47:in `render_partial'
actionview (5.0.0.1) lib/action_view/helpers/rendering_helper.rb:35:in `render'
app/views/carts/_cart.html.erb:6:in `_app_views_carts__cart_html_erb___847962542900355211_69961295413560'
actionview (5.0.0.1) lib/action_view/template.rb:158:in `block in render'
activesupport (5.0.0.1) lib/active_support/notifications.rb:166:in `instrument'
actionview (5.0.0.1) lib/action_view/template.rb:348:in `instrument'
actionview (5.0.0.1) lib/action_view/template.rb:156:in `render'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:343:in `render_partial'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:311:in `block in render'
actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:42:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:41:in `instrument'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:310:in `render'
actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:47:in `render_partial'
actionview (5.0.0.1) lib/action_view/helpers/rendering_helper.rb:35:in `render'
app/views/pages/tickets.html.erb:19:in `_app_views_pages_tickets_html_erb__2769498277689600672_69961298930760'
actionview (5.0.0.1) lib/action_view/template.rb:158:in `block in render'
activesupport (5.0.0.1) lib/active_support/notifications.rb:166:in `instrument'
actionview (5.0.0.1) lib/action_view/template.rb:348:in `instrument'
actionview (5.0.0.1) lib/action_view/template.rb:156:in `render'
actionview (5.0.0.1) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:42:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:41:in `instrument'
actionview (5.0.0.1) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (5.0.0.1) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (5.0.0.1) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (5.0.0.1) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (5.0.0.1) lib/action_view/rendering.rb:103:in `_render_template'
actionpack (5.0.0.1) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (5.0.0.1) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (5.0.0.1) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (5.0.0.1) lib/action_controller/metal/renderers.rb:144:in `render_to_body'
actionpack (5.0.0.1) lib/abstract_controller/rendering.rb:26:in `render'
actionpack (5.0.0.1) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (5.0.0.1) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'
activesupport (5.0.0.1) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (5.0.0.1) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:43:in `render'
actionpack (5.0.0.1) lib/action_controller/metal/implicit_render.rb:36:in `default_render'
actionpack (5.0.0.1) lib/action_controller/metal/basic_implicit_render.rb:4:in `block in send_action'
actionpack (5.0.0.1) lib/action_controller/metal/basic_implicit_render.rb:4:in `tap'
actionpack (5.0.0.1) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.0.0.1) lib/abstract_controller/base.rb:188:in `process_action'
actionpack (5.0.0.1) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.0.0.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.0.1) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.0.0.1) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.0.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.0.1) lib/abstract_controller/base.rb:126:in `process'
actionview (5.0.0.1) lib/action_view/rendering.rb:30:in `process'
actionpack (5.0.0.1) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.0.1) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:725:in `call'
rack (2.0.1) lib/rack/etag.rb:25:in `call'
rack (2.0.1) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.1) lib/rack/head.rb:12:in `call'
rack (2.0.1) lib/rack/session/abstract/id.rb:222:in `context'
rack (2.0.1) lib/rack/session/abstract/id.rb:216:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.0.0.1) lib/active_record/migration.rb:552:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
web-console (3.1.1) lib/web_console/middleware.rb:131:in `call_app'
web-console (3.1.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.1.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.1.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.0.1) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `block in tagged'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `tagged'
railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.1) lib/rack/method_override.rb:22:in `call'
rack (2.0.1) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.0.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
railties (5.0.0.1) lib/rails/engine.rb:522:in `call'
puma (3.4.0) lib/puma/configuration.rb:224:in `call'
puma (3.4.0) lib/puma/server.rb:569:in `handle_request'
puma (3.4.0) lib/puma/server.rb:406:in `process_client'
puma (3.4.0) lib/puma/server.rb:271:in `block in run'
puma (3.4.0) lib/puma/thread_pool.rb:114:in `block in spawn_thread'

导致line_item无法仅在一个页面上正确呈现的原因是什么?如何解决这个问题?

Cart.rb:

class Cart < ApplicationRecord
    has_many :line_items, dependent: :destroy

  def add_ticket(showtime_id, tickettype_id, quantity)
    current_item = line_items.find_by(showtime_id: showtime_id, tickettype_id: tickettype_id)
    if current_item
      current_item.quantity += quantity
    else
      current_item = line_items.build(showtime_id: showtime_id, tickettype_id: tickettype_id, quantity: quantity)
    end
    current_item
  end

  def total_price
    line_items.to_a.sum { |item| item.total_price }
  end
end

0 个答案:

没有答案