Passing hard-coded values to controller


#1

given the following code snippet:

class PatientsController < ApplicationController

def find
@patients = Patient.find(:all, :conditions => { :location =>
[params:search_string] })
end

I would like to add a few hard coded links (i.e. show me all the
patients in a particular location)

This makes sense to me:

<% link_to “=Hospital 1 census”, {:controller=>"/
patients", :action=>“find”, :search_string=>“Hospital 1”} %>

but gives me error “Couldn’t find Patient with ID=find” (The above
link generates the URL example.com/patients/find?
search_string=Hillcrest)

I tried playing around with routes.rb, but got nowhere. What am I
doing wrong?

Thanks,
–b


#2

The way you are receiving parameter is wrong it should be like this

@patients = Patient.find(:all, :conditions => { :location =>params
[:search_string] })

GUL


#3

On May 6, 8:40 am, big poppa removed_email_address@domain.invalid wrote:

<% link_to “=Hospital 1 census”, {:controller=>"/
patients", :action=>“find”, :search_string=>“Hospital 1”} %>

but gives me error “Couldn’t find Patient with ID=find” (The above
link generates the URL example.com/patients/find?
search_string=Hillcrest)

I tried playing around with routes.rb, but got nowhere. What am I
doing wrong?

In addition to what Gul has said, assuming you have the default
map.resources :patients then you need to tell rails about your find
action ( take a look at the routing guide at guides.rubyonrails.org
(hint: :collection ))

Fred


#4

Since find is not one of the standard actions, have you included a
specific
route for patients/find? The default routes generated by map.resources
:patients will assume that find is a patient id, expecting to see
patients/2
for example.

Colin

2009/5/6 big poppa removed_email_address@domain.invalid


#5

sorry, that’s a typo. it appears the way you suggested in my code (it
compiles).

i’ll look into the routing some more, i was ready to bash my head in
last night and couldn’t take it any more…

–b


#6

Having thought further about this I would suggest that you may going
about
it the wrong way anyway. I think what you are trying to do is select a
set
of patients for display. The default action for showing a set of
patients
is the index action, which by default would show all patients and is
accessed by example.com/patients. You could use the URL
example.com/patients?search_string=Hillcrest (or whatever) and test for
the
presence of params[:search_string] in the index method of the
controller.
Then no special routing is required.

Colin

2009/5/7 Colin L. removed_email_address@domain.invalid


#7

thanks for your input and thoughtful reply. I like your proposal and
I’ll give it a shot

Thanks,
–b


#8

2009/5/7 big poppa removed_email_address@domain.invalid

Suppose I wanted to do this. Wouldn’t the syntax be (in config/
routes.rb)

map.connect ‘/patients/find’, :controller=>‘patient’, :action=>‘find’

Make sure it is before map.resources :patient otherwise this will be
seen
first and you will get the problem you have described. Also I do not
put a
leading / when using map.connect but I do not know whether that is an
issue
or not. So I would put map.connect ‘patients/find’ …

Use
rake routes
to show you the current mapping.

Colin


#9

Suppose I wanted to do this. Wouldn’t the syntax be (in config/
routes.rb)

map.connect ‘/patients/find’, :controller=>‘patient’, :action=>‘find’

prepended to the standard, scaffold generated

map.connect ‘:controller/:action/:id’
map.connect ‘:controller/:action/:id.:format’

–b