ActiveRecord/ActionController in javascript code

Hi,

I have a javascript file for generating a menu when the user hovers over
a link. These links are basically a list of logged in users. Now, in the
menu I would like to have a link to “Display Profile” so that a user can
view the profiles of other users. Also nice would be a picture in the
menu.

The problem is obviously that the javascript expects static links. But I
want to have things like @user.name (ActiveRecord) or even a call to an
ActionController action which could display the profile.

If these are impossible then hopefully there must be some other way to
achieve this. Any help would be greatly appreciated.

Thanks!

Hi Aditya

If these are impossible then hopefully there must be some other way to
achieve this. Any help would be greatly appreciated.

How about, as a suggestion use a partial that generates the javascript
array (im guessing the “static links” are in an array) then that partial
could use a helper to do the lookup to activerecord and display the text
you want.

Does that help?

Jonathan

How about, as a suggestion use a partial that generates the javascript
array (im guessing the “static links” are in an array) then that partial
could use a helper to do the lookup to activerecord and display the text
you want.

Thanks for the reply, Jonathan. Well, the problem with that is still the
same: how do I place the generated array within the javascript code
itself? Or how to have the javascript code access the array generated by
the partial?

Having direct access to ActiveRecord in JS seems like a big violation of
MVC… you need to chk out RJS - it’s not going to give you this
functionality out of the box but should prove insightful enough to get
you a pretty clean solution.