SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2008
    Location
    South Florida
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using jQuery ajax in RoR

    Hi All, I'm fairly new to Rails, having jumped into it at my new web dev job. I've been assigned the task of converting an existing website from the regular RoR/Prototype setup over to jQuery in order to develop unobtrusive ajax enhancements.

    My first todo is to ajaxify the contact form. I've followed the jquery Railscast:

    1. copy jquery.js to /public/javascripts and call javascript_include_tag 'jquery' in my application layout.

    2. used the jquery.ajaxSetup method to setup the request headers as text/javascript so that rails will execute format.js in the respond_to block.

    In my controller I do a respond_to block and this is where i'm getting stuck. I can't find the syntax to be able to render a javascript partial from any views folder i wan't.

    In the railscast, he simply calls format.js without arguments inside the respond_to block, which in turn renders a [controller method name].js.erb file within the views/[controller] folder.

    My question is how do i call any partial from any views folder i want?

    Im in the send_mail method of my contact_controller, and my desired javascript file is in views/widgets/contact.js.erb.

    In the respond_to block i'm trying this:

    Code Ruby:
    format.js { render :partial => 'widgets/contact'
    I have a simple alert('wtf') call in that partial but it never executes.

    The form submit action is correctly intercepted by my widget.js file that's included on the page already and it works fine.

    The page also works as intended with javascript disabled.

    So to sum up, how do I render any javascript partial after the ajax call from the respond_to block?

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2008
    Location
    South Florida
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok i figured it out.
    Rails adds the underscore in front of the filename when calling artial
    it also only adds a .erb extension if you don't specify one

    so the proper way to call a custom partial is:
    Code Ruby:
    render :partial => 'widgets/_javascript.js.erb

    widgets being your own directory within app/views, and _javascript can be any name you want.

  3. #3
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    972
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you shouldn't need to include the underscore in your render statement. The physical filename starts with an underscore, but to call you would just use 'widgets/javascript.js.erb'
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2008
    Location
    South Florida
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's strange because it didn't work that way, i tried without the underscore to no avail. Through trial and error I realized that by adding the file extension .js.erb, Rails doesn't add the underscore for you anymore. I'm using rails 2.2.2 if it makes a difference.

    I noticed this in the server log, when i called render artial => 'widgets/javascript' Rails would look for 'widgets/_javascript.erb'.
    Then when i changed the partial name to 'widgets/javascript.js.erb' Rails would look for exactly that file name without the underscore automatically added in.

  5. #5
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe the syntax would work without the underscore if you also leave out the file extension. So:
    Code:
    render :partial => 'widgets/javascript'


Tags for this Thread

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
  •