使用 jquery 和 Rails 加载部分内容

发布于 2024-08-20 23:31:48 字数 282 浏览 4 评论 0原文

我很好奇用 jquery 加载 Rails 部分的最佳方法是什么。到目前为止,我已经尝试了一个不起作用的解决方案:

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

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

谢谢。

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.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鲜血染红嫁衣 2024-08-27 23:31:48

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

我不确定 Rails,但我想您需要创建一个单独的控制器(或其他)来执行此操作。

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

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.

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

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.

迷爱 2024-08-27 23:31:48

代码不起作用的原因是 Rails 不会向外部公开视图文件。为了实现你想要的东西,你需要一个动作来渲染这个部分:

def image_gallery
  render :partial => "image_gallery"
end

还有一个名为 jRails 的插件,它用等效的面向 jquery 的辅助程序替换标准 ajax 辅助程序(使用原型)。在你的情况下,你会使用类似的东西

<%= remote_function(:update => "imagecontent", :url => "/newsletters/image_gallery") %>

,甚至更多,在 Rails 3 中你将不需要它。请参阅此链接

The reason that code doesn't work is that rails doesn't expose view files outside. To achieve the thing you want, you'll need an action which would render this partial:

def image_gallery
  render :partial => "image_gallery"
end

Also there is a plugin called jRails which replaces standard ajax helpers (which use prototype) with equivalent jquery-oriented ones. In your case you would use something like

<%= remote_function(:update => "imagecontent", :url => "/newsletters/image_gallery") %>

And even more, in Rails 3 you won't need it. See this link.

绮筵 2024-08-27 23:31:48

另一种选择:

$('#imagecontent').html('<%= escape_javascript render 'newsletters/image_gallery' %>');

Another alternative:

$('#imagecontent').html('<%= escape_javascript render 'newsletters/image_gallery' %>');
梦中楼上月下 2024-08-27 23:31:48

使用 Rails 3 你可以做...

控制器

@content = '/some/partial'

视图 - haml.erb(html.erb) 或 js.erb

$('#myDiv').html("<%= escape_javascript render @content %>");

如果 Rails 2 (只有原型可用,除非添加 noConflict) 你可以尝试这个...

在控制器中

@content = render :partial=>"/users/cool_partial"

调用你的 js

myFunction(#{@content.to_json})

你的函数...

myFunction = function(content){

    $("myDiv").html(content);

}

我确实同意中微子的观点,即这可以通过动作更好地处理。

def get_partial
  render 'some/partial'
end

希望这有帮助

With rails 3 you could do...

controller

@content = '/some/partial'

view - either haml.erb(html.erb) or js.erb

$('#myDiv').html("<%= escape_javascript render @content %>");

You could try this if rails 2 (only prototype available unless noConflict added)...

In the controller

@content = render :partial=>"/users/cool_partial"

Call your js

myFunction(#{@content.to_json})

Your function...

myFunction = function(content){

    $("myDiv").html(content);

}

I do agree with neutrino that this could be better handled via an action.

def get_partial
  render 'some/partial'
end

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文