未定义方法`to_i'用于#<trainerrate:0x007fd61f5b1150> </trainerrate:0x007fd61f5b1150>

时间:2014-03-01 16:51:46

标签: ruby-on-rails ruby mongodb mongoid

我是从嵌套模型中构建复选框,但不知怎的,我已经遇到了这个问题,它已经使用类似的形式工作得很好但是它被困在这里,从什么我可以假设它是以某种方式将我的模型主键转换为整数,而我使用Mongoid并且我的对象键是字符串,这里是详细信息,这方面的任何帮助/建议都非常受欢迎,我会马上接受答案。

错误:

Showing _form.html.erb where line #31 raised:

undefined method `to_i' for #<TrainerRate:0x007fd61d9d7470>

to.check_box行出现错误。 查看代码:

 <%= f.fields_for :booking_details do |to| %>

            <% ScheduleSlot.all.each do |tr| %>
              <tr>
                <td><%= tr.hour_from %></td>
                <td>$<%= @user.peak_rate %>/Hour</td>
                <% ScheduleSlotDay.all.each do |scd| %>

                    <% ob = @trainer_rates.where(:schedule_slot_day=>scd._id,:schedule_slot=>tr._id).first %>
                    <td> <label>  <%= to.check_box :trainer_rate %>$<%= ob.rate %>/hr</label></td>
                <% end

                %>
              </tr>
          <% end %>

在控制器中,如果我将trainer_rate:f._id更改为1,它可以很好地调出复选框,但它们不是我真正需要的,它们自然没有正确的ID,所以问题就在某处那里。

控制器代码(仅限相关操作):

def new
  @booking = Booking.new
  @user = User.find(params[:user])
  @consumer = User.new
  @trainer_rates = TrainerRate.where({user:@user._id}).all

  @trainer_rates.each do |f|
    @booking.booking_details.build trainer_rate:f._id
  end

  @booking.build_consumer

预订模式(仅限相关部分):

accepts_nested_attributes_for :booking_details

def booking_details_attributes=(details_attributes)
  details_attributes.each do |attributes|
    booking_details.build(attributes)
  end
end

预订明细模型(相关部分):

belongs_to :booking
belongs_to :trainer_rate

TrainerRate模式(如评论中所示):

class TrainerRate
  include Mongoid::Document
  field :rate, type: Float
  belongs_to :schedule_slot_day
  belongs_to :user
  belongs_to :schedule_slot
  has_many :booking_details
end

完成堆栈跟踪:

    /Users/mohammadusman/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/relations/proxy.rb:150:in `method_missing'
actionpack (4.0.0) lib/action_view/helpers/tags/check_box.rb:53:in `checked?'
actionpack (4.0.0) lib/action_view/helpers/tags/checkable.rb:10:in `input_checked?'
actionpack (4.0.0) lib/action_view/helpers/tags/check_box.rb:19:in `render'
actionpack (4.0.0) lib/action_view/helpers/form_helper.rb:930:in `check_box'
actionpack (4.0.0) lib/action_view/helpers/form_helper.rb:1649:in `check_box'
/Users/mohammadusman/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/contextual/mongo.rb:648:in `yield_document'
/Users/mohammadusman/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/contextual/mongo.rb:123:in `block in each'
moped (2.0.0.beta6) lib/moped/cursor.rb:28:in `block in each'
moped (2.0.0.beta6) lib/moped/cursor.rb:28:in `each'
moped (2.0.0.beta6) lib/moped/cursor.rb:28:in `each'
moped (2.0.0.beta6) lib/moped/query.rb:77:in `each'
/Users/mohammadusman/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/contextual/mongo.rb:122:in `each'
/Users/mohammadusman/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/contextual.rb:20:in `each'
/Users/mohammadusman/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/contextual/mongo.rb:648:in `yield_document'
/Users/mohammadusman/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/contextual/mongo.rb:123:in `block in each'
moped (2.0.0.beta6) lib/moped/cursor.rb:28:in `block in each'
moped (2.0.0.beta6) lib/moped/cursor.rb:28:in `each'
moped (2.0.0.beta6) lib/moped/cursor.rb:28:in `each'
moped (2.0.0.beta6) lib/moped/query.rb:77:in `each'
/Users/mohammadusman/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/contextual/mongo.rb:122:in `each'
/Users/mohammadusman/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/contextual.rb:20:in `each'
actionpack (4.0.0) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionpack (4.0.0) lib/action_view/helpers/capture_helper.rb:200:in `with_output_buffer'
actionpack (4.0.0) lib/action_view/helpers/capture_helper.rb:38:in `capture'
actionpack (4.0.0) lib/action_view/helpers/form_helper.rb:1864:in `block in fields_for_nested_model'
actionpack (4.0.0) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionpack (4.0.0) lib/action_view/helpers/capture_helper.rb:200:in `with_output_buffer'
actionpack (4.0.0) lib/action_view/helpers/capture_helper.rb:38:in `capture'
actionpack (4.0.0) lib/action_view/helpers/form_helper.rb:699:in `fields_for'
actionpack (4.0.0) lib/action_view/helpers/form_helper.rb:1863:in `fields_for_nested_model'
actionpack (4.0.0) lib/action_view/helpers/form_helper.rb:1849:in `block in fields_for_with_nested_attributes'
/Users/mohammadusman/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/relations/referenced/many.rb:165:in `block in each'
/Users/mohammadusman/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/relations/targets/enumerable.rb:188:in `block in each'
/Users/mohammadusman/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/relations/targets/enumerable.rb:187:in `each_pair'
/Users/mohammadusman/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/relations/targets/enumerable.rb:187:in `each'
/Users/mohammadusman/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-5b0f031992cb/lib/mongoid/relations/referenced/many.rb:165:in `each'
actionpack (4.0.0) lib/action_view/helpers/form_helper.rb:1847:in `fields_for_with_nested_attributes'
actionpack (4.0.0) lib/action_view/helpers/form_helper.rb:1521:in `fields_for'
actionpack (4.0.0) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionpack (4.0.0) lib/action_view/helpers/capture_helper.rb:200:in `with_output_buffer'
actionpack (4.0.0) lib/action_view/helpers/capture_helper.rb:38:in `capture'
actionpack (4.0.0) lib/action_view/helpers/form_helper.rb:435:in `form_for'
actionpack (4.0.0) lib/action_view/template.rb:143:in `block in render'
activesupport (4.0.0) lib/active_support/notifications.rb:161:in `instrument'
actionpack (4.0.0) lib/action_view/template.rb:141:in `render'
actionpack (4.0.0) lib/action_view/renderer/partial_renderer.rb:306:in `render_partial'
actionpack (4.0.0) lib/action_view/renderer/partial_renderer.rb:279:in `block in render'
actionpack (4.0.0) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.0) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (4.0.0) lib/action_view/renderer/partial_renderer.rb:278:in `render'
actionpack (4.0.0) lib/action_view/renderer/renderer.rb:47:in `render_partial'
actionpack (4.0.0) lib/action_view/helpers/rendering_helper.rb:27:in `render'
actionpack (4.0.0) lib/action_view/template.rb:143:in `block in render'
activesupport (4.0.0) lib/active_support/notifications.rb:161:in `instrument'
actionpack (4.0.0) lib/action_view/template.rb:141:in `render'
actionpack (4.0.0) lib/action_view/renderer/template_renderer.rb:49:in `block (2 levels) in render_template'
actionpack (4.0.0) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.0) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (4.0.0) lib/action_view/renderer/template_renderer.rb:48:in `block in render_template'
actionpack (4.0.0) lib/action_view/renderer/template_renderer.rb:56:in `render_with_layout'
actionpack (4.0.0) lib/action_view/renderer/template_renderer.rb:47:in `render_template'
actionpack (4.0.0) lib/action_view/renderer/template_renderer.rb:17:in `render'
actionpack (4.0.0) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionpack (4.0.0) lib/action_view/renderer/renderer.rb:23:in `render'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:127:in `_render_template'
actionpack (4.0.0) lib/action_controller/metal/streaming.rb:219:in `_render_template'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:120:in `render_to_body'
actionpack (4.0.0) lib/action_controller/metal/rendering.rb:33:in `render_to_body'
actionpack (4.0.0) lib/action_controller/metal/renderers.rb:26:in `render_to_body'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:97:in `render'
actionpack (4.0.0) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.0.0) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/mohammadusman/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
activesupport (4.0.0) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:40:in `render'
remotipart (1.2.1) lib/remotipart/render_overrides.rb:14:in `render_with_remotipart'
actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.0) lib/active_support/callbacks.rb:413:in `_run__1358898418329927427__process_action__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655: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'
remotipart (1.2.1) lib/remotipart/middleware.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241: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.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__1221675166191648576__call__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
actionpack (4.0.0) 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.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97: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/mohammadusman/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/Users/mohammadusman/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/Users/mohammadusman/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

1 个答案:

答案 0 :(得分:1)

最后制定了解决方案,我将对象链接到复选框,而不是使用它:

<%= check_box_tag 'booking[booking_details_attributes][][trainer_rate]',ob._id %>

而不是

<%= to.check_box :trainer_rate %>

希望这可以帮助面临类似问题的其他用户。 :)