Forum: Ruby on Rails passing hard-coded values to controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
61e6493a2b24f914bfb4781319708bd3?d=identicon&s=25 Ben Newman (cbnewman)
on 2009-05-06 09:41
(Received via mailing list)
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
99212827a5ef36f5a7e04dff9880c87b?d=identicon&s=25 Gul (Guest)
on 2009-05-06 17:51
(Received via mailing list)
The way you are receiving parameter is wrong it should be like this

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

GUL
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-06 20:29
(Received via mailing list)
On May 6, 8:40 am, big poppa <cbnew...@gmail.com> 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
61e6493a2b24f914bfb4781319708bd3?d=identicon&s=25 Ben Newman (cbnewman)
on 2009-05-06 23:47
(Received via mailing list)
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
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-07 09:07
(Received via mailing list)
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 <cbnewman@gmail.com>
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-07 15:39
(Received via mailing list)
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 Law <clanlaw@googlemail.com>
61e6493a2b24f914bfb4781319708bd3?d=identicon&s=25 Ben Newman (cbnewman)
on 2009-05-07 21:32
(Received via mailing list)
thanks for your input and thoughtful reply. I like your proposal and
I'll give it a shot

Thanks,
--b
61e6493a2b24f914bfb4781319708bd3?d=identicon&s=25 Ben Newman (cbnewman)
on 2009-05-07 22:08
(Received via mailing list)
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
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-07 22:57
(Received via mailing list)
2009/5/7 big poppa <cbnewman@gmail.com>

>
> 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
This topic is locked and can not be replied to.