Reputation: 579
This feels dumb, but so far everything I've read makes me feel like I'm doing it right but it's still not working.
I'm using a form_tag to submit params to a custom controller action. Instead of the action I indicate, it seems intents on submitting to the show
action in my controller, which I need to reserve for profiles. Anyways here's the code (please excuse it's un-refactored state):
Doctors controller:
class DoctorsController < ApplicationController
def new
end
def show
@doctor_list = Doctor.find(params[:id])
end
def index
@doctor_list = Doctor.all
end
def search
end
def results
if params[:zip] && params[:zip].length === 5 && params[:zip]
@doctor_list = Doctor.where("zip = ?", params[:zip])
elsif params[:id]
begin
@doctor_list = []
@doctor_list<<Doctor.find(params[:id])
rescue
flash.now[:errors] = "That doctor does not exist!"
render 'search'
end
else
flash.now[:errors] = "That is not a valid zipcode!"
render 'search'
end
end
end
Routes:
resources :users
resources :doctors
root 'doctors#search'
get 'doctors/results' => 'doctors#results'
search.html.erb:
<% provide(:title, "Home") %>
<div class="hero">
<h1>Find an M.D.</h1>
<%= form_tag(doctors_results_path, method: "get") do %>
<%= label_tag("Zipcode: ") %>
<%= text_field_tag(:zip) %><br>
<%= submit_tag "FIND", class: "button"%>
<% end %>
</div>
Again, the issue is that I'm getting an error (Couldn't find Doctor with 'id'=results
) because the form is using my show action vs. my results action. The application trace says the error is located at app/controllers/doctors_controller.rb:6:in 'show'
. As an added confusion, I don't really understand why it's sending "id"=>"results"
as part of the params hash on submit, but it seems like that might be a non-issue if it would use the correct controller action to begin with.
Thanks for any thoughts.
Upvotes: 2
Views: 1143
Reputation: 33542
Yes its a priority issue. Since the resources :doctors
is first, the GET show
will be given the priority than get 'doctors/results' => 'doctors#results'
Moving the get 'doctors/results' => 'doctors#results'
above resources :doctors
should solve your problem
#routes.rb
root 'doctors#search'
get 'doctors/results' => 'doctors#results'
resources :users
resources :doctors
Upvotes: 3