NoMethodError - 控制器操作无法随机工作

时间:2014-08-06 03:00:16

标签: ruby-on-rails

我正在使用Ruby on Rails 4.1创建一个票务预订应用程序作为我的示例项目。三个是三个模型 - 活动,门票和预订。活动有很多门票和预订。门票有很多预订,属于活动。预订属于活动和门票。

预订控制器如下:

class BookingsController < ApplicationController
  before_action :authenticate_user!

  def index
    @event = Event.find(params[:event_id])
    @bookings = @event.bookings.all
  end

  def new
    @event = Event.find(params[:event_id])
    @ticket = @event.tickets.find(params[:ticket_id])
    @booking = Booking.new
  end

  def create
    @event = Event.find(params[:event_id])
    @ticket = @event.tickets.find(params[:ticket_id])
    @booking = @event.bookings.create(booking_params)
    @booking.ticket = @ticket

    Stripe.api_key = Rails.configuration.stripe[:secret_key]
    #token = params[:stripeToken]
    @amount = @booking.total_amount

    begin
      customer = Stripe::Customer.create(
        :email => @booking.email,
        :card  => params[:stripeToken]
        )

      charge = Stripe::Charge.create(
        :customer    => customer.id,
        :amount => @amount,
        :currency => "usd",
        #:card => token
        )
      flash[:notice] = "Thanks for the order"
    rescue Stripe::CardError => e
      flash[:danger] = e.message
    end  
    if @booking.save
        BookingMailer.booking_confirmation_user(@booking).deliver
        redirect_to [@event, @booking]
    else
        render 'new'
    end
  end

  def show
    @event = Event.find(params[:event_id])
    @booking = @event.bookings.find(params[:id])
  end

  def destroy
    @event = Event.find(params[:event_id])
    @booking = @event.bookings.find(params[:id])
    @booking.destroy
    redirect_to event_bookings_path
  end

  private
  def booking_params
    params.require(:booking).permit(:buyer_name, :email, :mobile, :address, :order_quantity, :ticket_id)
  end
end

预订模式:

class Booking < ActiveRecord::Base
  #before_create :check_ticket_count

  belongs_to :event
  belongs_to :ticket
  has_many :charges

  def total_amount
    ticket.ticket_price.to_i * order_quantity.to_i
  end

  def check_ticket_count
    count = ticket.ticket_quantity.to_i - order_quantity.to_i
    ticket.update_attribute(:ticket_quantity, count)
  end

end

门票模型:

class Ticket < ActiveRecord::Base
  belongs_to :event
  has_many :bookings
  belongs_to :user

  before_create :check_start_date
  before_update :check_start_date


    def check_start_date
        if (self.booking_start_date >= DateTime.now) && (self.booking_end_date != DateTime.now)
            self.status = 'Open'
        else
            self.status = 'Closed'
        end
    end

    def maximum_tickets_allowed
        (1..maximum_quantity.to_i).to_a
    end

end

The Bookings Index Page:

<h2>All Bookings</h2>

  <div class="row">
    <div class="col-md-10">
      <table class="table">
        <thead>
              <tr>
                  <th>Buyer Name</th>
                  <th>Email Address</th>
                  <th>Ticket Type</th>
                  <th>No. of Tickets</th>
                  <th>Amount</th>
                  <th></th>
              </tr>
          </thead>
          <tbody>
            <% @event.bookings.each do |booking| %> 
              <tr> 
                  <td><%= booking.buyer_name %></td>
                  <td><%= booking.email %></td>
                  <td><%= booking.ticket.ticket_name %></td>
                  <td><%= booking.order_quantity %></td>
                  <td><%= number_to_currency(booking.total_amount) %></td>                  
                  <td><%= link_to "Delete", event_booking_path(@event, booking), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-link" %></td>
            <% end %>
          </tr>
      </table>
    </div>
    </div>

预订显示页面:

感谢您的购买!

<div class="row">
    <div class="col-md-8">
        <b>Name:</b><p><%= @booking.buyer_name %></p>
        <b>No. of Tickets</b><p><%= @booking.order_quantity %></p>
        <b>Ticket Tier</b><p><%= @booking.ticket.ticket_name %></p>
        <b>Amount Paid:</b><p><%= number_to_currency(@booking.total_amount) %></p>
        <p><% @booking.check_ticket_count %></p>
    </div>

现在,我的问题是预订工作完美。获取,显示所有细节并正确执行模型方法。但是,有时甚至没有对这些文件进行单一更改,我得到“操作控制器异常捕获错误 - 预订中的NoMethodError #index”和“操作控制器异常捕获错误 - 预订中的NoMethodError#show”。罪魁祸首始终是booking.ticket.ticket_name和@ booking.ticket.ticket_name中的ticket_name,其中错误表明它是未定义的方法。

完整跟踪在这里:

app/views/bookings/index.html.erb:21:in `block in _app_views_bookings_index_html_erb___3226472832114682596_70125337010980'
activerecord (4.1.1) lib/active_record/relation/delegation.rb:46:in `each'
activerecord (4.1.1) lib/active_record/relation/delegation.rb:46:in `each'
app/views/bookings/index.html.erb:17:in `_app_views_bookings_index_html_erb___3226472832114682596_70125337010980'
actionview (4.1.1) lib/action_view/template.rb:145:in `block in render'
activesupport (4.1.1) lib/active_support/notifications.rb:161:in `instrument'
actionview (4.1.1) lib/action_view/template.rb:339:in `instrument'
actionview (4.1.1) lib/action_view/template.rb:143:in `render'
actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:55:in `block (2 levels) in render_template'
actionview (4.1.1) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `instrument'
actionview (4.1.1) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:54:in `block in render_template'
actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:62:in `render_with_layout'
actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:53:in `render_template'
actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:17:in `render'
actionview (4.1.1) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.1.1) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.1.1) lib/action_view/rendering.rb:99:in `_render_template'
actionpack (4.1.1) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.1.1) lib/action_view/rendering.rb:82:in `render_to_body'
actionpack (4.1.1) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.1.1) lib/action_controller/metal/renderers.rb:32:in `render_to_body'
actionpack (4.1.1) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.1.1) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.1.1) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/mohan/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.1.1) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.1.1) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.1.1) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.1.1) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.1.1) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.1) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.1) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.1) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.1.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.1) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:676:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.1) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.1) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.1) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.1) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.1) lib/rails/engine.rb:514:in `call'
railties (4.1.1) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/mohan/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/Users/mohan/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/Users/mohan/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

问题是它是正确的字段名称。为什么这些操作在大多数情况下都会起作用,并且在代码中没有一次更改的情况下会随机失败?

1 个答案:

答案 0 :(得分:1)

booking.ticket可能是空白

你可以做到

<td><%= booking.ticket.ticket_name rescue nil %></td>

<td>
  <% unless booking.ticket.blank? %>
    <%= booking.ticket.ticket_name %>
  <% else %>
    No Ticket
</td>