新手问题 怎么给 controller 改名?

liwei78 · 2013年03月29日 · 最后由 eva 回复于 2013年04月02日 · 2585 次阅读

比如说我有一个 source controller,我希望 url 里能通过/dest/1 也可以去访问 source/1 的东西。 我知道有一个东西叫:map.resources :sourse, :as => :dest可以做到, 但link_to "dest", source_path(@source)创建的连接里还是 source/1 而不是 dest/1,我想要的是这个 link_to 产生的连接里也是 dest/1. 要怎么办呢? 我试着用link_to "dest", dest_path(@source),但提示 dest_path 未定义。

注:由于项目原因,rails 版本比较旧,为 rails 2.3.14

Naming Routes You can specify a name for any route using the :as option.

match 'exit' => 'sessions#destroy', :as => :logout This will create logout_path and logout_url as named helpers in your application. Calling logout_path will return /exit

在向导http://guides.rubyonrails.org/routing.html, 搜索 Nameing Routes 就能看到你想要的东西

可通过:as 改名,然后再自己设置路由,如:

map.resources :source, :as => "dest"
map.new_dest "dest/new", controller => "dest",:action => "new"
map.show_dest "dest/:id", controller => "dest", :action => "show"
map.edit_dest "dest/:id/edit",controller => "dest", :action => "edit"

然后使用new_dest_path, show_dest_path, edit_dest_path来设置 url 了

需要 登录 后方可回复, 如果你还没有账号请 注册新账号