自定义URL在轨道上的ruby
我知道rails使用控制器动作风格的url,比如www.myapp.com/home/index
我想在我的rails应用程序上有这样的url, www.myapp.com/my_page_here
这是可能的,如果是这样,我该如何解决这个问题?
您只需在routes.rb
文件中使用任何resources
或namespace
块之外的routes.rb
:
get 'my_page_here ', :to => 'home#index'
假设您使用的是Rails 3+,请勿使用match
。 这可能很危险,因为如果页面接受表单中的数据,则应该接受POST请求。 match
将允许对具有副作用的动作的GET请求 – 这是不好的。
始终尽可能使用get
, put
, post
或这些变体。
要获取路径助手,请尝试:
get 'my_page_here ', :to => 'home#index', :as => :my_page
这样,在您的视图中, my_page_path
将等于http://{domain}/my_page_here
你只需要制作一个路由规则来匹配那个url,在这种情况下它会是这样的
match 'my_page_here' => 'your_controller#your_action'
您的控制器和操作将指定该页面的行为
所以你可以做到
match 'my_page_here' => 'home#index'
要么
get 'my_page_here', :to => 'home#index'
正如其他答复所建议的那样
如果您有这样的控制器,用于家庭控制器中的索引操作
有关详细信息,请参阅http://guides.rubyonrails.org/routing.html
还可以看到Ruby on Rails Routes – get和match之间的区别