Rails路由url edit_path

时间:2015-04-17 10:10:43

标签: ruby-on-rails-3 routing

我的路线有问题:

No route matches events/edit_national_event

生成的网址:     http://localhost:3000/events/edit_national_event.:id

我需要:     http://localhost:3000/events/:id/edit_national_event

它适用于破坏,但不适用于编辑。我的代码出了什么问题?

路线:

 resources :events, only: [:index, :destroy] do
    collection do
      get :new_national_event
      get :new_local_event
      post :create_national_event
      post :create_local_event
    end
    member do
      get :edit_national_event
      get :edit_local_event
      put :update_national_event
      put :update_local_event
    end
  end

事件控制器:

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

观点:

@group_events.each do |event|
  tr
    td
      =event.title
    td
      =link_to edit_national_event_event_path(event)
      =link_to event_path(event), method: :delete

当我在做rake路线时:

new_national_event_events GET    /events/new_national_event(.:format)                                   events#new_national_event
new_local_event_events GET    /events/new_local_event(.:format)                                      events#new_local_event
create_national_event_events POST   /events/create_national_event(.:format)                                events#create_national_event
create_local_event_events POST   /events/create_local_event(.:format)                                   events#create_local_event
edit_national_event_event GET    /events/:id/edit_national_event(.:format)                              events#edit_national_event
edit_local_event_event GET    /events/:id/edit_local_event(.:format)                                 events#edit_local_event
update_national_event_event PUT    /events/:id/update_national_event(.:format)                            events#update_national_event
update_local_event_event PUT    /events/:id/update_local_event(.:format)                               events#update_local_event
events GET    /events(.:format)                                                      events#index
event DELETE /events/:id(.:format)                                                  events#destroy

1 个答案:

答案 0 :(得分:0)

正如您在rake routes输出中所看到的,您要查找的路线显示为/events/:id/edit_national_event(.:format)。这意味着它期望URL中的事件ID。你没有提供一个,这就是为什么它不匹配。

它期望ID的原因是您在member块中定义路由,而不是collection块。如果您不希望URL中有ID,请将路径移至collection块。如果您确实需要ID,请确保在链接到该操作时包含该ID。