0

I am trying to render a partial from a javascript in my assets directory. I found that I can't call the render method within the assets directory. But I cant move my ajax command out of the assets directory either. So my Question is now, how to pass my ajax result onto the action.js file in my view.

So I call the ajax on a onclick jQuery event:

/app/javascript/mymodel.js.coffee

$(document).ready ->
---some actions ----
$('.myid').click ->
    ---some action---
    $.ajax 'mypath'
        dataType: 'json'
        success: (result)->
            ---render result in partial on myid-div---

So how do I pass my beloved result and myid onto:

/app/views/mymodel/show.js.erb

$('.myid').html("<%= escape_javascript( render( :partial => 'myartial') ) %>");  

To render it out?

I'm banging my head around this for quite some time, so I would really appreciate some help!

Jan
  • 47
  • 7

2 Answers2

0
$(document).ready ->
---some actions ----
$('.myid').click ->
    ---some action---
    $.ajax 'mypath'
        dataType: 'json'
        params: (pass in what you need, i don't know the exact syntax, just google it)
        success: (result)->
            ---render result in partial on myid-div---
controller

def show
  @result = params[:result]
end

view
show.js.erb
$('#some_id').html('<%= render("mypartial").html_safe %>');

@result is accessible now

Pass your result as a parameter from your AJAX call to your controller method. Then instantiate an instance variable (@result for example from the parameter), that way your views is not able to use the instance variable.

YaBoyQuy
  • 783
  • 5
  • 8
  • Cool I like that answer. Sadly I'm not able to create my code out of it. My ajax call does GET me my information. But how do I call the render in the success function? Can u walk me through it? Whats the function call to say render _mypartial as format = js with this argument? – Jan Sep 13 '12 at 17:40
  • so after the show method runs it will execute a JS file (it will look for show.js.erb). (check updated answer) – YaBoyQuy Sep 18 '12 at 19:04
0

You can call the mymodel controller's method to render your partial, but the file name of partial should start with an underscore _

In your example, these code render :partial => 'show' may work for your to render js if you put them into mymodel controller and you should rename show.js.erb to _show.js.erb in the view of mymodel.

To be optimistic, you should be able to render the _show.js.erb and you can see somethings like render _show.js.erb successful inside your terminal.

But It doesn't means that the rails was processing the js.erb as javascript, because there are syntax error in your js.erb ( I am absolutely sure!! ). So you may also change

$('.myid').html("<%= escape_javascript( render( :partial => 'myartial') ) %>");

to

$('.myid').html('<%= escape_javascript( render( :partial => 'myartial') ) %>');

Check out my questions for more details. I faced the similar problem before. It will also show you an example of how to send a ajax call and render .js.erb.

:remote => true will send a ajax request to line_items_path.

And according to the routing, it will call the create method, format.js will render create.js.erb by convention. Alternatively, I can render the js.erb with format.js {render :partial=> "create"} if _create.js.erb exist

Community
  • 1
  • 1
code4j
  • 4,208
  • 5
  • 34
  • 51
  • Thanks for your answer. I thinks thats not part of my Problem. The 'show' was not my partial it was my js template for the action. my show has an _ in front of it. And its not called so far, because I dont know how. Why do u think I should replace the " with the ' in my show.js? – Jan Sep 13 '12 at 18:16
  • rendering partial is common for ajax call. [check this out](http://apidock.com/rails/ActionController/Base/render). The reason I think you should replace "" with ' because I predict the partials of myartial will come out many ", and you cannot enclose "" inside "". You can use Firebug to see the ajax response – code4j Sep 13 '12 at 18:25
  • I have refined my answer:) check it out to help you understand how to render js – code4j Sep 13 '12 at 18:42