开发者

How to custom edit_user_path with nice URL

I'm adding a nav menu in application.html.erb which navigate users in authlogic. All routers like: :logout, :register and :login seems working with custom paths.

match 'account/login' => 'user_sessions#new', :as => :login
match 'account/logout' => 'user_sessions#destroy', :as => :logout
match 'register' => 'users#new', :as => :register

Here is nav menu:

<% if current_user %>
    <%= link_to "Edit Profile", edit_user_path(current_user.id)%> <%=h current_user.firstname %>
    <%= link_to "Logout", :logout %>
<% else %>
    <%= link_to "Register", :register %> | 
    <%= link_to "Login", :login %>
<% end %>

But edit_user_path is transferring me to /users/:id/edit. How do I make the nice URL for this path. I would like to make it /account/edit. It also need to disable this path /users/:id/edit to prevent user from requesting another user ID which doesnt belong to him/her. It should throw a 404 page is perfect.

Current paths in the nav menu:

Logout: /account/logout

L开发者_StackOverflow社区ogin: /account/login

Register: /register

I would love to have another path for edit_user_path:

Edit profile: /account/edit

Is there any way I can simply use edit_user_path(current_user.id) and the path automatically transfers me to /account/edit and disable id request.

Any help would be much appreciated! Thanks!

[Updated] This is my /config/router.rb

Appcatous::Application.routes.draw do
  resources :users, :user_sessions

  match "account" => "users#show", :as => :account
  match 'account/login' => 'user_sessions#new', :as => :login
  match 'account/logout' => 'user_sessions#destroy', :as => :logout
end

users.rb model is very simple:

class User < ActiveRecord::Base
  acts_as_authentic

  validates :firstname, :presence => true  
  validates :lastname,  :presence => true 

end


It would be helpful if you included all the routes in question - which means also the routes for the users. Did you create those with:

resources :users

In case you did, you'll have to disable that command and recreate just the routes you need manually with (example!):

match 'account/edit' => 'users#edit', :as => :edit_user

or whatever your controller is named. That way you'll get your "/account/edit" - however, you'll also have no way to transfer the user id that way. That one you'll have to transfer by other means, like through a variable stored in a cookie or a session. The latter method is the usual way of remembering user authentification anyway.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜