(Ruby on Rails)在Routes.rb中构建URL



我是Rails的新手,所以请在这里忍受我。我正在尝试构建以下URL:

localhost:3000/products/toyota

所以,主页(Localhost:3000我在本地运行该项目时(和产品页面(Localhost:3000/products(已经存在,但是我正在尝试创建一个Toyota页面(Localhost:3000/products/toyota(。Toyota页面有独立于产品页面的视图(HAML页面/JavaScript(。

所以,我在config/doutes.rb

中尝试了此操作。
get 'toyota', to: 'static#products#toyota'

但是Toyota_url带我去Localhost:3000/Toyota而不是Local主机:3000/Products/Toyota。

关于如何解决此问题的任何想法?

您需要此而是

get '/products/toyota', to: 'static#products#toyota', as: 'toyota'

希望有帮助!

如注释中所述:是get 'my/full/path', to: ....,请尝试get 'products/toyota', to: 'static#products#toyota', :as => 'toyota'

,但我建议的是以下内容。为了使其更加灵活 - 我认为您还将拥有其他供应商/产品,请创建一个动作:

路线:

get 'products/:vendor', to: 'products#vendor'

控制器:

def vendor(id)
   @car_or_whatever = Product.find_by(....)     
end

查看:

# file views/products/vendor.html.erb
<some fancy html>
   @car_or_whatever.name
   ... etc

来自文档:http://guides.rubyonrails.org/routing.html#generating-paths-paths-and-urls-from-code

最新更新