Javascript 如何在 rails 3 中渲染 partial.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2755337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:47:26  来源:igfitidea点击:

How to render partial.js in rails 3

javascriptruby-on-railspartialruby-on-rails-3

提问by Julian Mann

Using rails3 - I have a project with many tasks. I want to use javascript to build the UI for each task. I figured I could display those tasks on the projects show page by rendering a javascript partial for each. I can't get 'tasks/show' to see tasks/show.js.erb Any ideas?

使用 rails3 - 我有一个有很多任务的项目。我想使用 javascript 为每个任务构建 UI。我想我可以通过为每个任务渲染一个 javascript 部分来在项目显示页面上显示这些任务。我无法通过 'tasks/show' 来查看 tasks/show.js.erb 有什么想法吗?

In projects/show.html.erb

在项目/show.html.erb

<div id="tasks">
<%= render(:partial => "tasks/show", :collection => @project.tasks) %> 
</div>

tasks/show.js.erb

任务/show.js.erb

$("tasks").append(new TaskWidget(task.id))

I get the errors

我得到了错误

ActionView::MissingTemplate in Projects#show 

Missing partial tasks/show with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths .... around line #13

Thanks

谢谢

回答by wombleton

Shouldn't it be in the file _show.js.erb?

不应该在文件中_show.js.erb吗?

From "Using Partials".

来自“使用部分”

回答by cortex

You can try this. Instead of have a Javacript partial you can make an html one _show_js.html.erband surround your JS code with the html <script>tag.

你可以试试这个。您可以制作一个 html_show_js.html.erb并用 html<script>标记包围您的 JS 代码,而不是使用 Javacript 部分。

<script type='text/javascript'>
$("tasks").append(new TaskWidget(task.id))
</script>

and,

和,

<div id="tasks">
<%= render(:partial => "tasks/show_js", :collection => @project.tasks) %> 
</div>

回答by htafoya

To make it work I needed to add the format:

为了使它工作,我需要添加格式:

 <script> 
  <%= render(:partial => 'shared/topmenujs', :formats => [:js] ) %>
  </script>

回答by xavi

Try:

尝试:

render :partial => "tasks/show.js"

渲染:部分=>“任务/show.js”

回答by ramontiveros

Well I don't know if is the same issue, but I went through a similar thing with rails 3. I had for example a partial view in app/views/invoices/_select.html.erband I was able to render in a view with

好吧,我不知道是否是同样的问题,但我在 Rails 3 中经历了类似的事情。例如,我有一个局部视图app/views/invoices/_select.html.erb,我能够在视图中渲染

<%= render :partial => "invoices/select" %>

<%= render :partial => "发票/选择" %>

with out any problem and if in the controller I place

没有任何问题,如果在控制器中我放置

render :partial => "invoices/select"

渲染:部分=>“发票/选择”

I was able to opened the action in the browser, the problem arise when I try to call the action from an ajax call, then I get the ActionView::MissingTemplateerror. The only way I found to solved was to rename the view to _select.rhtmland then every thing works fine.

我能够在浏览器中打开该操作,当我尝试从 ajax 调用中调用该操作时出现问题,然后ActionView::MissingTemplate出现错误。我发现解决的唯一方法是将视图重命名为_select.rhtml,然后一切正常。

回答by Michael Waxman

The problem is that in Rails 3 the render from the .js file will only look for files in the form of show.js.erb or show.erb

问题在于,在 Rails 3 中,.js 文件的渲染只会查找 show.js.erb 或 show.erb 形式的文件

To get around this, you would either need to create a _show.js.erb file, but since that sort of defeats the purpose of partials, you could alternatively just rename the show.html.erb file to show.erb

为了解决这个问题,您要么需要创建一个 _show.js.erb 文件,但由于这种方式违背了部分的目的,您也可以将 show.html.erb 文件重命名为 show.erb

This is probably bad practice, though, to make a standard controller action without a type ending (e.g. show.erb)

但是,这可能是一种不好的做法,即制作没有类型结尾的标准控制器操作(例如 show.erb)

What I would do if I were you is rename your partial something like _tasks.erb instead of _show.html.erb, and then if you needed a tasks/show.html.erb you could just render the _tasks partial inside of that view.

如果我是你,我会做的是将你的部分重命名为 _tasks.erb 而不是 _show.html.erb,然后如果你需要一个 tasks/show.html.erb,你可以在该视图中渲染 _tasks 部分。