Rails routes have no id - ruby-on-rails

I added resource :products, :path => 'catalog/' to my routes.rb, but my routs look like this:
products POST /catalog(.:format) products#create
new_products GET /catalog/new(.:format) products#new
edit_products GET /catalog/edit(.:format) products#edit
GET /catalog(.:format) products#show
PATCH /catalog(.:format) products#update
PUT /catalog(.:format) products#update
DELETE /catalog(.:format) products#destroy
Why do they have no :ids? For example, product#show should have URI /products/:id(.:format), right?
Also, = link_to products_path(product), class: 'product' do leads me to http://localhost:3000/catalog.1

You should use resources :products instead of resource :proucts. For more info: https://cbabhusal.wordpress.com/2015/10/21/rails-routes-difference-between-resource-and-resources-in-routes-rb/

Related

In Rails, how can I reliably look for a specific parameter when using nested routes?

Assume the following routes:
resources :products do
resources :images
end
This creates the following routes:
product_images GET /products/:product_id/images(.:format) images#index
POST /products/:product_id/images(.:format) images#create
new_product_image GET /products/:product_id/images/new(.:format) images#new
edit_product_image GET /products/:product_id/images/:id/edit(.:format) images#edit
product_image GET /products/:product_id/images/:id(.:format) images#show
PATCH /products/:product_id/images/:id(.:format) images#update
PUT /products/:product_id/images/:id(.:format) images#update
DELETE /products/:product_id/images/:id(.:format) images#destroy
products GET /products(.:format) products#index
POST /products(.:format) products#create
new_product GET /products/new(.:format) products#new
edit_product GET /products/:id/edit(.:format) products#edit
product GET /products/:id(.:format) products#show
PATCH /products/:id(.:format) products#update
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
Now assume I have a ProductController and a ProductImageController that both inherit from ApplicationController. In ApplicationController I want to be able to have a property called active_product that is automatically set to the relevant product. Something like the following:
def active_product
#product = Product.find(params[:id])
end
The problem here is that sometimes the parameter is called :id and sometimes it's called :product_id. Is there a better pattern for being able to extract the value of the product id?
Sure, how about doing
def active_product
#product = Product.find params[:product_id] || params[:id]
end

How to change default Product Route in Spree?

Any ideas on what route it would take to change this URL:
http://domain.com/products/item1
To show and link to like this:
http://domain.com/item1/products
Current routes look like this:
root_path GET / spree/home#index
products_path GET /products(.:format) spree/products#index
POST /products(.:format) spree/products#create
new_product_path GET /products/new(.:format) spree/products#new
edit_product_path GET /products/:id/edit(.:format) spree/products#edit
product_path GET /products/:id(.:format) spree/products#show
PATCH /products/:id(.:format) spree/products#update
PUT /products/:id(.:format) spree/products#update
DELETE /products/:id(.:format) spree/products#destroy
You would need to prepend the route:
get '/:id/products/' => 'spree/products#show', as: :product
to your config/routes.rb file.

rails - Pass id parameter on a link_to / routing

I am trying to add a checkout link in order to direct a client to checkout view.
I have a ProductsController, and an OrdersController where I defined the action for checkout. I am doing this because I set it so a product can have many orders.
class OrdersController < ApplicationController
def checkout
#product = Product.find(params[:id])
end
In my show.html.erb I added the line:
<%= link_to 'Contribute Now', order_checkout_path, :id => #product, :controller => "orders", :method => :get %>
And my routes look like:
root :to => 'products#index'
match '/products' => 'products#index'
get 'order/checkout'
resources :products
resources :orders
After running rake routes I get:
root / products#index
products /products(.:format) products#index
order_checkout GET /order/checkout(.:format) order#checkout
GET /products(.:format) products#index
POST /products(.:format) products#create
new_product GET /products/new(.:format) products#new
edit_product GET /products/:id/edit(.:format) products#edit
product GET /products/:id(.:format) products#show
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
I have also defined a checkout.html.erb template.
After doing all that, I keep on getting an error of:
Routing Error
uninitialized constant OrderController
Try running rake routes for more information on available routes.
What am I missing?
You want get 'orders/checkout', corresponding to the plural form of "order" used in your controller name.

Route to action of controller

Previous i used:
data: {autocomplete_source: categories_path} %>
To point to the action index in categories controller.All worked fine!
Now i created an new action in categories controller
def search
#categories = Category.order(:name).where("name like ?", "%#{params[:term]}%")
render json: #categories.map(&:name)
end
And tried to point to that action:
data: {autocomplete_source: search_categories_path} %>
But i get the error:
undefined local variable or method `search_categories_path' for #<#<Class:0x51844c8>:0x5375820>
What did i wrong? Thanks!
My routes:
products GET /products(.:format) products#index
POST /products(.:format) products#create
new_product GET /products/new(.:format) products#new
edit_product GET /products/:id/edit(.:format) products#edit
product GET /products/:id(.:format) products#show
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
Routes:
Autorails::Application.routes.draw do
resources :products
resources :categories do
collection do
:search
end
end
Check rake routes if that route really exists under that name.
For more information see http://guides.rubyonrails.org/routing.html#path-and-url-helpers
You should have something like this in your routes.rb:
resources :categories do
collection do
get :search
end
end
You should do something like this
resources :categories do
collection do
get :search
end
end

Changing every route from /product to /eshop in Rails Route 3

in my routes.rb i have foll entries:-
resources :products do
get 'get_products', :on => :collection
get 'show_product_details',:on=>:member
get 'buy_this',:on=>:collection
get 'search_product',:on=>:collection
end
i want to change every /products to /eshop in the url.
i am not sure but can i use :path=>:eshop.Will it will be also applicable to the sub routes as well such as eshop/get_products,eshop/buy_this...etc.
You can modify your routes and run rake routes in the terminal to check the paths.
resources :products, :path => 'eshop', :as => 'eshop' do
get 'get_products', :on => :collection
get 'show_product_details',:on=>:member
get 'buy_this',:on=>:collection
get 'search_product',:on=>:collection
end
will produce these
get_products_eshop_index GET /eshop/get_products(.:format) products#get_products
show_product_details_eshop GET /eshop/:id/show_product_details(.:format) products#show_product_details
buy_this_eshop_index GET /eshop/buy_this(.:format) products#buy_this
search_product_eshop_index GET /eshop/search_product(.:format) products#search_product
eshop_index GET /eshop(.:format) products#index
POST /eshop(.:format) products#create
new_eshop GET /eshop/new(.:format) products#new
edit_eshop GET /eshop/:id/edit(.:format) products#edit
eshop GET /eshop/:id(.:format) products#show
PUT /eshop/:id(.:format) products#update
DELETE /eshop/:id(.:format) products#destroy

Resources