Auto Routing In Rails 3

Long long ago,  one fine day I switched from Rails 2.x to Rails 3.x and I notices a thing like this, when you have a controller like this one

class UserController < ApplicationController

  def profile
    @title = "Your Links"
    @links = Kaminari.paginate_array(current_user.links | current_user.jeweled_links).page(params[:page]).per(20)
  end

  def search
    @title = "Search for #{params[:s]}"
    @links = current_user.search_links(params[:s]).page(params[:page]).per(20)
    render :profile
  end

end
and if you type http://localhost:3000/user/profile , you will get an error unless the route get ‘user/profile’ is added into routes.rb .
It might be frustrating for one to add hundreds of routes in a large project and quiet not so good looking to see a huge routes.rb file, so how to go about the Rails 2 way where you put an action in an controller you can simply access it?
Well its quiet simple, just launch your routes.rb file and find this line
match ':controller(/:action(/:id))(.:format)'

It would be commented out, so uncomment it. Now launch http://localhost:3000/user/profile in your browser and it will load!

Advertisements

, ,

  1. #1 by SteveRob on August 14, 2013 - 12:08 pm

    Its a very handy feature but one that must be used with atmost caution. This will expose all the actions in your controller to the outside world. This is something that you would not want to happen.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: