I have a rails model with a large number of fields (112), for loading a configuration. I would like, in the edit and show forms, display only if the field is already filled. That is, if the field is null in the database then do not display it for edit.
我有一个带有大量字段的轨道模型(112),用于加载配置。我希望,在编辑和显示表单中,仅在字段已填充时显示。也就是说,如果数据库中的字段为空,则不显示它以进行编辑。
There are two records - the main one is Batch and there is a 1:1 relationship to the Primer3Batch class. I'm trying to do an edit on the Primer3Batch class on the show action of Batch, I'm not sure if this is a good idea or will even work.
有两条记录 - 主要是Batch,与Primer3Batch类有1:1的关系。我正试图在Batch的show动作上对Primer3Batch类进行编辑,我不确定这是一个好主意还是甚至可以工作。
I have tried using the attributes method and am getting this error:
我尝试使用属性方法,并收到此错误:
undefined method `attributes' for #<SimpleForm::FormBuilder
batches_controller.rb
def show
@batch = Batch.find(params[:id])
@primer3 = Primer3Batch.where(:batch_id => @batch.id)[0]
respond_to do |format|
format.html # show.html.erb
format.json { render json: @batch }
end
end
batches/show.html.erb
<h1>Batch Details: <%= @batch.id %></h1>
<%= simple_form_for(@primer3) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<% f.attributes.each_attribute do |a| %>
<% if a %><%# if a is not nil %>
<%= f.input a %><%# send the field to the form %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
EDIT
Thank you JSWorld for pointing out the error of using the instance variable. I have corrected it and seem to have gotten further but it's still not quite right. This is the line changed - note attributes.each as attributes.each_attribute doesn't work.
感谢JSWorld指出使用实例变量的错误。我纠正了它,似乎已经进一步发展但它仍然不太正确。这是更改的行 - 注意attributes.each作为attributes.each_attribute不起作用。
<% @primer3.attributes.each do |a| %>
Now I am getting an error on the form fields:
现在我在表单字段上收到错误:
undefined method `["id", 110]' for #<Primer3Batch:
I guess I need to somehow turn this :
我想我需要以某种方式转变:
a ["id", 110]
into:
<%= f.input :id %>
* EDIT 2 *
*编辑2 *
Final code block based on IIya Khokhryakov's answer.
最终代码块基于IIya Khokhryakov的回答。
<%= simple_form_for(@primer3) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<% @primer3.attributes.each_pair do |name, value| %>
<%= f.input name if value %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
2 个解决方案
#1
1
@primer3.attributes
is a Hash of the model's attributes and their values. So you can do something like this:
@ primer3.attributes是模型属性及其值的哈希值。所以你可以这样做:
<% @primer3.attributes.each_pair do |name, value| %>
<%= f.input name if value %>
<% end %>
#2
2
I hope your intention is @primer3.attributes
and not f.attributes
. The error is because f
is the form object and does not have attributes
associated with it.
我希望你的意图是@ primer3.attributes而不是f.attributes。该错误是因为f是表单对象,并且没有与之关联的属性。
#1
1
@primer3.attributes
is a Hash of the model's attributes and their values. So you can do something like this:
@ primer3.attributes是模型属性及其值的哈希值。所以你可以这样做:
<% @primer3.attributes.each_pair do |name, value| %>
<%= f.input name if value %>
<% end %>
#2
2
I hope your intention is @primer3.attributes
and not f.attributes
. The error is because f
is the form object and does not have attributes
associated with it.
我希望你的意图是@ primer3.attributes而不是f.attributes。该错误是因为f是表单对象,并且没有与之关联的属性。