Home>
The problem I want to solve
[email protected] cms_site.pages.root.children.published.each do | seminar |
  li
    = link_to seminar.url (relative: true), {class:'seminars-card'}
      .seminars-card-image
      // Followed by the code below

The part of this code that is cms_fragment_content (: redirect_url, seminar)

If there is cms_fragment_content (: redirect_url, seminar)
As cms_fragment_content (: redirect_url, seminar) otherwise

seminar.url (relative: true)

I want to

The contents of the li tag

--if cms_fragment_content (: redirect_url, seminar) .present?
  = link_to cms_fragment_content (: redirect_url, seminar), {class:'seminars-card'}
--other
  = link_to seminar.url (relative: true), {class:'seminars-card'}
    .seminars-card-image
    // Followed by the code below


I tried it, but with this, the content under .seminars-card-image is

If cms_fragment_content (: redirect_url, seminar) .present? Is not called, it will not work as expected.

If anyone knows the solution, could you please tell me?

  • Answer # 1

    If it exists, that's it, and if it doesn't, it's different, so I think it's a good idea to use presence to put it together.

    = link_to ((cms_fragment_content (: redirect_url, seminar) .presence || seminar.url (relative: true)), {class:'seminars-card'})
      .seminars-card-image
        // Followed by the code below

    However, if you put it as it is, it will be difficult to understand as above, so it is better to put it in the variable once.

    --url = cms_fragment_content (: redirect_url, seminar) .presence || seminar.url (relative: true)
    = link_to url, {class:'seminars-card'}
      .seminars-card-image
        // Followed by the code below