# Passing hard-coded values to controller

**URL:** <https://www.ruby-forum.com/t/passing-hard-coded-values-to-controller/167274>\
**Category:** Rails\
**Created:** [May 6, 2009, 9:41am UTC](https://www.ruby-forum.com/t/passing-hard-coded-values-to-controller/167274 "2009-05-06T09:41:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbnewman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cbnewman](https://www.ruby-forum.com/u/cbnewman)\
**Post date:** [May 6, 2009, 9:41am UTC](https://www.ruby-forum.com/t/passing-hard-coded-values-to-controller/167274/1 "2009-05-06T09:41:16Z")

</div>

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](http://example.com/patients/find)?  
search\_string=Hillcrest)

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

Thanks,  
–b

---

<div class="post-metadata">

**Author:** ![cbnewman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cbnewman](https://www.ruby-forum.com/u/cbnewman)\
**Post date:** [May 6, 2009, 5:51pm UTC](https://www.ruby-forum.com/t/passing-hard-coded-values-to-controller/167274/2 "2009-05-06T17:51:47Z")

</div>

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

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

GUL

---

<div class="post-metadata">

**Author:** ![cbnewman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cbnewman](https://www.ruby-forum.com/u/cbnewman)\
**Post date:** [May 6, 2009, 8:29pm UTC](https://www.ruby-forum.com/t/passing-hard-coded-values-to-controller/167274/3 "2009-05-06T20:29:01Z")

</div>

On May 6, 8:40 am, big poppa [removed\_email\_address@domain.invalid](mailto: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](http://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](http://guides.rubyonrails.org)  
(hint: :collection ))

Fred

---

<div class="post-metadata">

**Author:** ![cbnewman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cbnewman](https://www.ruby-forum.com/u/cbnewman)\
**Post date:** [May 7, 2009, 9:07am UTC](https://www.ruby-forum.com/t/passing-hard-coded-values-to-controller/167274/4 "2009-05-07T09:07:23Z")

</div>

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](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cbnewman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cbnewman](https://www.ruby-forum.com/u/cbnewman)\
**Post date:** [May 6, 2009, 11:47pm UTC](https://www.ruby-forum.com/t/passing-hard-coded-values-to-controller/167274/5 "2009-05-06T23:47:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cbnewman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cbnewman](https://www.ruby-forum.com/u/cbnewman)\
**Post date:** [May 7, 2009, 3:39pm UTC](https://www.ruby-forum.com/t/passing-hard-coded-values-to-controller/167274/6 "2009-05-07T15:39:55Z")

</div>

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](http://example.com/patients). You could use the URL  
[example.com/patients?search\_string=Hillcrest](http://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](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cbnewman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cbnewman](https://www.ruby-forum.com/u/cbnewman)\
**Post date:** [May 7, 2009, 9:32pm UTC](https://www.ruby-forum.com/t/passing-hard-coded-values-to-controller/167274/7 "2009-05-07T21:32:02Z")

</div>

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

Thanks,  
–b

---

<div class="post-metadata">

**Author:** ![cbnewman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cbnewman](https://www.ruby-forum.com/u/cbnewman)\
**Post date:** [May 7, 2009, 10:57pm UTC](https://www.ruby-forum.com/t/passing-hard-coded-values-to-controller/167274/8 "2009-05-07T22:57:22Z")

</div>

2009/5/7 big poppa [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![cbnewman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cbnewman](https://www.ruby-forum.com/u/cbnewman)\
**Post date:** [May 7, 2009, 10:08pm UTC](https://www.ruby-forum.com/t/passing-hard-coded-values-to-controller/167274/9 "2009-05-07T22:08:36Z")

</div>

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
