Joern Akkermann
Joern Akkermann

Reputation: 3622

Rails 3 Routes - routing a parameter different to :id

I have problems getting clear about the new routing system of Rails 3+.

I want to match "/:name" to :controller => :profiles, :action => :show, :name => name

How do I realize this?

I tried with match "/:name" => "profiles#show" but this just uses the :name as :id...

Yours Joern.

Upvotes: 0

Views: 717

Answers (1)

basicxman
basicxman

Reputation: 2115

With match "/:name" => "profiles#show" this is going to trigger the show action on the profiles controller. Inside that controller instance you can access the matched URL from params[:name]

I assume you are trying to get a Model record by name instead of id, thus you must modify your show action. For instance,

def show
  @profile = Profile.find_by_name(params[:name])
end

Upvotes: 2

Related Questions