# Ajax and Rails

**URL:** <https://www.ruby-forum.com/t/ajax-and-rails/105101>\
**Category:** Rails\
**Created:** [May 25, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/ajax-and-rails/105101 "2007-05-25T19:39:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 25, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/ajax-and-rails/105101/1 "2007-05-25T19:39:06Z")

</div>

Hello, I have been trying to use ajax and rails for a drop down list and  
I can’t seem to get it to work.

This is what I am looking for, I have a drop down list, and as soon as  
the user selects an option, I want it to dissapear and make my content  
between the div I specified appear on the form.

I have like 3 different divs with different content in them for each  
drop down item.

I have tried to do it with RJS and declaring my own javascript functions  
but I can’t seem to get it to work.

Any help would be appreciated please?

Thanks

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 25, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/ajax-and-rails/105101/2 "2007-05-25T19:55:26Z")

</div>

Hi Matthew,

Matthew L. wrote:

> I have been trying to use ajax and rails for a drop  
> down list and I can’t seem to get it to work.

What do you mean ‘can’t seem to get it to work’? What is it doing / not  
doing. Where’s the relevant code? We can’t help much without more  
info.

Bill

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 25, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/ajax-and-rails/105101/3 "2007-05-25T19:58:13Z")

</div>

Bill W. wrote:

> Hi Matthew,
> 
> Matthew L. wrote:
> 
> > I have been trying to use ajax and rails for a drop  
> > down list and I can’t seem to get it to work.
> 
> What do you mean ‘can’t seem to get it to work’? What is it doing / not  
> doing. Where’s the relevant code? We can’t help much without more  
> info.
> 
> Bill

Sorry hehe, well when I click on the item I want, I tried to put an  
observer on the select id=“mediaType”, then i wanted to put in my RJS  
template something like if page[:mediaType][:value] = ‘Game’  
page.remove ‘mediaType’  
page.show ‘form\_game’  
end

I wanted to do that for all my mediaType items

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 25, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/ajax-and-rails/105101/4 "2007-05-25T20:11:27Z")

</div>

Matthew L. wrote:

> Sorry hehe, well when I click on the item I want, I tried  
> to put an observer on the select id=“mediaType”,

Is the observer firing? What do you see in Firebug when you click an  
item?

> then i wanted to put in my RJS template something like  
> if page[:mediaType][:value] = ‘Game’

What is this? Is this how you’re trying to get the value returned when  
the  
user makes a selection? Cause it ain’t the way to do it. Post the  
relevant  
code from the view that generates the request, the controller method  
that  
gets invoked, and the RJS template. Then we can give you some guidance.

Have you worked with RJS before? If not, I highly recommend Cody  
Fauser’s  
PDF at O’Reilly: “RJS Templates”. It’s $10 you’ll be glad you spent.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 25, 2007, 8:53pm UTC](https://www.ruby-forum.com/t/ajax-and-rails/105101/5 "2007-05-25T20:53:47Z")

</div>

Bill W. wrote:

> Matthew L. wrote:
> 
> > Sorry hehe, well when I click on the item I want, I tried  
> > to put an observer on the select id=“mediaType”,
> 
> Is the observer firing? What do you see in Firebug when you click an  
> item?
> 
> > then i wanted to put in my RJS template something like  
> > if page[:mediaType][:value] = ‘Game’
> 
> What is this? Is this how you’re trying to get the value returned when  
> the  
> user makes a selection? Cause it ain’t the way to do it. Post the  
> relevant  
> code from the view that generates the request, the controller method  
> that  
> gets invoked, and the RJS template. Then we can give you some guidance.
> 
> Have you worked with RJS before? If not, I highly recommend Cody  
> Fauser’s  
> PDF at O’Reilly: “RJS Templates”. It’s $10 you’ll be glad you spent.
> 
> Best regards,  
> Bill

Ok here’s my controller code:

class ClientController \< ApplicationController  
scaffold :client

def change

end  
end

my view code:

Release Name:

Release Type:

 Game Music Movie
 Genre: 

and I do the same thing for music and movie with different inputs

and here is my RJS ‘change.rjs’ template code:

I was doing something like

if page[:mediaType][:value] = ‘Music’  
page.remove ‘mediaType’  
page.show ‘music\_form’  
end

I did this for game and movie too

I would buy the book but I don’t have a credit card 🙂

Thanks

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 25, 2007, 9:52pm UTC](https://www.ruby-forum.com/t/ajax-and-rails/105101/6 "2007-05-25T21:52:41Z")

</div>

Hi Matthew,

No offense intended, but after seeing your code I think you ought to  
work  
through a basic tutorial or two to get your arms around Rails’ basics  
before  
you tackle RJS. The cookbook tutorial at O’Reilly that Curt and I did  
is  
pretty much up to date. It’s at

> **[Radar - O’Reilly](https://www.oreilly.com/radar/?page=1)**
>
> Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 26, 2007, 1:43am UTC](https://www.ruby-forum.com/t/ajax-and-rails/105101/7 "2007-05-26T01:43:13Z")

</div>

Bill W. wrote:

> Hi Matthew,
> 
> No offense intended, but after seeing your code I think you ought to  
> work  
> through a basic tutorial or two to get your arms around Rails’ basics  
> before  
> you tackle RJS. The cookbook tutorial at O’Reilly that Curt and I did  
> is  
> pretty much up to date. It’s at  
> [Radar – O’Reilly](http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html?page=1)
> 
> Best regards,  
> Bill

I’ve already done that, I already made some exercises with that and they  
worked fine, its just I’m having trouble in using ajax, I know how to  
use my models and controllers and to make my custom views its just I  
want to learn more with ajax. I have already made some examples that I  
insert data into 2 different tables and databases on the same form thats  
not my problem.
