使用jquery和rails加载部分

时间:2022-10-16 00:31:22

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

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

$('#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.

5 个解决方案

#1


8  

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.

您应该无法从HTTP请求访问模板文件(.erb),这是非常糟糕的做法。我建议你把它们放在你的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. Therefore you need to pass these .erb files through your Rails application so that they are properly interpreted into HTML.

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

#2


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:

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

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

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

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

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

更重要的是,在Rails 3中你不需要它。看到这个链接。

#3


3  

Here's what you want:

这是你想要的:

http://www.slideshare.net/JamesEdwardGrayII/ajax-with-jquery-in-rails

#4


3  

Another alternative:

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

#5


3  

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

希望这可以帮助

#1


8  

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.

您应该无法从HTTP请求访问模板文件(.erb),这是非常糟糕的做法。我建议你把它们放在你的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. Therefore you need to pass these .erb files through your Rails application so that they are properly interpreted into HTML.

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

#2


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:

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

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

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

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

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

更重要的是,在Rails 3中你不需要它。看到这个链接。

#3


3  

Here's what you want:

这是你想要的:

http://www.slideshare.net/JamesEdwardGrayII/ajax-with-jquery-in-rails

#4


3  

Another alternative:

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

#5


3  

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

希望这可以帮助