SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    16
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Smile Render partial with Ajax!

    So I had some issues with an App I'm doing, I was calling an entire template...

    The problem was that when you call a template, you load all the libraries with it... its just crap.

    So I create a partial to just call the partial for every search, without the libraries or anything that I don't need.

    To do that you must specify the partial in the respond_to like this

    Code:
    respond_to do |format|
    
          format.html { render :partial => 'partials/results' } # index.html.erb
    
          format.json { render json: @results, :callback => params[:callback] }
    
    end
    And with jquery you just call the data

    Code:
    $.get "/search/result", data, (data) ->
    
          $("#resultados").html data
    Maybe some of you already know that, but I'm just letting this here so maybe it can be helpful to someone else.

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,332
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    Thanks for sharing your discovery, @robzdc ;
    I hope to see more posts with valuable information we can share with fellow Rubyists.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OR what would be even better than that, keep your results partial, but have a results.html.erb file AND a results.js.erb file. Render the partial per usual in the results.html.erb file. In the js.erb file I usually put my content in a variable
    Code:
    var content = $('<%= escape_javascript(render "PARTIAL_NAME") %>');
    and then you can append to page or do whatever you want with the data. That way you still have a standard response to a browser with js disabled.
    Don't forget to add format.js to your respond_to block. And the get action, just append .js to the url.
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  4. #4
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    16
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I tried that, but it was appended like text.
    Im using CoffeeScript


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
  •