https://avo.cool logo
Hey all, I've inherited a Rails app with a cus...
# fields
p
Hey all, I've inherited a Rails app with a custom admin, which I am switching out to use Avo. Just started off by setting up the basics and i've reached a part where Pages have a Template defined and the template defines what fields each page has. The content is a hash so in my Page Resource I have this: # Dynamically add fields from the template's structure template = record&.template return unless template&.structure_fields template.structure_fields.each do |name, settings| type = settings["type"] case type when "SingleText" field name, as: :text do record.content.fields[name] end when "RichText" field name, as: :textarea do record.content.fields[name] end when "Image" field name, as: :file else field name, as: :text do record.content.fields[name] end end end This dynamically builds the fields for each type. The issue is that when I Edit the Page, those fields do not show up. Any ideas as to why?
m
Thread automatically created by rctneil in #fields
l
Hi, that's because field blocks don't render on edit views, use format_using instead
p
Hi @loud-jewelry-99127 I've been looking into this further and getting a bit confused as to what route to take here. Let me try to explain: Scenario: I have a field that has a template of what fields should be available. The contents of those fields is also available. The fields from the template need to be rendered as fields and the content should fit to them. Firstly I wrote what I have above and it iterates over the template and outputs the fields and displays the content in each. That's good. I then noticed that they didn't show on the Edit view. You stated this is because thr Edit view does not support the block format, I switched to using format_using but they still fail to display on Edit. I've been doing a bit of research on this and I saw that because the actual data for these fields is stored in a hash, I need the name of the fields to be something like
content[:title
. and I was told by your AI in the docs that this cannot be done in regular fields and so i'd need a custom field. I looked into this and thought i could create a custom field that could be used something like this:
Copy code
field :stuff, as: :dynamic_content_from_template,
      template: record.template.structure_fields,
      content: record.content.fields
I tried this and my Show Component looks like:
Copy code
<% field.template.each do |name, settings| %>
  <% type = settings["type"] %>

  <% case type %>
  <% when "SingleText" %>
    <%= field_wrapper **field_wrapper_args do %>
      <%= field.content[name] %>
    <% end %>
  <% when "RichText" %>
    <%= field_wrapper **field_wrapper_args do %>
      <%= field.content[name] %>
    <% end %>
  <% end %>
<% end %>
but weirdly it outputs three rows when i'd expect two and i'm unsure how to get the label of the field to say the name of the field instead of "STUFF". Am I going the right way for something like this or not? or do I switch back to the way I had it before? It does appear that this setup is used on a number of models across the app so I guess a custom field would help in de-duplicating the code in some Resources.?? A few pointers would help massively! Thanks!
Hi all, Been continuing to pursue this. Mainly down the more simple route rather than the custom field route (as I think outputting multiple field inputs in one custom field is a bit silly). I currently have:
Copy code
record.template.structure_fields.each do |name, settings|
      type = settings["type"]
      case type
      when "SingleText"
        field name, as: :text,
          format_using: -> { record.content.fields[name] },
          for_attribute: :content
      when "RichText"
        field name, as: :textarea,
          format_using: -> { record.content.fields[name] },
          for_attribute: :content
      when "Image"
        field name, as: :file
      end
    end
    
  end
end
This does make the text and textarea fields actually display on the Edit view (they error when trying to save a change - > Content(#2214160) expected, got "How things work 2" which is an instance of String(#4440)) Any ideas how I should get this saved back correctly? The Template I have to work with looks like:
Copy code
--- !ruby/hash:TemplateStructureHash
title: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  type: SingleText
  options: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    validators: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
      presence: true
    searchable: true
introduction: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  type: RichText
content: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  type: RichText
  options: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    validators: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
      presence: true
    searchable: true
image: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  type: Image
  options: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    styles: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
      thumbnail: 232
      large: x600>
l
Hi @purple-park-97233 how is this going? any progress?
p
Hey Paul, thanks for checking in. I veered away from that issue for a little whilst I got started with one of the bigger parts of the app update (Paperclip to Active Storage) (which is progressing well but taking my time to ensure it’s done right). In regards to this issue, the fields now show on both the Show and Edit views but I still get the error when trying to save them. It’s been several days since I worked on that part so I’ll jump back and test it out and post an update after that.
l
Sure, it's hard for me to clearly understand the issue without having hands on the issue since reading through this thread carries a lot of context. My suggestion is for you to debug step by step, from the form submission until the DB request. Maybe you could use
update_using
if you need to preprocess the data before it hits the DB.
2 Views