jquery ajax 使用 Rails 视图渲染部分 [英] jquery ajax render partial with rails view

查看:43
本文介绍了jquery ajax 使用 Rails 视图渲染部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 jquery ajax 成功

I have my jquery ajax success as

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

我有一个名为 _information.html.erb 的部分文件

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

如何将我的 ajax 成功响应呈现给 Rails 部分视图(information).

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.

推荐答案

UPDATE以下是针对您的评论提供的更多信息.

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

首先请阅读关于 JavaScript 的Rails 指南以了解更多信息.

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

update.js.erb 是你的观点.控制器中带有 format.js 的 respond_to 块将发送 update.js.erb(格式化为 javascript 代码)回您的 jquery 函数,而不是为您的视图提供 update.html.erb 文件.

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 可以包含纯 javascript.但是,它在转换为 javascript 之前由服务器处理,因此您可以嵌入任何您想要的 ruby​​ 代码.该 ruby​​ 代码被转换为 javascript.

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.

如果您使用 Chrome 开发人员工具,则可以在 jquery 调用运行后查看网络"选项卡.您将看到为您刚刚进行的 AJAX 调用显示一个新条目.如果您点击该条目,您将看到返回的 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.

我已经稍微更新了下面的 update.js.erb 文件,以展示如何将常规 JavaScript 代码放入 .js.erb 文件中.第一行是javascript.第二行是服务器将其转换为 javascript 的 ruby​​ 代码.因此,当它到达您的浏览器时,整个 update.js.erb 文件已转换为 javascript.

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.

希望有帮助...

原始答案如下:

选项 1:

假设您的 jQuery 成功函数与控制器操作的成功完成相关联(我将在我的示例中使用编辑操作),您将创建一个名为 update.js.erb 的视图,该视图将在成功完成后调用编辑.

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.

控制器:

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

因为这是从 javascript 调用的,并且您在 respond_to 块中有 format.js,所以将自动调用 update.js.erb.

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:

update.js.erb:

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

<小时>

选项 2

您包含的代码段:

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

仅适用于 js.erb 文件,其中首先处理嵌入的 ruby​​ 代码,然后将其转换为 javascript 代码.这将适用于以下情况:

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:

控制器:

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:

create.js.erb:

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

这篇关于jquery ajax 使用 Rails 视图渲染部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆