Kir
Kir

Reputation: 8111

operations with REST routes

I have a route for Users controller:

  get 'users/get_my_rating'
  get 'users/recover_password'
  get 'users/get_last_comments'
  post 'users/update_rating'

  resources :users do
    member do
      get 'about'
      get 'comments'
      get 'friends'
    end
  end

Upvotes: 1

Views: 229

Answers (3)

Thomas
Thomas

Reputation: 4999

The other answers about what to do for your second question are perfectly fine, however the best way to remap the create action as signup is to use the :path_names option.

In this case it should be something like:

resources :users, :path_names => { :create => "signup" } do
    # All your other routing
end

However, one thing to note is that the URL for the create action is never shown to the user...the create URL is posted to by the form created by the new action. The URL that the user will see is the one for the new action.

As such you might instead want to do:

resources :users, :path_names => { :new => "signup" } do
    # All your other routing
end

Upvotes: 3

brettish
brettish

Reputation: 2648

To integrate the first 3 routes, do the following:

resources :users do
  member do
    get 'about'
    get 'comments'
    get 'friends'
  end
  collection do
    get 'get_my_rating'
    get 'recover_password'
    get 'get_last_comments'
  end
end

Also, you can add an :as => 'name_here' to the various member and collection rules to generate the *_url and *_path helpers. Check out rake routes to find the name of the route etc.

To override where the REST create goes:

match '/users' => 'users#signup', :as => 'signup', :via => :post

Make sure you place this before the resources :users stuff since rails tries to find the route from the top down and goes with the first match.

Also, just something to consider: from the name of 'users/get_my_rating' (and the 2 that follow) it sounds like these are more of member methods than collection methods, so I would consider moving these into the member block instead of having them in a collection block. Just my 2 cents.

Upvotes: 0

Edward M Smith
Edward M Smith

Reputation: 10627

How to remap create REST action to signup ?

match '/signup' => 'users#create', :as => 'signup'

giving you

signup_url

for use in your code

How to put first 3 rules to the resources :users block ?

not entirely sure what you mean here, but...

resources :users do
  member do
    get 'about'
    get 'comments'
    get 'friends'
  end
  collection do
    get 'get_my_rating' # => UsersController get_my_rating
    get 'recover_password' # => UsersController recover_password
    get 'get_last_comments' # => UsersController get_last_comments
    post 'update_rating' # => UsersController update_rating
  end
end

results in

 get_my_rating_users     GET    /users/get_my_rating(.:format)              {:action=>"get_my_rating", :controller=>"users"}
 recover_password_users  GET    /users/recover_password(.:format)           {:action=>"recover_password", :controller=>"users"}
 get_last_comments_users GET    /users/get_last_comments(.:format)          {:action=>"get_last_comments", :controller=>"users"}
 update_rating_users     POST   /users/update_rating(.:format)              {:action=>"update_rating", :controller=>"users"}

Upvotes: 2

Related Questions