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
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/
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.
I am trying to add an custom action to a resource but I get a routing error No route matches [GET] "/products/list/up". I have tried commenting out the URI in routes.rb, but they also don't work. What am I doing wrong?
I have this in the routes.rb:
namespace :api, :defaults =>{format: 'json'} do
scope module: :v1 ,constraints: ApiConstraints.new(version:1, default: true) do
resources :products do
member do
match "/list/up" =>"products#product_list" ,:via=>:get
#get "/list/up" , :action=>"product_list"
#get "/list/up" , :to=>"product_list"
end
end
end
end
in the products_controller.rb:
def product_list
#products= Product.all
respond_to do |format|
format.json { render json: #products.to_json}
end
end
Try collection instead of member as you don't provide any id in the path:
resources :products do
collection do # <-----<
get "/list/up" =>"products#product_list"
end
end
Running rake routes gives:
list_up_products GET /products/list/up(.:format) products#product_list
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