开发者

How to generate a path with id for a newly created method in rails controller

I'm adding new method 'editpass' to existing REST based User controller.

I want to pass the id to the editpass method which edit does for the edit_user_path

i have added the following in routes.rb

resources :user do
    collection do
     get '开发者_运维百科editpass'
    end
 end

and

 match 'user/:id/editpass' => 'user#editpass'

rake:routes produces following for the editpass :

 /user/:id/editpass(.:format)    {:action=>"editpass", :controller=>"user"

this routes doesn't have a path name.

what is the bestway to generate path for the method with id in rails3

Thanks in advance

Senling


First, you should pluralize user to users to be more conventional if you're using resources and not resource

resources :users do
  collection do
    get :editpass
  end
end

Then you can use:

link_to "Edit Pass", [:editpass, :users]

or

link_to "Edit Pass", editpass_users_path

Also using match 'user/:id/editpass' => 'user#editpass' is messing your routes and it is absolutley stupid idea, because your match is actually a member, while you've added it as a collection


 match 'user/:id/editpass' => 'user#editpass', :as => 'user_editpass'

That should make user_editpass_path(:id => @user) work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜