用于嵌套浅层资源的方法的Rails路由

时间:2014-04-28 18:05:49

标签: ruby-on-rails rails-routing

由于输入很多,我已经清理了我的深路线并用浅路线取而代之。我试图让自定义方法工作,并且缺少连接点上的东西。我的routes.rb有这些行:

resources :members, shallow: true do
  resources :events, shallow: true do
    get 'complete' => 'events#complete'
    resources :items
  end
end

我的目标是能够在事件事件上调用' events /:id / complete' 来完成事件并对其进行后期处理。以上路线添加以下路线:

event_event_complete GET    /events/:event_id/complete(.:format)     events#complete
       member_events GET    /members/:member_id/events(.:format)     events#index
                     POST   /members/:member_id/events(.:format)     events#create
    new_member_event GET    /members/:member_id/events/new(.:format) events#new
          edit_event GET    /events/:id/edit(.:format)               events#edit
               event GET    /events/:id(.:format)                    events#show
                     PATCH  /events/:id(.:format)                    events#update
                     PUT    /events/:id(.:format)                    events#update
                     DELETE /events/:id(.:format)                    events#destroy

目前控制器操作:

def complete
  @event = Event.find(params[:event_id])
end

我认为应该如何:

def complete
  @event = Event.find(params[:id])
end

似乎我把它放在错误的地方或遗漏了一些东西,因为它传递了事件:event_id而不是:id。一切正常,但看起来这样比较麻烦,应该是这样,我打赌我做了一些愚蠢的错误。

提前感谢您的帮助!

标记

2 个答案:

答案 0 :(得分:1)

更新您的路线,如下所示:

  resources :members, shallow: true do
    resources :events, shallow: true do
      member do
        get 'complete' => 'events#complete'
      end
      resources :items
    end
  end

通过这种方式,您可以在params[:id]中收到活动ID,而不是params[:event_id]行为路线的complete

答案 1 :(得分:0)

使用@event = Event.find(params[:id])

执行此操作
resources :members, shallow: true do
    resources :events, shallow: true do
      member do
        get 'complete' => 'events#complete'
      end
      resources :items
    end
  end