SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Aug 2006
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    passing ruby variable values to javascript

    Hello,

    I have been trying to pass values obtained from ruby methods to a javascript file that I downloaded off the web with little success.

    Part of the javascript file is like that
    Code:
    photos({id:'my_photos',
              border:'',
              size_mode:'image',
              width:150,
              height:100,
              sides:4,
              steps:10,
              speed:8,
              direction:'top',
              images:['/images/pic18.jpg',
                      '/images/pic2.jpg',
                      '/images/pic3.jpg',
    		  '/images/pic1.jpg'],
    I want to change the section with the image links, so it can be changed with every page. I've created the ruby files to output the different image file names for the different pages, but if I do this:
    Code:
    '/images/<%= p[0] %>
    etc, where the variable p[0] contains the name of the file, I get an error for unrecognized variable, although I try to declare the variable at the top of the page, and I am able to get it written on the page, so I know the value exist. Is there anyway I can get ruby variables to be passed to the javascript? I've checked various documentation,i am still at a lost.

    Thanks,

    CathyM

  2. #2
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess you have to use a rjs file instead a js (with a javascript_controller where you define an action with the same name of the rjs file. And into the rjs file something like

    Code:
    var images= new Array();
    <% for image in @images -%>
    	images.push(new Array(<%= image.path %>));
    <% end -%>
    where @images, defined within the action with the same name of your file, is an array that contains your images.


    I suggest you to take a look of RailsCast #88, it shows this technique.


    Hope this helps
    Alla prossima ...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •