在一个视图中生成两个独立的 .rb 文件

发布于 2024-11-30 02:09:25 字数 298 浏览 1 评论 0原文

我是铁路新手。我想在数据库结果旁边生成“_posts”表单。我该怎么做?

我目前默认使用此文件两次生成 index.html.erb,但我希望第二个是 _form.html.erb 文件,以便我可以在观看数据库时写一篇文章:

<tr>
<td><%= yield %></td>
<td><%= yield %></td>
</tr>

最终我希望刷新表单和使用 ajax 的帖子列表。但那是以后的事了。

I'm new to rails. I would like to yield my "_posts" form next to my database results. How can I do this?

I am currently yielding the index.html.erb by default twice using this but I want the second to be the _form.html.erb file so that I can write a post while watching the database:

<tr>
<td><%= yield %></td>
<td><%= yield %></td>
</tr>

Eventually I hope to refresh both the form and the list of posts with ajax. but that's for later.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零崎曲识 2024-12-07 02:09:25
<tr>
<td><%= yield %></td>
<td><%= render :partial => "form" %></td>
</tr>
<tr>
<td><%= yield %></td>
<td><%= render :partial => "form" %></td>
</tr>
浅暮の光 2024-12-07 02:09:25

看起来您的 _posts 表单已经在部分表单中,因此您可以将该部分表单包含在索引页的模板中:

在 index.html.erb 中包括:

<%= render( :partial => "<model-name>/<partial_name>" %>

其中 是为您的帖子表单引用建模, 是部分的名称(即您拥有表单的文件名。

例如,

<%= render( :partial => "posts/new" %>

部分只是通过在开头添加下划线来表示)文件名。即如果之前是 new.html.erb ,则将其设为 _new.html.erb

Rails 文档中有很多关于部分和渲染的好信息:
http://guides.rubyonrails.org/layouts_and_rendering.html

It looks like your _posts form is already in a partial and so you can include that partial in the template for your index page:

In index.html.erb include:

<%= render( :partial => "<model-name>/<partial_name>" %>

Where <model_name> is that of the model your posts form references and <partial_name> is the name of the partial (i.e. the filename of where you have the form.

e.g.

<%= render( :partial => "posts/new" %>

A partial is simply denoted by adding an underscore to the start of the filename. i.e. if before it was new.html.erb then make it _new.html.erb.

There is lots of good information on partials and rendering in the Rails docs:
http://guides.rubyonrails.org/layouts_and_rendering.html

冷情妓 2024-12-07 02:09:25

你需要的不是通常在布局中使用的yield,而是渲染模板。

What you need is not yield - which is usually used in layout - but render template.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文