I have a controller, clients_controller
, with corresponding index, show, edit, delete, new & form views. Is there a way to create a new view like clients/prospects.html.erb
that acts the same way as clients/index.html.erb
, except is routed at clients/prospects/
?
I've tried this:
match '/clients/prospects' => 'clients#prospects'
And some other things in routes.rb
, but of course get the error "Couldn't find Client with id=prospects".
The goal here is basically to have a prospects view and a clients view, and by simply switching the hidden field to a 1, it (in the user's mind) turns a prospect into a client (it's a CRM-like app).
There's a couple of things you need to do. First you need to put the your custom route before any generic route. Otherwise Rails assumes the word "prospects" is an id for the show action. Example:
get '/clients/prospects' => 'clients#prospects' # or match for older Rails versions
resources :clients
Also you need to copy / paste the index method in your ClientsController and name it prospects. Example:
class ClientsController < ApplicationController
def index
@clients = Client.where(prospect: false)
end
def prospects
@prospects = Client.where(prospect: true)
end
end
Lastly, you need to copy the index.html.erb view and name the copy prospects.html.erb. In the example above you would have to work with the @prospects instance variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With