简体   繁体   English

使用jquery和rails加载部分

[英]load partial with jquery and rails

Im curious about what the best way to load a rails partial with jquery. 我很好奇用jquery加载rails部分的最佳方法。 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. 谢谢。

You should not be able to access Template files (.erb) from HTTP requests, this is very bad practice. 您应该无法从HTTP请求访问模板文件(.erb),这是非常糟糕的做法。 I suggest you put them outside your web servers path and access them only through your application. 我建议你把它们放在你的web服务器路径之外,只能通过你的应用程序访问它们。

I'm not sure about Rails, but I suppose you need to create a separate controller (or whatever) to do this. 我不确定Rails,但我想你需要创建一个单独的控制器(或其他)来做到这一点。

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. 编辑这是因为.erb文件是嵌入式Ruby文件,如果您尝试请求它们,您将获得不想向用户显示的所有不需要的数据,如<%= page_title %> Therefore you need to pass these .erb files through your Rails application so that they are properly interpreted into HTML. 因此,您需要通过Rails应用程序传递这些.erb文件,以便将它们正确解释为HTML。

With rails 3 you could do... 使用rails 3你可以做...

controller 调节器

@content = '/some/partial'

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

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

You could try this if rails 2 (only prototype available unless noConflict added)... 你可以尝试这个如果rails 2(只有原型可用,除非noConflict添加)...

In the controller 在控制器中

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

Call your js 打电话给你的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 希望这可以帮助

另一种选择:

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

The reason that code doesn't work is that rails doesn't expose view files outside. 代码不起作用的原因是rails不会在外部公开视图文件。 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. 还有一个名为jRails的插件,它将标准的ajax助手(使用原型)替换为等效的面向jquery的助手。 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. 更重要的是,在Rails 3中你不需要它。 See this link . 看到这个链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM