rails - Pass id parameter on a link_to / routing - ruby-on-rails

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.

Related

Rails routes have no id

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/

rails button_to current_user correct path

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)

No Routing error when trying to add custom action to a resource

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

Pass value to controller from link name Rails

I have a list of names, that contain this:
<td><%= product.date %></td>
and it is in my index page. (It is the list of my applications)
I want to assign a custom link to that, and pass THAT value to the controller:
I tried this:
<td><%= link_to product.date, {:controller => "product", :action => "sort_by_date", <how do I pass the 'product.date' string?> }%></td>
I read the rails route guide but I couldn't find help
Thanks
what rakes routes returns:
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
root /
products#home.html
use like below
<%= link_to product.date, {:controller => "product", :action => "sort_by_date", :product_date => product.date %>
and in your controller you should get the date in params[:product_date]
You can also use resource helper function to pass custom parameters like below.
<%= link_to product.date, products_path(:product_date => product.date)
This method will route to the ProductsController#index with a params[:product_date]
Edit:
I just tried with following
resources :products do
collection do
get 'sort_by_date' => 'products#sort_by_date'
end
end
The rake routes are as following
sort_by_date_products GET /products/sort_by_date(.:format) products#sort_by_date
To generate the URL it will be better to use the resource helper function like below
sort_by_date_products_path(:product_date => '1/1/2012')
This will generate a url like /product/sort_by_date?product_date=1/1/2012 which will call the sort_by_date method of ProductsController and also have params[:product_date] available.
Lets check if its work...

Edit multiple records, controller & routing

I am following railscast 198 http://railscasts.com/episodes/198-edit-multiple-individually, trying to update it to rails 3, and am stuck with a routing error. The route http://localhost:3000/orders/edit_individual gives me the error:
ActiveRecord::RecordNotFound in OrdersController#show - Couldn't find Order with ID=edit_individual
I have updated and used his rails 2 routing
map.resources :products, :collection => { :edit_individual => :post, :update_individual => :put }
to the rails 3 convention as described by engineyard http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ (updated to my needs)
resources :orders do
collection do
post :edit_individual
put :update_individual
end
end
Here's what I've tried:
I've tried changing the routes resources as suggested by the answer to: Adding an action to an existing controller (Ruby on Rails) but the same error still shows. I've tried removing the cancan load and authorize resources, the 'resources :orders' route entry, as well as the 'show' entry in the controller, but another error indicates it's still trying to show a record with ID='edit_individual' instead of treating "edit_individual" like a route.
Here are my routes and controller,
myapp::Application.routes.draw do
resources :products
resources :roles
devise_for :users #, :controllers => { :registrations => "registrations" }
# match 'dashboard' => 'user_dashboard#index', :as => 'user_root'
resources :orders do
collection do
post :edit_individual
put :update_individual
end
end
resources :orders
class OrdersController < ApplicationController
load_and_authorize_resource # cancan method
def index
end
def show
end
def edit_individual #from railscast 198
#orders = current_user.customer_orders
end
def update_individual
#orders = Order.update(params[:orders].keys, params[:orders].values).reject { |p| p.errors.empty? }
if #orders.empty?
flash[:notice] = "Orders updated"
redirect_to orders_url
else
render :action => "edit_individual"
end
end # ...etc.
I've removed the cancan methods, but is it the culprit still? I've tried everything I can think of and am at a dead end.. any ideas?
edit:
the output from command prompt:
Started GET "/orders/edit_individual" for 127.0.0.1 at Thu Jun 30 11:19:02 -0700
2011
Processing by OrdersController#show as HTML
Parameters: {"id"=>"edit_individual"}
←[1m←[36mOrder Load (0.0ms)←[0m ←[1mSELECT "orders".* FROM "orders" WHERE "or
ders"."id" = 0 LIMIT 1←[0m
Completed in 2584ms
ActiveRecord::RecordNotFound (Couldn't find Order with ID=edit_individual):
and the route in my html:
<%= link_to 'Update Payments Received', edit_individual_orders_path %>
and rake routes:
edit_individual_orders POST /orders/edit_individual(.:format) {:action=>"edit_individual", :controller=>"orders"}
update_individual_orders PUT /orders/update_individual(.:format) {:action=>"update_individual", :controller=>"orders"}
orders GET /orders(.:format) {:action=>"index", :controller=>"orders"}
POST /orders(.:format) {:action=>"create", :controller=>"orders"}
new_order GET /orders/new(.:format) {:action=>"new", :controller=>"orders"}
edit_order GET /orders/:id/edit(.:format) {:action=>"edit", :controller=>"orders"}
order GET /orders/:id(.:format) {:action=>"show", :controller=>"orders"}
PUT /orders/:id(.:format) {:action=>"update", :controller=>"orders"}
DELETE /orders/:id(.:format) {:action=>"destroy", :controller=>"orders"}
You should probably run "rake routes" and see what route it gives for "edit_individual" action.
Also your log
Started GET "/orders/edit_individual" for 127.0.0.1 at Thu Jun 30 11:19:02 -0700
says that you are calling a post action as get.
Try below
resources :orders do
collection do
get :edit_individual
put :update_individual
end
end
OR either way you can use
<%= link_to 'Update Payments Received', edit_individual_orders_path, :method => "post" %>
So the error message: OrdersController#show - Couldn't find Order with ID=edit_individual tells me that for whatever reason you're being routed to the 'show' action, which is inherently expecting something like
/orders/1
meaning it's a member not a collection. Are you certain that the url being hit in your server output matches /orders/edit_individual?

Resources