在一个视图中生成两个独立的 .rb 文件
我是铁路新手。我想在数据库结果旁边生成“_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您的 _posts 表单已经在部分表单中,因此您可以将该部分表单包含在索引页的模板中:
在 index.html.erb 中包括:
其中
是为您的帖子表单引用建模,
是部分的名称(即您拥有表单的文件名。例如,
部分只是通过在开头添加下划线来表示)文件名。即如果之前是
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:
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.
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
你需要的不是通常在布局中使用的yield,而是渲染模板。
What you need is not yield - which is usually used in layout - but render template.