Search code examples
ruby-on-railsrubyroutescontrollers

attribute values are changing while trying to insert into tables in rails


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.


Solution

  • Your routes will be matched in the order they are defined. Since /:username is defined at the top of your routes file and has low specificity, it appears to be masking routes defined afterwards. I suggest moving that route to the bottom of the file and/or applying some constraints, like a regex, to make it less "greedy".