Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ajax render partial with rails view

I have my jquery ajax success as

success: function(data) {
    $('#someId').html(data);
}

I have a partial file in the name of _information.html.erb

How do i render my ajax success response to rails partial view(information).

Most of the resources showing something like this

$('#holderDiv').empty().append('<ul> <%= j render @comments %> </li>')

But i didn't feel comfortable with it. Any other way to solve it.

like image 790
Prabhakaran Avatar asked Feb 16 '23 02:02

Prabhakaran


1 Answers

UPDATE Here's some more info in response to your comments.

First please read this Rails Guide on Javascript for more info.

update.js.erb is your view. Instead of having an update.html.erb file for your view, the respond_to block with format.js in your controller will send update.js.erb (formatted as javascript code) back to your jquery function.

update.js.erb could contain pure javascript. However it is processed by the server before being converted to javascript, so you can embed any ruby code you want. That ruby code gets converted into javascript.

If you use chrome developer tools, you can look in the "network" tab after your jquery call runs. You'll see a new entry appear for the AJAX call you just made. If you click on the entry, you'll see the javascript that was returned.

I've updated the update.js.erb file below slightly to show how you can put regular javascript code in the .js.erb file. The first line is javascript. The second line is ruby code which the server converts into javascript. So by the time that it gets to your browser, the entire update.js.erb file has been converted into javascript.

Hope that helps...

Original Answer Below:

Option 1:

Assuming that your jQuery success function is tied to the successful completion of a controller action (I'll use the edit action for my example), you would create a view called update.js.erb which will be called after a successful edit.

Controller:

if @user.update_attributes(params[:user])
    respond_to do |format|
        format.html { redirect_to @user, notice: "Successfully updated user." }
        format.js
    end
else
    # ...
end

Because this is being called from javascript and you have format.js in the respond_to block, update.js.erb will automatically be called.

update.js.erb:

console.log('see... this is a regular javascript call.');
<%= render partial: 'information', format: 'js' %>

Option 2

The snippet you included:

$('#holderDiv').empty().append('<ul> <%= j render @comments %> </li>')

will only work in a js.erb file, where embedded ruby code is first processed then converted into javascript code. That would work in a situation such as:

Controller:

def create
    user = User.new(params[:user])

    respond_to do |format|
        if @user.save
            @comments = 'some comments to display!'
            format.js
        else
            # ...
        end
    end
end

create.js.erb:

$('#holderDiv').empty().append('<%= j render @comments %>')
like image 51
manishie Avatar answered Mar 03 '23 00:03

manishie