2010-12-11 76 views
1

如何使用鏈接到我的控制器中的方法的link_to創建鏈接。我想創建一個鏈接是這樣的:無法創建鏈接到我的控制器中的方法的url

/會議/ 10 /聯繫人/ 2/send_invite

的send_invite方法是在聯繫人控制器。我不確定下一步如何設置路線文件。我嘗試過嵌套資源,但目前爲止沒有運氣。另外,在link_to中指定的鏈接是什麼?我嘗試了各種組合,例如send_invite_path,meeting_contact_send_invite_path,但是所有內容都會引發錯誤。

我的路線文件看起來像這樣:

resources :meetings do 
    resources :contacts, :only => [:send_invite], :as => :send_invite 
end 

但後來當我嘗試send_invite_path稱,這是行不通的。

回答

1

擴大奧斯汀的答案。這種嵌套在配置/ routes.rb中

resources :meetings do 
    resources :contacts do 
     member do 
     post :send_invite 
     end 
    end 
    end 

會給你這些路線

send_invite_meeting_contact POST /meetings/:meeting_id/contacts/:id/send_invite(.:format) {:action=>"send_invite", :controller=>"contacts"} 
        meeting_contacts GET /meetings/:meeting_id/contacts(.:format)     {:action=>"index", :controller=>"contacts"} 
             POST /meetings/:meeting_id/contacts(.:format)     {:action=>"create", :controller=>"contacts"} 
        new_meeting_contact GET /meetings/:meeting_id/contacts/new(.:format)    {:action=>"new", :controller=>"contacts"} 
       edit_meeting_contact GET /meetings/:meeting_id/contacts/:id/edit(.:format)  {:action=>"edit", :controller=>"contacts"} 
         meeting_contact GET /meetings/:meeting_id/contacts/:id(.:format)    {:action=>"show", :controller=>"contacts"} 
             PUT /meetings/:meeting_id/contacts/:id(.:format)    {:action=>"update", :controller=>"contacts"} 
             DELETE /meetings/:meeting_id/contacts/:id(.:format)    {:action=>"destroy", :controller=>"contacts"} 
          meetings GET /meetings(.:format)          {:action=>"index", :controller=>"meetings"} 
             POST /meetings(.:format)          {:action=>"create", :controller=>"meetings"} 
          new_meeting GET /meetings/new(.:format)         {:action=>"new", :controller=>"meetings"} 
         edit_meeting GET /meetings/:id/edit(.:format)        {:action=>"edit", :controller=>"meetings"} 
           meeting GET /meetings/:id(.:format)         {:action=>"show", :controller=>"meetings"} 
             PUT /meetings/:id(.:format)         {:action=>"update", :controller=>"meetings"} 
             DELETE /meetings/:id(.:format)         {:action=>"destroy", :controller=>"meetings"} 
+0

感謝的問題!這是完美的。 – Allen 2010-12-11 22:20:25

0

您將需要在您的routes.rb文件中定義路由並設置您的路由以進行嵌套。就名稱而言,您可以在您的routes.rb文件中定義該文件,並使用:as => "some_name",這將允許您使用link_to some_name_path

有關路由的詳細信息:http://guides.rubyonrails.org/routing.html

具體的嵌套資源:http://guides.rubyonrails.org/routing.html#nested-resources

+0

我已經更新了我什麼是我的路線文件 – Allen 2010-12-11 22:10:47

相關問題