Ruby on rails 3 数据映射程序关联“NoMethodError”

Ruby on rails 3 数据映射程序关联“NoMethodError”,ruby-on-rails-3,datamapper,padrino,Ruby On Rails 3,Datamapper,Padrino,我被一个问题缠住了,不知道问题在哪里。因为我比较新,希望你能帮助我: 首先,我将padrino与DataMapperGem一起使用 我有一个一对多的旅行和住宿模式 模型的外观: Trip.rb 住宿.rb 住宿.rb 这是住宿/新视野 这就是错误所在 提前谢谢 编辑2 这是我的路线。也许筑巢是让它工作的好方法 我想打电话 =button_to pat(:new), url(:accommodations, :new), :method => :get, :class => :butt

我被一个问题缠住了,不知道问题在哪里。因为我比较新,希望你能帮助我:

首先,我将padrino与DataMapperGem一起使用

我有一个一对多的旅行和住宿模式

模型的外观: Trip.rb

住宿.rb

住宿.rb

这是住宿/新视野

这就是错误所在

提前谢谢

编辑2 这是我的路线。也许筑巢是让它工作的好方法 我想打电话

=button_to pat(:new), url(:accommodations, :new), :method => :get, :class => :button_to
在我的旅行/表演中

但是我得到了错误

route mapping for url(:accommodations_new) could not be found!
为什么呢?根据我的路线,我应该到达/trips/:trip\u id/住宿/新

路线

当前_行程返回零。 首先,在调用@trip的一个方法时,确保@trip不是nil,因为如果还没有设置trip呢

  get :new do
    @trip = current_trip
    if @trip
      @accommodation = @trip.accommodations.new  
      render 'accommodations/new'
    else
      # trip is nil. Call an error or something.
    end
  end
其次,我不确定您的helper方法是否会返回trip,因为@current\u trip尚未初始化。现在的旅行是为了什么?您是否希望跨会话为用户持久化?如果是这样,您可能希望将当前的_旅行保存在cookie中

def set_current_trip(trip_id = 0)

  response.set_cookie 'current_trip', :value => trip_id, :path => '/', :expires => Time.now + 60 * 60 * 24 * 365 * 20

end

def current_trip 
  trip_id = request.cookies["current_trip"]
  current_trip = Trip.get trip_id
end

好的,谢谢你的快速回复。正如预期的那样,当前_跳闸不会返回跳闸,否则情况会引发错误。然而,我不认为使用cookies是解决我问题的正确方法。我希望它如下所示:选择一次旅行,例如/trips/show/1。从那里为旅行增加住宿。因为我不知道如何跟踪上一次选择的行程,所以在调用/trips/show/:id时,我考虑使用helper方法存储当前的行程。您能跟我来吗?或者有没有一种方法可以更容易地实现相同的功能?您不能以这种方式存储在助手方法中。变量并不是真的在页面之间持久存在。只是想澄清一下:用户进入/trips/1以选择trip 1,然后进入/accountations或类似的内容以向该旅行添加住宿?您可以在url中传递旅行,也可以将旅行作为参数传递,以便将它们发送到/contractions?trip_id=1或将所选旅行存储为cookie。好的,我正在尝试进行以下调用:'=button_to pat:new,url:contractions,:new,:id=>@trip.id,:method=>:get,:class=>:button_to',然后简单地说'@trip=trip.getparams[:id]“在我的房间里。但我的@trip对象仍然为零……我的问题是对如何传递对象或创建子对象的深刻误解。在我的行程视图中,我现在有了用于添加住宿的创建按钮。我将trip_id传递给住宿控制器并创建'@trip.acculations.new。。。好了,现在很好用。然而,我标记了你的答案并投票表决,因为这是上述错误的解决方案。谢谢你的帮助!
.block
    .secondary-navigation
        %ul.wat-cf
            %li.first.active=link_to pat(:list), url(:trips, :index)
            %li=link_to pat(:new), url(:trips, :new)
    .content
        %h2.title
            
        .inner
            -@trip.accommodations.each do |acco|
                %ud
                    %li= acco.id
                    %li= acco.name
            =button_to pat(:new), url(:accommodations, :new), :method => :get, :class => :button_to

        .actions-bar.wat-cf
            .actions=" "
.block
  .secondary-navigation
    %ul.wat-cf
      %li.first=link_to pat(:list), url(:accommodations, :index)
      %li.active=link_to pat(:new), url(:accommodations, :new)
  .content
    %h2.title
      =pat(:new)
      =mt(:accommodation)
    .inner
      -form_for :accommodation, url(:accommodations, :create), :class => :form do |f|
        =partial "accommodations/form", :locals => { :f => f }
#<NoMethodError: undefined method `accommodations' for nil:NilClass>

/home/paddy/Projects/hht_backoffice/app/controllers/accommodations.rb in block (2 levels) in <top (required)>
    @trip.accommodations.new
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/padrino-core-0.10.7/lib/padrino-core/application/routing.rb in call
            proc { |a,p| unbound_method.bind(a).call }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/padrino-core-0.10.7/lib/padrino-core/application/routing.rb in block in route
            proc { |a,p| unbound_method.bind(a).call }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/padrino-core-0.10.7/lib/padrino-core/application/routing.rb in []
        halt_response     = catch(:halt) { route_eval { @route.dest[self, @block_params] } }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/padrino-core-0.10.7/lib/padrino-core/application/routing.rb in block (3 levels) in process_destination_path
        halt_response     = catch(:halt) { route_eval { @route.dest[self, @block_params] } }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/sinatra-1.3.4/lib/sinatra/base.rb in route_eval
      throw :halt, yield
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/padrino-core-0.10.7/lib/padrino-core/application/routing.rb in block (2 levels) in process_destination_path
        halt_response     = catch(:halt) { route_eval { @route.dest[self, @block_params] } }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/padrino-core-0.10.7/lib/padrino-core/application/routing.rb in catch
        halt_response     = catch(:halt) { route_eval { @route.dest[self, @block_params] } }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/padrino-core-0.10.7/lib/padrino-core/application/routing.rb in block in process_destination_path
        halt_response     = catch(:halt) { route_eval { @route.dest[self, @block_params] } }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/padrino-core-0.10.7/lib/padrino-core/application/routing.rb in instance_eval
    Thread.current['padrino.instance'].instance_eval do
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/padrino-core-0.10.7/lib/padrino-core/application/routing.rb in process_destination_path
    Thread.current['padrino.instance'].instance_eval do
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/http_router-0.10.2/lib/http_router/node/root.rb in []
      end
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/http_router-0.10.2/lib/http_router.rb in block in call
    response = catch(:success) { @root[request] }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/http_router-0.10.2/lib/http_router.rb in catch
    response = catch(:success) { @root[request] }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/http_router-0.10.2/lib/http_router.rb in call
    response = catch(:success) { @root[request] }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/padrino-core-0.10.7/lib/padrino-core/application/routing.rb in route!
          if base.compiled_router and match = base.compiled_router.call(@request.env)
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/padrino-core-0.10.7/lib/padrino-core/application/routing.rb in dispatch!
          route!
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/sinatra-1.3.4/lib/sinatra/base.rb in block in call!
      invoke { dispatch! }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/sinatra-1.3.4/lib/sinatra/base.rb in block in invoke
      res = catch(:halt) { yield }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/sinatra-1.3.4/lib/sinatra/base.rb in catch
      res = catch(:halt) { yield }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/sinatra-1.3.4/lib/sinatra/base.rb in invoke
      res = catch(:halt) { yield }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/sinatra-1.3.4/lib/sinatra/base.rb in call!
      invoke { dispatch! }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/sinatra-1.3.4/lib/sinatra/base.rb in call
      dup.call!(env)
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/sass-3.2.5/lib/sass/plugin/rack.rb in call
        @app.call(env)
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.5.1/lib/rack/head.rb in call
    status, headers, body = @app.call(env)
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.5.1/lib/rack/methodoverride.rb in call
      @app.call(env)
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/padrino-core-0.10.7/lib/padrino-core/reloader.rb in call
        @app.call(env)
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/padrino-core-0.10.7/lib/padrino-core/logger.rb in call
        status, header, body = @app.call(env)
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/sinatra-1.3.4/lib/sinatra/showexceptions.rb in call
      @app.call(env)
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.5.1/lib/rack/session/abstract/id.rb in context
          status, headers, body = app.call(env)
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.5.1/lib/rack/session/abstract/id.rb in call
          context(env)
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/sinatra-1.3.4/lib/sinatra/base.rb in block in call
        synchronize { prototype.call(env) }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/sinatra-1.3.4/lib/sinatra/base.rb in synchronize
          yield
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/sinatra-1.3.4/lib/sinatra/base.rb in call
        synchronize { prototype.call(env) }
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/padrino-core-0.10.7/lib/padrino-core/router.rb in block in call
        return app.call(
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/padrino-core-0.10.7/lib/padrino-core/router.rb in each
      @mapping.each do |host, path, match, app|
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/padrino-core-0.10.7/lib/padrino-core/router.rb in call
      @mapping.each do |host, path, match, app|
/home/paddy/.rvm/gems/ruby-1.9.3-p374/gems/rack-1.5.1/lib/rack/handler/webrick.rb in service
        status, headers, body = @app.call(env)
/home/paddy/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/webrick/httpserver.rb in service
      si.service(req, res)
/home/paddy/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/webrick/httpserver.rb in run
          server.service(req, res)
/home/paddy/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/webrick/server.rb in block in start_thread
irb(main):001:0> @trip = Trip.get(1)
  DEBUG - (0.000101) SET sql_auto_is_null = 0
  DEBUG - (0.000127) SET SESSION sql_mode = 'ANSI,NO_BACKSLASH_ESCAPES,NO_DIR_IN_CREATE,NO_ENGINE_SUBSTITUTION,NO_UNSIGNED_SUBTRACTION,TRADITIONAL'
  DEBUG - (0.000092) SELECT `id`, `name`, `type`, `starts_at`, `ends_at`, `no_of_free_places`, `no_of_guests`, `raceday`, `created_at`, `updated_at` FROM `trips` WHERE `id` = 1 LIMIT 1
=> #<Trip @id=1 @name="Rofl" @type="Wettkampfreise" @starts_at=nil @ends_at=nil @no_of_free_places=nil @no_of_guests=nil @raceday=nil @created_at=#<DateTime: 2013-01-31T15:00:16+01:00 ((2456324j,50416s,0n),+3600s,2299161j)> @updated_at=#<DateTime: 2013-01-31T15:16:40+01:00 ((2456324j,51400s,0n),+3600s,2299161j)>>

irb(main):02:0> @trip.accommodations.new
=> #<Accommodation @id=nil @name=nil @no_of_appartments=nil @location=nil @capacity=nil @trip_id=1>
  def set_current_trip(trip=nil)
    @current_trip = trip
  end

  def current_trip 
    @current_trip 
  end
=button_to pat(:new), url(:accommodations, :new), :method => :get, :class => :button_to
route mapping for url(:accommodations_new) could not be found!
URL                           REQUEST  PATH
    (:accommodations, :index)        GET    /trips/:trip_id/accommodations
    (:accommodations, :new)          GET    /trips/:trip_id/accommodations/new
    (:accommodations, :create)      POST    /trips/:trip_id/accommodations/create
    (:accommodations, :edit)         GET    /trips/:trip_id/accommodations/edit/:id
    (:accommodations, :update)       PUT    /trips/:trip_id/accommodations/update/:id
    (:accommodations, :destroy)    DELETE   /trips/:trip_id/accommodations/destroy/:id
    (:base, :index)                 GET    /
    (:trips, :index)                GET    /trips
    (:trips, :show)                 GET    /trips/show/:id
    (:trips, :new)                  GET    /trips/new
    (:trips, :create)              POST    /trips/create
    (:trips, :edit)                 GET    /trips/edit/:id
    (:trips, :update)               PUT    /trips/update/:id
    (:trips, :destroy)            DELETE   /trips/destroy/:id
  get :new do
    @trip = current_trip
    if @trip
      @accommodation = @trip.accommodations.new  
      render 'accommodations/new'
    else
      # trip is nil. Call an error or something.
    end
  end
def set_current_trip(trip_id = 0)

  response.set_cookie 'current_trip', :value => trip_id, :path => '/', :expires => Time.now + 60 * 60 * 24 * 365 * 20

end

def current_trip 
  trip_id = request.cookies["current_trip"]
  current_trip = Trip.get trip_id
end