[英]How do i change Rails routing from controller/:id to controller/:name in Rails 3?
就像話題說的那樣。 我想在Rails 3中將默認的controller /:id路由更改為controller /:name。在Rails 2中,您使用了命名路由,如下所示:
配置/ routes.rb中
map.location 'location/:name',
:controller => 'your_controller', :action => 'your_action'
備用命名路線
map.location 'goto/:name', :controller => 'location', :action => 'your_action'
視圖中URL規范的示例
<%= link_to 'foo', location_url({:name => 'foo'}) %>
<%= link_to 'bar', location_path({:name => 'bar'}) %>
但是我敢肯定,Rails 3中還有另一種(更好)的方式。
如果:name是對象的唯一標識符,則可以https://github.com/norman/friendly_id獲得對ActiveRecord的常規永久鏈接支持。
僅按指定
has_friendly_id :name
你會自動像
http://example.com/states/washington
代替
如果您有location.name,則可以像這樣link_to:
<%= link_to 'foo', location_path(location.name) %>
將為您提供url / locations /:name
相對於
<%= link_to 'foo', location %> #same as <%= link_to 'foo', location_path(location) %>
這將為您提供/ locations /:id
不確定,但這可能會對您有幫助。
匹配'user_delete /:name',:to =>'sessions#destroy'
這和
map.user_delete'/ user_delete /:name',:controller =>'sessions',:action =>'destroy'
嘗試這個:
match 'location/:name' => 'your_controller#your_action', :as => location
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.