无法在视图中使用模型方法

时间:2014-07-23 17:08:31

标签: ruby-on-rails ruby

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

票证控制器如下所示:

class TicketsController < ApplicationController
def index
    @event = Event.find(params[:event_id])
    @tickets = @event.tickets.all
end

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

def new
    @event = Event.find(params[:event_id])
    @ticket = Ticket.new
end

def create
    @event = Event.find(params[:event_id])
    @ticket = @event.tickets.create(ticket_params)
    if @ticket.save
        redirect_to [@event, @ticket]
    else
        render 'new'
    end
end

def edit
    @event = Event.find(params[:event_id])
    @ticket= @event.tickets.find(params[:id])
end

def update
    @event = Event.find(params[:event_id])
    @ticket = @event.tickets.find(params[:id])

    if @ticket.update(ticket_params)
        redirect_to [@event, @ticket]
    else
        render 'edit'
    end
end

def destroy
    @event = Event.find(params[:event_id])
    @ticket = @event.tickets.find(params[:id])
    @ticket.destroy
    redirect_to event_tickets_path
end


private

def ticket_params
    params.require(:ticket).permit(:ticket_name, :booking_start_date, :booking_end_date, :ticket_price, :ticket_quantity, :minimum_quantity, :maximum_quantity, :terms_conditions, :more_information)
end

票证模型如下所示

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

 def maximum_tickets_allowed
    max = ticket.maximum_quantity.to_i
    self.maximum_quantity = (1..max).to_a
 end

   end

Tickets show文件如下所示:

<h2>Tickets</h2>

<p><%= @ticket.ticket_name %></p>
<p><%= @ticket.booking_start_date %></p>
<p><%= @ticket.booking_end_date %></p>
<p><%= @ticket.maximum_tickets_allowed %></p>

maxium_quantity是一个人可以预订的最大票数,我在此尝试将其转换为要在collection_select中使用的数组。

现在,如果我使用<%= @ticket.maximum_tickets_allowed %>,我会获得undefined method maximum_tickets_allowed&#39; error. I tried using self.maximum_tickets_allowed`,但它不起作用。我哪里错了?

错误的完整堆栈跟踪:

activemodel (4.1.1) lib/active_model/attribute_methods.rb:435:in `method_missing'
activerecord (4.1.1) lib/active_record/attribute_methods.rb:206:in `method_missing'
app/views/tickets/show.html.erb:6:in `_app_views_tickets_show_html_erb__2112154808966627486_70166705061960'
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: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'
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'

2 个答案:

答案 0 :(得分:1)

您在方法中调用ticket.maximum_quanitity,因为您使用的是故障单实例方法,它不仅仅是maximum_quantity吗?

答案 1 :(得分:0)

要考虑的一些事情。

从评论中,听起来@ticket变量可能是零?我不确定@event.tickets.find(params[:id])是否会抛出ActiveRecord::RecordNotFoundError Ticket.find之类的event_id。你为控制器获取数据的方式有点像Rails方式。 Ticket和Event之间存在一对多的关系。无需将idTicketsController#show传递给class TicketsController < ApplicationController def show @ticket = Ticket.find params[:id] @event = @ticket.event end end 方法。只需传递票证ID:

Ticket.find

如果故障单不存在,使用ActiveRecord::RecordNotFoundError会引发Ticket.find。如果我没记错的话,Rails中的默认控制器错误处理应该捕获该特定异常并呈现404 Not Found页面,因此在@event之后,您可以确保拥有Ticket对象。

接下来,如果您仍需要@ticket.event实例变量,请使用ActiveRecord关系通过class Ticket < ActiveRecord::Base belongs_to :event has_many :bookings def maximum_tickets_allowed (1..self.maximum_quantity).to_a end end 获取它。

现在模型本身需要以下代码:

maximum_tickets_allowed

def maximum_tickets_allowed Range.new(1, maximum_quantity).to_a end 方法应该只返回一个数组。我还想知道方法调用作为Range的结束值是否正在绊倒Ruby解释器。您可以尝试直接实例化Range对象:

<h2>Tickets</h2>

<p><%= @ticket.ticket_name %></p>
<p><%= @ticket.booking_start_date %></p>
<p><%= @ticket.booking_end_date %></p>
<p><%= @ticket.maximum_tickets_allowed %></p>

视图保持不变:

show

编辑#1:正如上面提到的评论一样,如果最近添加了数据库中的maximum_quantity列,请确保您已运行所有Active Record迁移,否则该方法将当Rails应用程序启动时,ActiveRecord不会自动生成。

编辑#2:在您的find方法中,您正在查找事件,然后查找所有故障单并只获取该事件可用的其中一张故障单。由于您已获得该ID,然后从故障单对象中获取该事件,因此这不如获取故障单有效。

我没有找到有关ActiveRecord集合的{{1}}方法的任何文档,因此我无法验证其行为。