0

I've spent a day spinning my wheels trying to understand how Rails :remote=>true works. The question below seems complicated but I'm trying to understand this simple question with the information I provide:

How can I make an Ajax call that simply renders as JS without using :remote=>true? From my understanding :remote=>true simply generates and handles an AJAX call:

My view looks like this:

  • opts in a very complicated way, creates a link with :remote => true. This is omitted for simplicity
.e_list
  = opts.sort_link(:name)
  = opts.sort_link(:description)
  - e_list.each do |e|
    .entry
      = link_to(e.name, '#', class: 'select_e', data: {e_id: e.id})
      = e.description
  = paginate e_list, remote: true, params: {search:"j", com_id: com.id}

Gallery.js.erb

$('#com<%= @com.id %>').replaceWith('<%= escape_javascript render(partial: "shared/com", locals: {com: @com}) %>'); 

My Controller:

  def gallery

    if params[:com_id] 
      @com = @s.com.find(params[:com_id])
      @com.filter = params
    end

    if c = @s.com.where(:_type => "Com").first
      @current_e = c.entries(@user.app_id).first
      @current_e.og_url = view_context.og_url(@current_e)
    end

    render :text => "foobar" if !@current_e

  end

logs, after the user clicks on the pagination links or sort links (the key is those links have :remote => true)

Started GET "super long url" for 127.0.0.1 at 2012-05-04 16:08:42 -0700
Processing by CController#gallery as JS

SO I TRY TO RECREATE THIS WITH AJAX:

  $('button.search').live 'click', (e) ->
    search = $(e.target).attr('search-term')
    success_callback = (results) ->
      if results
        console.log(results)
        update_components(results[0].entry, '.entry')
      else
    $.ajax(
      url: 'super long url that is exactly the same url as above!'
    ).done ->
    return false

MY FAILED RESPONSE THAT DOES NOT RENDER AS JS, YET I THOUGHT :remote => true was simply an ajax call wtf?:

Started GET "super long url identical as the one that renders JS" for 127.0.0.1 at 2012-05-04 16:07:22 -0700
Processing by ContestController#gallery as */*

What is going on? How can I make an Ajax call that simply renders as JS without using :remote=>true?

4
  • nope. Started GET "/screens/4fa02763dc1c82269c0001da/contest/gallery?app_row_id=5&component_id=4f9f0305dc1c8255a300006c&page_id=136755716356584&screen_only=1&screen_permanent_id=4f9edb0cdc1c8228800000ef&search=j&source_component_id=4fa0228edc1c82269c00012c" for 127.0.0.1 at 2012-05-04 16:07:22 -0700 Processing by ContestController#gallery as / Each url is very long, but identical. Commented May 5, 2012 at 0:04
  • try /screens/4fa02763dc1c82269c0001da/contest/gallery.js?app_row_id=5.... If you want the js from the response to execute in the browser you should need to do something like eval(response), but i'm just suggesting, I never done it and even know how to eval code of a string in javascript. Commented May 5, 2012 at 0:14
  • ... if you submit this as an answer I will accept. Commented May 9, 2012 at 22:46
  • done, I'm glad I could help. it was the .js right? Commented May 10, 2012 at 2:15

3 Answers 3

1

Try

$.ajax({
    url: 'your url',
    dataType: 'script'
})

http://www.alfajango.com/blog/rails-3-remote-links-and-forms-data-type-with-jquery/

Sign up to request clarification or add additional context in comments.

Comments

0

try

/screens/4fa02763dc1c82269c0001da/contest/gallery.js?app_row_id=5....

If you want the js from the response to execute in the browser you should need to do something like eval(response), but i'm just suggesting, I never done it and even know how to eval code of a string in javascript.

Comments

0

You could use jQuery to accomplish what you are trying to do:

/* your jquery file */
jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

...
$('#button').click(function() {
  $.post('/controller/action', {
    query_string1: value1
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.