Ruby-on-rails 从 Rails 3 中的另一个部分渲染另一个文件夹中的部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7124766/
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-09-03 01:48:42  来源:igfitidea点击:

Rendering partial from another folder from another partial in Rails 3

ruby-on-railspartial

提问by aetaur

For example: I'm have two models: Taskand List. Taskbelongs_toList. I'm render lists/_form.html.erbpartial within lists/show.html.erb view. Now I need to render tasks/_fields.html.erbpartial within lists/_form.html.erbpartial:

例如:我有两个模型:TaskListTaskbelongs_toList. 我lists/_form.html.erblists/show.html.erb view. 现在我需要tasks/_fields.html.erblists/_form.html.erb部分渲染部分:

<%= render 'tasks/fields' %>

But I get an error ActionView::MissingTemplate

但我收到一个错误 ActionView::MissingTemplate

If I try to render tasks/_fields.html.erbwithin lists/_form.html.erb, everything works.

如果我尝试在tasks/_fields.html.erb内渲染lists/_form.html.erb,则一切正常。

I see two bad ways to solve this problem:

我看到解决这个问题的两种不好的方法:

Is there a good way?

有什么好办法吗?

回答by Arun Kumar Arjunan

Try this:

尝试这个:

<%= render :partial => 'tasks/fields' %>

回答by pduersteler

If you are sharing things like this, why not put them into a folder like app/views/shared/or directly into app/views/layouts?

如果你正在分享这样的东西,为什么不把它们放到一个文件夹中,app/views/shared/或者直接放入app/views/layouts

回答by Ahmed Elkoussy

For Rails 5 & above:

对于 Rails 5 及以上版本

You better use the render without partial like this:

你最好像这样使用没有部分的渲染:

<% render 'tasks/fields' %>

Because the partialcan cause this kind of issues& is not needed any more

因为partial可能会导致此类问题并且不再需要

回答by Matias Jurfest

Based on @ArunKumarArjun but with the new Rails notation:

基于@ArunKumarArjun 但使用新的 Rails 符号:

<%= render partial: 'tasks/fields' %>