用jQuery和Rails加载部分 [英] load partial with jquery and rails

查看:70
本文介绍了用jQuery和Rails加载部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇用jquery加载rails部分的最佳方法.到目前为止,我已经尝试了一种不起作用的解决方案:

Im curious about what the best way to load a rails partial with jquery. So far I have tried a solution not working:

$('#imagecontent').load('/newsletters/_image_gallery.html.erb', function() {
alert('Load was performed.');
});

是为此构建单独的动作和/或控制器并设置路线的最佳解决方案吗?我问是因为使第一个解决方案有效似乎更容易,也许不是那么轻松.

Is the best solution to build a seperate action and/or controller for this and set up a route? Im asking because making the first solution work seems more easy, perhaps not so restful.

谢谢.

推荐答案

您不应该能够从HTTP请求访问模板文件(.erb),这是非常糟糕的做法.建议您将它们放在Web服务器路径之外,并仅通过应用程序访问它们.

You should not be able to access Template files (.erb) from HTTP requests, this is very bad practice. I suggest you put them outside your web servers path and access them only through your application.

我不确定Rails,但是我想您需要创建一个单独的控制器(或其他任何控制器)来实现此目的.

I'm not sure about Rails, but I suppose you need to create a separate controller (or whatever) to do this.

编辑,这是因为.erb文件是嵌入式Ruby文件,如果您尝试请求它们,则会得到所有不需要的数据,例如<%= page_title %>,而您不想显示它们.用户.因此,您需要通过Rails应用程序传递这些.erb文件,以便将它们正确解释为HTML.

EDIT this is because .erb files are Embedded Ruby files, and if you try to request them you will get all unwanted data, like <%= page_title %>, that you don't want to show the users. Therefore you need to pass these .erb files through your Rails application so that they are properly interpreted into HTML.

这篇关于用jQuery和Rails加载部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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