0

Right now I have the route as below;

resources :invoices do
  collection do
    match 'search' => 'invoices#search', via: [:get, :post], as: :search
  end
end

But I would like to get 'deliveries#search' instead of 'invoices#search'.

I successfully customized for the below routes;

get 'deliveries', to: 'invoices#index', as: :deliveries

get 'deliveries/new', to: 'invoices#new', as: :delivery

get 'deliveries/:id/edit', to: 'invoices#edit', :as => :del

I have tried but unable to customized for the 'deliveries#search'.

Any suggestions are most welcome.

Thank you in advance.

2 Answers 2

1
resources :invoices, path: 'deliveries' do
 collection do
  match 'search' => 'invoices#search', via: [:get, :post], as: :search
 end
end

it will generate route with prefix deliveries like /deliveries/your_action

1

Change your routes as follows

resources :deliveries, controller: 'invoices', only: [:index, :new, :edit]
resources :deliveries, only: [] do
  collection do
    get :search
    post :search
  end 
end

This will generate following routes for you:

        deliveries GET  /deliveries(.:format)           invoices#index
      new_delivery GET  /deliveries/new(.:format)       invoices#new
     edit_delivery GET  /deliveries/:id/edit(.:format)  invoices#edit
search_deliveries  GET  /deliveries/search(.:format)    deliveries#search
                   POST /deliveries/search(.:format)    deliveries#search

EDIT

According to your comment I think you just need to use different controller

Try this

resources :deliveries, controller: 'invoices', only: [:index, :new, :edit] do
  collection do
    get :search
    post :search
  end
end

This will generate following routes:

search_deliveries  GET  /deliveries/search(.:format)    invoices#search
                   POST /deliveries/search(.:format)    invoices#search
        deliveries GET  /deliveries(.:format)           invoices#index
      new_delivery GET  /deliveries/new(.:format)       invoices#new
     edit_delivery GET  /deliveries/:id/edit(.:format)  invoices#edit
4
  • Thanks for the reply.But I can't do this as I have got invoices controller and invoice model.What I want is when I do the search instead of showing localhost:3000/invoices/search I want to show as localhost:3000/deliveries/search. Sep 13, 2016 at 6:21
  • so you just want to show them as /deliveries/search but you want to use invoices controller Sep 13, 2016 at 6:25
  • Thanks.That is working fine but when I go to new or edit actions it gives me the errors as NoMethodError in Invoices#edit and the extracted source it is showing as , <%= simple_form_for(@invoice) do |f| %> Sep 13, 2016 at 6:35
  • you might need to change your path_name acordingly as they are changed to new_delivery_path and edit_delivery_path Sep 13, 2016 at 6:41

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.