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.
Related
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/
What path can I use to display a current_user's own listed products?
= button_to "View My Products", root_path, method: :get, class: "button"
Instead of root_path I tried to use #product.user.all which did not work.
Thanks!
edit:
user GET /users/:id(.:format) users#show
search_products GET /products/search(.:format) products#search
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
welcome_index GET /welcome/index(.:format) welcome#index
root GET / welcome#index
and all of Devise's standard routes.
I think you want one product controller index action and association between user and products
Then in products#index action
def index
#products = current_user.products
end
and your link will be
= link_to 'View My Products', {controller: :products, action: :index}
If your product#index action for non authenticated users then make that action conditional
like
#products = (current_user ? current_user.products : Product.all)
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
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
I have a create product page that takes infomation like name description. I want to make another page that allows the user to add a photo. To that product page. So i created a method for this page.
def pics
#product = Product.find(params[:id])
#photo = Photo.new
#product.photos.build
end
And also i added this to the config file
resources :products do
collection do
get :pics
end
end
And my rake routes look like this.
pics_products GET /products/pics(.:format) products#pics
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
Right now it's not taking the ID of the product it's in. How can i turn it into pics_products GET /products/:id/pics(.:format)?
You should change you routes like this:
resources :products do
member do
get :pics
end
end