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)
Related
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
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.
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.
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