- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读 Rails 指南 (http://guides.rubyonrails.org/getting_started.html),但卡在第 11 项“构建多模型表单”。
练习的这一部分解释了如何将一个模型的表单字段包含在另一个模型的表单中......
我的问题是第二个模型的表单字段没有显示..
代码完全按照练习(我什至绝望并直接从示例中复制粘贴代码)
我的代码发布在下面。我花了几个小时将其与指南中的代码进行比较,结果完全一样。任何人都可以指出可能出了什么问题吗?
非常感谢。
这是我包含部分的代码
<%= render :partial => 'tags/form',
:locals => {:form => post_form} %>
这是我的部分内容
<p>Above - THIS IS DISPLAYING</p>
<%= form.fields_for :tags do |tag_form| %>
<p>Inside - THIS IS NOT DISPLAYING</p>
<div class="field">
<%= tag_form.label :name, 'Tag:' %>
<%= tag_form.text_field :name %>
</div>
<% unless tag_form.object.nil? || tag_form.object.new_record? %>
<div class="field">
<%= tag_form.label :_destroy, 'Remove:' %>
<%= tag_form.check_box :_destroy %>
</div>
<% end %>
<% end %>
这是我的 Post 模型,设置为接受标签的嵌套属性
class Post < ActiveRecord::Base
validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
has_many :comments, :dependent => :destroy
has_many :tags
accepts_nested_attributes_for :tags, :allow_destroy => :true,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end
最佳答案
这是你做的吗?
We also add a @post.tags.build at the top of this form. This is to make sure there is a new tag ready to have its name filled in by the user. If you do not build the new tag, then the form will not appear as there is no new Tag object ready to create.
<% @post.tags.build %>
<%= form_for(@post) do |post_form| %>
…
如果这不起作用,请尝试将 @post.tags.build
添加到您的 PostController 的 new
方法中,看看是否能为您带来字段!
关于ruby-on-rails - Rails 指南 - 停留在多模型表单上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8834326/
这是我的代码 />100 1000 它不按要求工作.. 当提交表单时(并且在任何错误情况下)它返回到默认选中的单选按钮,即值 = 1000 用户必须再次单击值 = 100,而目标是,如果用户选择了 1
假设我有一个透明的红色 HTML 元素。当我悬停该元素时,它应该变成纯红色。当我停止悬停该元素时,它应该动画回到第一个状态,但仅在 X 秒后。 到目前为止一切顺利,请参阅代码片段。 我的问题是当我停止
我遇到了 cookie 情况,我的 cookie 会存储一个颜色名称或根本不存储任何内容。所以让我们这样解释吧。我的 cookie 与我网站的外观有关,我的网站有 3 种外观: 正常(完全没有 coo
这是我的问题。我有一张包含三个 div 的 Bootstrap v4 卡。 A 是最重要的一个,我希望它保持在左上角。 当页面较宽时,我希望 B 和 C 在 A 的右侧。 当页面变窄时,卡片缩小,C
示例表: uid time_stp traf 1 2016-01-13 00:00:00 6 1 2016-01-13 05:00:00 8 1
我是一名优秀的程序员,十分优秀!