在Rails表单中访问项属性

时间:2022-10-17 15:57:01

If I am inside a form block in Rails, e.g

如果我在Rails中的表单块中,例如

form_for @widget do |f|
end

and am thus able to do things such as f.text_field :attribute etc, how can I also find out what the value of different attributes are in order to carry out some display logic?

因此我可以做f.text_field:attribute等事情,我怎样才能找出不同属性的值是为了执行一些显示逻辑呢?

For instance, something like

例如,像

form_for @widget do |f|
  f.text_field :some_property
  f.checkbox :active if ????.active
end

How can I access the properties?

我该如何访问这些属性?

2 个解决方案

#1


form_for @widget do |f|
  f.text_field :some_property
  f.checkbox :active if @widget.active?
end

#2


f.property.active

where property is any defined method or property of your @widget or the parent of Widget (ActiveRecord usually)

其中property是@widget的任何已定义方法或属性或Widget的父级(通常是ActiveRecord)

#1


form_for @widget do |f|
  f.text_field :some_property
  f.checkbox :active if @widget.active?
end

#2


f.property.active

where property is any defined method or property of your @widget or the parent of Widget (ActiveRecord usually)

其中property是@widget的任何已定义方法或属性或Widget的父级(通常是ActiveRecord)