如何在rails 3中渲染partial.js

时间:2022-10-08 14:00:28

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'来查看任务/ show.js.erb任何想法?

In projects/show.html.erb

在projects / 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

谢谢

6 个解决方案

#1


26  

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

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

From "Using Partials".

从“使用部分”。

#2


10  

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

你可以试试这个。而不是有一个Javacript部分你可以使用html

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

and,

和,

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

#3


5  

Try:

尝试:

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

render:partial =>“tasks / show.js”

#4


4  

To make it work I needed to add the format:

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

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

#5


3  

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.erb and 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::MissingTemplate error. The only way I found to solved was to rename the view to _select.rhtml and then every thing works fine.

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

#6


2  

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部分。

#1


26  

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

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

From "Using Partials".

从“使用部分”。

#2


10  

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

你可以试试这个。而不是有一个Javacript部分你可以使用html

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

and,

和,

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

#3


5  

Try:

尝试:

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

render:partial =>“tasks / show.js”

#4


4  

To make it work I needed to add the format:

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

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

#5


3  

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.erb and 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::MissingTemplate error. The only way I found to solved was to rename the view to _select.rhtml and then every thing works fine.

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

#6


2  

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部分。