0

I am working on a project where there are users and users have profiles. I am using the devise gem for the user authentication purposes.I have changed a path for an action in a controller which is resulting in unexplained behavior.

Test::Application.routes.draw do
  match '/:username' => 'profile#show' , :as => :username


  get "profile/update"

  get "static_pages/index"

  devise_for :users 

The server logs show the following.

Started POST "/users" for 127.0.0.1 at 2013-02-22 03:07:48 +0530
Processing by ProfileController#show as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Tr90BvI6sztk7gxlPm6KF3td3xIe91SpCZDGsIniv60=", "user"=>{"username"=>"example", "email"=>"example@email.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up", "username"=>"users"}
  Rendered profile/show.html.erb within layouts/application (0.1ms)
Completed 200 OK in 12ms (Views: 11.5ms | ActiveRecord: 0.0ms)

The data is not being inserted into the database and the username parameter is getting changed, and when i change the route to the default route i.e

match '/:username' => 'profile#show' , :as => :username

changed to

get "profile/show"

everything is back to normal. the data is being inserted and everything works fine.I dont understand why and action from profile controller is being called when it is a user registration.rake routes gives the following output

                username        /:username(.:format)           profile#show
          profile_update GET    /profile/update(.:format)      profile#update
      static_pages_index GET    /static_pages/index(.:format)  static_pages#index
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                    root        /                              static_pages#inde

Thanks in advance.

4

1 回答 1

0

您的路线将按照定义的顺序进行匹配。由于/:username在您的路由文件的顶部定义并且具有较低的特异性,因此它似乎是在屏蔽之后定义的路由。我建议将该路径移动到文件底部和/或应用一些约束,如正则表达式,以使其不那么“贪婪”。

于 2013-02-21T21:55:53.143 回答