I'm new to Rails, so bear with me here. I'm trying to build the following URL:
localhost:3000/products/toyota
So, the homepage (localhost:3000 when I run the project locally) and the products page (localhost:3000/products) already exist, but I'm trying to create a toyota page (localhost:3000/products/toyota). The Toyota page has its own view (haml page/javascript) independent of the products page.
So, I tried this in config/routes.rb
get 'toyota', to: 'static#products#toyota'
but the toyota_url takes me to localhost:3000/toyota instead of localhost:3000/products/toyota.
Any ideas on how to fix this?
You need this instead
get '/products/toyota', to: 'static#products#toyota', as: 'toyota'
Hope that helps!
As stated in the comment: it's get 'my/full/path', to: ....
so try get 'products/toyota', to: 'static#products#toyota', :as => 'toyota'
But what I would recommend is the following. To keep it a bit more flexible - I assume you will have other vendors/products as well, create an action:
Route:
get 'products/:vendor', to: 'products#vendor'
Controller:
def vendor(id)
@car_or_whatever = Product.find_by(....)
end
View:
# file views/products/vendor.html.erb
<some fancy html>
@car_or_whatever.name
... etc
From the docs: http://guides.rubyonrails.org/routing.html#generating-paths-and-urls-from-code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.