Forum: Ruby on Rails Ajax and Rails

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.
Matthew L. (Guest)
on 2007-05-25 21:39
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
Bill W. (Guest)
on 2007-05-25 21:55
(Received via mailing list)
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
Matthew L. (Guest)
on 2007-05-25 21:58
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
Bill W. (Guest)
on 2007-05-25 22:11
(Received via mailing list)
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
Matthew L. (Guest)
on 2007-05-25 22:53
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: <input type="text" id="release_name" name="release[name]"
value="<%= @release.name %>" />

Release Type:
<select id="mediaType" onChange="<%= link_to_remote 'Change Release',
:url => {:action => 'change'} %>">
 <option value="Game">Game</option>
 <option value="Music">Music</option>
 <option value="Movie">Movie</option>
</select>

<div id="game_form">
 Genre: <input type="text" id="game_genre" name="game[genre]" value="<%
@game.genre %>" />
</div>


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

<input type="submit" id="Submit" value="Submit" />



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
Bill W. (Guest)
on 2007-05-25 23:52
(Received via mailing list)
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
http://www.onlamp.com/pub/a/onlamp/2006/12/14/revi...

Best regards,
Bill
Matthew L. (Guest)
on 2007-05-26 03:43
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
> 
http://www.onlamp.com/pub/a/onlamp/2006/12/14/revi...
>
> 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.
This topic is locked and can not be replied to.