How to get value from jsonb field only by name of field in Ruby on Rails?

61 Views Asked by At

Im new in admin_rails gem. I have partial that renders fields from model field that contains list of that fields. Fields and their values render in each loop, so in every iteration i have model of Field that contains name, data_type and other fields, but jsonb data store in other model. There is code, where fields passing as name in the loop. How can i get value to set default value to date_field, because im getting string from jsonb and calendar breaks down.

Here i have DataModuleField model, there is no jsonb data. Maybe im missing something?

<%= form.fields field.name, model: field.value do |dm_form| %>
  <% field.data_modules.each do |dm| %>
    <%= dm_form.fields dm.name, model: OpenStruct.new((field.value.presence || {})[dm.name]) do |field_form| %>
      <% dm.fields.each do |dm_field| %>
        <div class="form-group" style="margin-left: 0; margin-right: 0">
          <%= field_form.label dm_field.name, dm_field.label, class: 'col-sm-2 control-label' %>
          <div class="col-sm-10 controls">
            <% if dm_field.data_type == 'Float' %>
              <%= field_form.number_field dm_field.name, class: 'form-control', style: 'width: 50%', step: '0.000001' %>
            <% elsif dm_field.data_type == 'Enumeration' && dm_field.options.present? %>
              <%= field_form.select dm_field.name, ["", *dm_field.options], {}, class: 'form-control', style: 'width: 50%' %>
            <% elsif dm_field.data_type == 'Boolean' %>
              <%= field_form.check_box dm_field.name %>
            <% elsif dm_field.data_type == 'DateTime' %>
              <%= field_form.date_field dm_field.name, value: '%Y-%m-%d' %>
            <% elsif dm_field.data_type == 'Distribution' %>
              <%= render "data_modules_distribution_field", field: dm_field, form: field_form, model: OpenStruct.new(((field.value.presence || {})[dm.name].presence || {})[dm_field.name])%>
            <% else %>
              <%= field_form.text_field dm_field.name, class: 'form-control', style: 'width: 50%' %>
            <% end %>
          </div>
        </div>
      <% end %>
    <% end %>
  <% end %>
<% end %>

The problem is i cant render date. Rails trying to call 'strftime' for String from jsonb field.

I already tried to set default format for date_field as value: '%Y-%m-%d', all works but i cant set default value. On every update, if user didnt pick the date, setting date as today.

2

There are 2 best solutions below

0
Ariclene Chimbili On

I didn't understand exactly the main idea but if you want set a default value in date_field you can do that using "default" propriety instead "value" like this:

<%= f.date_field :date_field, default: Date.today %>

in your case maybe it can be like that:

<%= field_form.date_field dm_field.name, value: '%Y-%m-%d', default: your_default_date %>
0
Maksym Furgalo On

The solution was to use text field and set type as datetime:

<%= field_form.text_field dm_field.name, class: 'form-control', type: 'datetime-local' %>

so now rails understand to use this field correctly.