I have some functionality where I client can create custom fields, what I'm trying to do now is to loop through that data but I can't seem to get it to work.
Here are the params when we create or update a record, I've removed some as I'm only trying to loop through custom_fields_attr
Parameters: {... "custom_fields_attr"=>{"1"=>"testing12", "2"=>"", "3"=>"", "4"=>""}}, ...}
I have the following code:
<!-- start of custom fields -->
<% if Contact.has_custom_fields %>
<div class="form-block">
<h2 class="txt-title-alt">Custom Fields</h2>
<% Contact.custom_fields.in_groups_of(2).each do |field| %>
<div class="l-row-block clearfix">
<% field.each do |item| %>
<div class="l-06col l-ml-12col l-md-12col">
<div class="field field_display l-row-block clearfix">
<p class="like_p clearfix">
<strong class="l-06col">
<%= item.label %>:
</strong>
<span class="value l-06col">
<%= item.value %>
</span>
</p>
</div>
</div>
<% end %>
</div>
<% end %>
</div>
<% end %>
<!-- end of custom fields -->
I have a custom fields table which is where we get the label from but I need to get the value from the custom_fields_attr
hash
Edit The output of @contact.custom_fields_attr
is
>> @contact.custom_fields_attr
=> {"1"=>"test field123"}
Instead of doing field.each do |item|
try field.each do |key, value|
. This lets you access the key
and value
of each hash pair in your block.
Could you tell me what is the output of Contact.custom_fields
?
Can you tell, how the client is creating the custom fields, because if they are params send to the controller, you can access the params in the controller, in this case you want "custom_fields_attr", you can call it by params["custom_fields_attr"] and then to loop through it params["custom_fields_attr"].each do |key, value|
calling in each time key = "1" and value = "testing12" then key = "2" and value = "" and etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.