https://avo.cool logo
Hey Guys, I getting a trouble trying to display...
# avo-2
a
Hey Guys, I getting a trouble trying to display a url on a edit view, I would like that it appears as a clickable link to show users a preview, but any try didn't works for me I would like this result
m
Thread automatically created by Rodrigo in #740893011994738751
but when I try field :url, as: :text link_to('https://google.com'/) end It doenst appear in the edit view
Do you guys know a way or a component that allows to get it?
l
I will let @loud-jewelry-99127 have a look too, but I would say that you need a custom field which will give you a view component where you can do wtv you want
otherwise, the built-in fields will always display form elements
l
HI @ambitious-midnight-63037 The code that you shared have some syntax errors, I've tried it with valid syntax and works as expected
Copy code
ruby
    field :url do
      # Navigate on current tab
      link_to('https://google.com', 'https://google.com')

      # Navigate on new tab
      link_to('https://google.com', 'https://google.com', target: "_blank")
    end
Let me know how it works for you
One question, I just notice that this thread is on avo-2 channel, what version of avo are you using?
Another thing that I just noticed is that is on a form, i missinterpreted the question and responded with show view in mind
Let me start over 😄
Field only appear on forms if there is an writter for them on the model
Lets assume you're on avo 3 and working on a
Course
model and
Avo::Resources::Course
resource
l
shouldn't we assume it's Avo 2?
l
YOure right
Let me switch to avo 2 and try it there
brb
@ambitious-midnight-63037 please confirm when you get the chance actually, the approach is different depending on what version are you using
a
Here Im using avo3
Copy code
field :published_url, as: :text, name: "Published Course URL", show_on: [:edit] do
      link_to(record.published_url, record.published_url, target: :_blank, rel: :noopener) unless record.published_url.nil?
    end
I would like that it appears as a url like in the index view but in the edit view
but the field not even appear, I dont know if we have a way to solve this
l
Thanks for confirming,
Lets assume you're working on a
Course
model and
Avo::Resources::Course
resource
To make the field to appear on the form you need to add a writter for it on the model
Copy code
ruby
class Course < ApplicationRecord
  attr_writer :url
end
Then assuming you want to show this field only on forms but with a show look you can use
only_on
options alongside the
components
configuration to achive it:
Copy code
ruby
# app/avo/resources/course.rb

class Avo::Resources::Course < Avo::BaseResource
  def fields
    field :url, only_on: :forms, components: {
      edit_component: Avo::Fields::TextField::ShowComponent
    } do
      # Navigate on current tab
      link_to('https://google.com', 'https://google.com')

      # Navigate on new tab
      link_to('https://google.com', 'https://google.com', target: "_blank")
    end
  end
end
Let us know how it goes
a
Great, it works nice 😁
Thank you so much
l
Sure, anytime, this is a bit hacky, specially the part to add an writter to the model to trick avo into rendering that in the form We're tracking native support for this use case in this issue: https://github.com/avo-hq/avo/issues/2140
a
Nice, it'll be good
2 Views