Editing topic tags as a list in a Phoenix form

This episode is part 2 in a Phoenix CMS series:

  • Series Overview - Planning
  • Part 1 - Setting up schemas, contexts and ecto relations
  • Part 2 - Editing multiple tags at once as a list
  • Part 3 - Listing mixed forms of content together for topics
  • Part 4 - Setting up meta tags for SEO
  • Part 5 - Making content-specific markdown extensions
  • Part 6 - Using ETS to cache information and speed up the site <hr />

Last episode, we started work on a unified tagging system for multiple types of content. We came up with a data model, generated the schemas and migrations we needed and put the basic pieces in place.

In this episode, we display do the following:

  • put videos in our episode pages
  • set up functions to add and remove tags on our various types of content en masse
  • create edit forms that use them
  • update styles to display the tags in a nicer way

Once we're done, it will be possible to quickly update an article or a video with a new set of tags and the episodes portion of the CMS will have the basic functionality we need. Next episode, we'll make pages that display all the content (of various types) with that topic tag.

(Source code available for premium members)

Level up your Elixir skills

Request a free email course

Back to index

4 Comments

This fkey constraint is on the join table, not on the topics!

- alchemist · 5 years ago

Trying to delete topics from the topics index page produces an error:

** (Ecto.ConstraintError) constraint error when attempting to delete struct:

* entities_topics_topic_id_fkey (foreign_key_constraint)

If you would like to stop this constraint violation from raising an exception and instead add it as an error to your changeset, please call foreign_key_constraint/3 on your changeset with the constraint :name as an option.

The changeset has not defined any constraint.

I cannot find the correct way to display an appropriate message for the error. Adding something like |> foreign_key_constraint(:entities, name: :entities_topics_topic_id_fkey, message: "There still is content related to that Topic") to the Topic changeset doesn't seem to work. Is this something discussed in the followup video's?

- jstender · 5 years ago

Thank you, this worked:


def delete_changeset(topic) do    
   topic    
     |> change()    
     |> foreign_key_constraint(:topic_id,      
       name: :entities_topics_topic_id_fkey,      
       message: "There still is content related to that Topic"    
      ) 
end
- jstender · 5 years ago

No problem!

- alchemist · 5 years ago