首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataMapper Associations 'NoMethodError‘

DataMapper Associations 'NoMethodError‘
EN

Stack Overflow用户
提问于 2013-01-31 16:29:00
回答 1查看 232关注 0票数 0

我被一个问题困住了,不知道问题出在哪里。因为我是相对较新的,所以你可以希望帮助我:)。

首先,我使用padrinodatamapper gem。

我有一个带one_to_many关系的旅行和住宿模式

模型的样子:Trip.rb

代码语言:javascript
复制
class Trip
  include DataMapper::Resource

  # Associations
  has n, :accommodations

  property :id, Serial, :key => true
  # many more properties
end

Accommodation.rb

代码语言:javascript
复制
class Accommodation
  include DataMapper::Resource

  has n, :appartments
  belongs_to :trip, :child_key => [:trip_id]

  # property <name>, <type>
  property :id, Serial
end

根据dm文档,我假设关联是有效的。

我想达到以下的申请行为:

用户可以选择一次特定的旅行,并添加或删除住宿。

这就是我的控制器的样子:trips.rb

代码语言:javascript
复制
HhtBackoffice.controllers :trips do
  get :index do
    @trips = Trip.all
    render 'trips/index'
  end

  get :show, :with => :id do
    @trip = Trip.get(params[:id])
    set_current_trip(Trip.get(params[:id]))
    render 'trips/show'
  end

  get :new do
    @trip = Trip.new
    render 'trips/new'
  end

  post :create do
    @trip = Trip.new(params[:trip])
    if @trip.save
      flash[:notice] = 'Neue Reise wurde erfolgreich hinzugef&uuml;gt'
      redirect url(:trips, :edit, :id => @trip.id)
    else 
      render 'trips/new'
    end
  end
..........
more code
........
end

accommodations.rb

代码语言:javascript
复制
HhtBackoffice.controllers :accommodations do
  get :index do
    @accommodations = Accommodation.all
    render 'accommodations/index'
  end

  get :new do
    @trip = current_trip
    @accommodation = @trip.accommodations.new  # <-- This function is not known according to the error
    render 'accommodations/new'
  end

  post :create do
    @trip = current_trip
    @accommodation = @trip.accommodations.new(params[:accommodation])
    if @accommodation.save
      flash[:notice] = 'Neue Reise wurde erfolgreich hinzugef&uuml;gt'
      redirect url(:accommodations, :edit, :id => @accommodation.id)
    else 
      render 'accommodations/new'
    end
  end
.... more code here ....
end

如您所见,我使用了两个助手函数。set_current_trip (调用显示视图时)以跟踪当前行程。和current_trip去拿current_trip。

这是我的trips/show.haml视图

代码语言:javascript
复制
.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="&nbsp;"

这是accommodations/new视图

代码语言:javascript
复制
.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 }

这是错误

代码语言:javascript
复制
#<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

通过padrino控制台执行“相同”操作没有问题。

代码语言:javascript
复制
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>

对解决这个问题有什么建议吗?

好吧,我猜到了。我的current_trip对象是零。这些是我的助手方法:

helper/trips.rb

代码语言:javascript
复制
  def set_current_trip(trip=nil)
    @current_trip = trip
  end

  def current_trip 
    @current_trip 
  end

提前谢谢!

编辑2

这是我的路线。也许嵌套是让它工作的好方法,我想打个电话

代码语言:javascript
复制
=button_to pat(:new), url(:accommodations, :new), :method => :get, :class => :button_to

在我的trips/show.haml

但我明白错误

代码语言:javascript
复制
route mapping for url(:accommodations_new) could not be found!

为什么会这样呢?根据我的路线,我应该去/旅行/:trip_id/住宿/新的

路由

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-31 20:22:16

current_trip正在返回零。首先,在调用其中一个方法时,确保@trip不是零,因为如果尚未设置trip怎么办?

代码语言:javascript
复制
  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

其次,我不确定您的助手方法是否会返回一个trip,因为@current_trip还没有初始化。current_trip是干什么用的?这是否是您希望用户跨会话持久存在的东西?如果是这样的话,您可能希望将current_trip保存在cookie中。

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14630535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档