2011-11-24 56 views
0

在我的AdminController中,我有名爲edit,update和update_admin的方法。並在route.rb如何在route.rb中編寫匹配

resources :session, :only => [update] 
    match '/:controller(/:action(/:id))' 
    end 

當我瀏覽URL'/用戶/編輯/ 1'匹配。從這個頁面我想調用AdminController的update_admin中的action方法。這個怎麼做?

我edit.erb有

<%= render :partial => '/submit', :locals = 
> {:button_html => f.submit('Update'), :validate_present => true} 
%> 

回答

0

首先,檢查你的路線去安慰,做

rake routes | grep session 

那裏你會得到清單Rails應用程序的所有路由(匹配到grep的)

在第二我不明白你的routes.rb

會怎麼做

resources :session, :only => [update] do 
    match '/:controller(/:action(/:id))' 
end 

resources :session, :only => [update] 
    match '/:controller(/:action(/:id))' 
end 

這些都是2個不同的東西。我想你想要最後一個。但在這裏我們去,還有另一個問題

resources :session, :only=>[update] 

,這將引發一個錯誤。(未定義的局部變量或方法'更新」的#) 如果要指定動作,你需要做它作爲一個重點

resources :session, :only=>[:update] 

但你也想編輯消息,(編輯的形式,更新的動作保存更改),所以你要做的

resources :users, :only=>[:edit, :update] 

現在檢查耙路線,看看voil一個!

    edit_session GET  /session/:id/edit(.:format)    {:action=>"edit", :controller=>"session"} 
        session PUT  /session/:id(.:format)      {:action=>"update", :controller=>"session"} 

//編輯

,如果你想這樣做在你的admin_controller你應該有一個命名空間

#i take example for a user 
namespace :admin do 
    resources :user :only => [:edit, :update] 
end 

,如果你現在想從視圖的路線鏈接到它被命名爲 edit_admin_user_path

並且您需要在窗體中帶上名稱空間_for like:

=form_for [:admin, @user] do |f| 
相關問題