如何在表单中返回适当的嵌套属性值(编辑操作)?
我有以下结构:
class Project < ActiveRecord::Base
has_many :costs, :dependent => :destroy
accepts_nested_attributes_for :costs, :allow_destroy => true
end
class Cost < ActiveRecord::Base
belongs_to :project
end
假设成本模型中有两个属性:cost_plan(在新操作中使用)和cost_fact(在编辑中正确使用)。我在编辑表单时想做这样的事情:
<!-- _cost_fields.erb -->
<div title="<%= value of :cost_plan %>">
<%= f.label :cost_fact %>
<%= f.text_field :cost_fact %>
</div>
我可以使用 hidden_field
返回 :cost_plan
的值,但如何将其作为标题文本返回?
I have following structure:
class Project < ActiveRecord::Base
has_many :costs, :dependent => :destroy
accepts_nested_attributes_for :costs, :allow_destroy => true
end
class Cost < ActiveRecord::Base
belongs_to :project
end
Suppose there are two attributes in Cost model: cost_plan (used in new action) and cost_fact (properly used in edit). I'm tying to do something like this when editing form:
<!-- _cost_fields.erb -->
<div title="<%= value of :cost_plan %>">
<%= f.label :cost_fact %>
<%= f.text_field :cost_fact %>
</div>
I can return value of :cost_plan
using hidden_field
, but how to return it as title text?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需取回表单助手的对象并直接检索其关联对象即可:
Just get the object of the form helper back and retrieves it's associated objects directly: