# Passing parameters on rails

**URL:** <https://www.ruby-forum.com/t/passing-parameters-on-rails/133798>\
**Category:** Rails\
**Created:** [April 3, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/passing-parameters-on-rails/133798 "2008-04-03T19:35:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![add](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/0ea827/32.png) [@add](https://www.ruby-forum.com/u/add)\
**Post date:** [April 3, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/passing-parameters-on-rails/133798/1 "2008-04-03T19:35:40Z")

</div>

Hi, I want to know how I can pass the following parameters …  
I have  
faculdade: id, name  
curso : id, faculdade\_id

and the following code

#view

\<%= select(:curso, :faculdade\_id , Faculdade.find(:all).collect {|c|  
[c.nome, c.id] }) %\>

 Preço do Curso

controller

def lista\_faculdade  
teste = params[:teste]  
@faculdade\_teste = Curso.find(:all).select do |student|  
student.faculdade\_id = teste  
end

if I try to do it manually (with /cursos/lista\_faculdade?teste=3 for  
example it works  
all I need to know is how to pas the parameter from the select to the  
href link  
Thanks in advance! =\*

---

<div class="post-metadata">

**Author:** ![add](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/0ea827/32.png) [@add](https://www.ruby-forum.com/u/add)\
**Post date:** [April 3, 2008, 8:46pm UTC](https://www.ruby-forum.com/t/passing-parameters-on-rails/133798/2 "2008-04-03T20:46:06Z")

</div>

On 3 Apr 2008, at 18:35, Laura Laizer smith wrote:

> \<%= select(:curso, :faculdade\_id , Faculdade.find(:all).collect {|c|  
> [c.nome, c.id] }) %\>
> 
> Preço do Curso
> 
> controller

You’ll need to use some javascript to do that, unless you use a form  
(you can always style the button to look like what you want).

Fred

---

<div class="post-metadata">

**Author:** ![add](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/0ea827/32.png) [@add](https://www.ruby-forum.com/u/add)\
**Post date:** [April 4, 2008, 12:43am UTC](https://www.ruby-forum.com/t/passing-parameters-on-rails/133798/3 "2008-04-04T00:43:34Z")

</div>

Yes, look into observe\_field in the API, and you could easily write a  
small RJS template which replaced the link with one with the  
appropriately inserted value.

Julian.

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) VIDEO #3  
OUT APRIL 6  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![add](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/0ea827/32.png) [@add](https://www.ruby-forum.com/u/add)\
**Post date:** [April 4, 2008, 1:06am UTC](https://www.ruby-forum.com/t/passing-parameters-on-rails/133798/4 "2008-04-04T01:06:26Z")

</div>

Just also of interest, is this:

> def lista\_faculdade  
> teste = params[:teste]  
> @faculdade\_teste = Curso.find(:all).select do |student|  
> student.faculdade\_id = teste  
> end

You should probably be using Curso.find\_by\_faculdade\_id(teste) as this  
will put the searching at the database level (this is what databases  
are good at), and not force Rails to load the entire dataset into  
memory to only then filter it back using Ruby’s select iterator.

Julian.

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) VIDEO #3  
OUT APRIL 6  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![add](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/0ea827/32.png) [@add](https://www.ruby-forum.com/u/add)\
**Post date:** [April 4, 2008, 5:07pm UTC](https://www.ruby-forum.com/t/passing-parameters-on-rails/133798/5 "2008-04-04T17:07:16Z")

</div>

Laura Laizer smith wrote:

> > You should probably be using Curso.find\_by\_faculdade\_id(teste) as this  
> > will put the searching at the database level (this is what databases  
> > are good at), and not force Rails to load the entire dataset into  
> > memory to only then filter it back using Ruby’s select iterator.
> 
> thanks i will be studying now and see how far I can get !

I tried the following

#view

Search:

\<%= observe\_field ‘faculdade\_find’,  
:frequency =\> 0.5,  
:update =\> ‘results’,  
:url =\> { :controller =\> ‘curso’, :action=\> ‘lista\_faculdade’ },  
:with =\> “‘search\_text=’ + escape(value)”%\>

#controller

def lista\_faculdade  
teste = params[‘search\_text’]  
@faculdade\_teste = Curso.find(:all).select do |curso|  
curso.nome\_curso = teste  
end  
@curso\_pages, @cursos = paginate\_collection @faculdade\_teste,  
:per\_page =\> 10  
end  
def show  
@curso = Curso.find(params[:id])  
end

but it’s not finding, instead it’s changing the name of all cursos …  
Can someone tell me what am I doing wrong ?

---

<div class="post-metadata">

**Author:** ![add](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/0ea827/32.png) [@add](https://www.ruby-forum.com/u/add)\
**Post date:** [April 4, 2008, 2:34am UTC](https://www.ruby-forum.com/t/passing-parameters-on-rails/133798/6 "2008-04-04T02:34:42Z")

</div>

> You should probably be using Curso.find\_by\_faculdade\_id(teste) as this  
> will put the searching at the database level (this is what databases  
> are good at), and not force Rails to load the entire dataset into  
> memory to only then filter it back using Ruby’s select iterator.

thanks i will be studying now and see how far I can get !

---

<div class="post-metadata">

**Author:** ![add](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/0ea827/32.png) [@add](https://www.ruby-forum.com/u/add)\
**Post date:** [April 4, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/passing-parameters-on-rails/133798/7 "2008-04-04T17:38:50Z")

</div>

On 4 Apr 2008, at 16:07, Laura Laizer smith wrote:

> def lista\_faculdade  
> teste = params[‘search\_text’]  
> @faculdade\_teste = Curso.find(:all).select do |curso|  
> curso.nome\_curso = teste  
> I suspect you want curso.nome\_curso == teste, otherwise as you note  
> you will just be assigning (and since you are assigning non null  
> values you are effectively selecting them all)

Fred

---

<div class="post-metadata">

**Author:** ![add](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/0ea827/32.png) [@add](https://www.ruby-forum.com/u/add)\
**Post date:** [April 4, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/passing-parameters-on-rails/133798/8 "2008-04-04T18:46:15Z")

</div>

Frederick C. wrote:

> On 4 Apr 2008, at 16:07, Laura Laizer smith wrote:
> 
> > def lista\_faculdade  
> > teste = params[‘search\_text’]  
> > @faculdade\_teste = Curso.find(:all).select do |curso|  
> > curso.nome\_curso = teste  
> > I suspect you want curso.nome\_curso == teste, otherwise as you note  
> > you will just be assigning (and since you are assigning non null  
> > values you are effectively selecting them all)
> 
> Fred

Half of my problems are solved  
I can do search now  
but if I try Math for instance it opens 4 frames inside the frame of app  
to then do the math search  
the same number of letter I type the same frames it open inside the  
application I think I am doing something very stupid But I dont know  
what …

---

<div class="post-metadata">

**Author:** ![add](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/0ea827/32.png) [@add](https://www.ruby-forum.com/u/add)\
**Post date:** [April 4, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/passing-parameters-on-rails/133798/9 "2008-04-04T18:49:54Z")

</div>

Laura Laizer smith wrote:

> Frederick C. wrote:

You are doing it inside a for condition …  
Dunno you can replace it but when you use do it’s a iteration…
