Rails之配置路由

修改routes.rb,路由优先级从上到下逐行匹配,最下面是默认路由,修改后重启服务

 

1,自定义路由

match 'controllerA/action1' => 'controllerB#action2'
(或 match 'controllerA/action1', :to => 'controllerB#action2')

#将controllerA的action1路由到controllerB的action2,地址栏url不跳转,仅仅网页内容做切换

 

2,命名路由
match '/meetings' => 'events#index', :as => "meetings"
会产生mettings_path和meetings_url的helper,前者产生相对路径,后者产生绝对路径

 

3,match '/:username' => 'users#show'
#controller中可以通过params[:username]获取 !!!

http://127.0.0.1/ciaos会跳转到http://127.0.0.1/user/show(username=ciaos)

 

4,默认网站根目录,必须同时删除publick/index.html
配置网站根目录 root :to => "democontroller#index"

 

5,命名空间Namespace,我們可以在URL網址前多加一段,特別適合例如後台介面:

#如rails generate controller admin/main index login logout
namespace :admin do
  get "main/index"
  get "main/login"
  get "main/logout"
end
#访问127.0.0.1/admin/main/index

6,默认路由
match ':controller(/:action(/:id))(.:format)'

 

7,在项目根目录下执行rake routes查看所有路由


参照 :
1,http://ihower.tw/rails3/routing.html
2,http://guides.ruby-china.org/routing.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值