开发者_高级运维:profiles, :action => :show, :name => name" />
开发者

Rails 3 Routes - routing a parameter different to :id

开发者 https://www.devze.com 2023-03-16 03:17 出处:网络
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

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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消