Reputation: 2883
My application has deals which have orders. In my admin area I want to be able to process the orders manually.
In my access/deals view
<%= link_to "Process Orders", "Not sure what I put here?" %>
in my access/deals_controller
def process_orders
@deals = Deal.find(params[:id]
@orders = @deals.orders.where("state" == ?, "pending")
@orders.each do |order|
#order processing code here
end
end
How should I structure my link_to method to call the process_orders method in my admin/deals controller?
I thought something like
<%= link_to "Process Orders", access_deal_path(deal) %>
which give me the following url
localhost:3000/access/deals/9
how do I get something like
localhost:3000/access/deals/9/process_orders
I'm also open to suggestions on moving the processing_orders method to model or helper if that is a better way of doing this.
My excerpt of my routes file.
resources :deals do
resources :orders
end
namespace "access" do
resources :deals, :podcasts, :pages, :messages
end
Upvotes: 6
Views: 6553
Reputation: 12397
It would be nicer if you move the process_orders
method to your OrdersController
but this is your decision.
To get your code working just add this route to your routes.rb
:
resources :deals do
get :process_orders
resources :orders
end
and call it with <%= link_to("Process Orders", deal_process_orders(deal)) %>
.
Upvotes: 1
Reputation: 1807
You can do 1 of the following:
Create a custom route:
match 'access/deals/:id/process_orders' => 'access/deals#process_orders', :as => 'access_deal'
then you can use this link_to:
<%= link_to "Process Orders", access_deal_path(deal) %>
OR
Add a member route:
namespace "access" do
resources :deals do
member do
get :process_orders
end
end
end
Your link_to will look something like this:
<%= link_to "Process Orders", process_orders_access_deal_path(deal) %>
Upvotes: 4
Reputation: 11647
If you can use a specific _path that be great, but I know I've been in situations where I wanted more explicit control.
The Ruby API here: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
Gives this example:
link_to "Profile", :controller => "profiles", :action => "show", :id => @profile
Upvotes: 2