cduruk

cduruk

Hi folks!

I have a routing question. I have a model that has two field, one UUID field as a primary field and a title. I have those two working fine.

I’ve been trying to implement a custom to_param method for my model that has a UUID ID field. Basically, I’ll be generating “slugified” slugs from the title, and then base-62 encode the UUID as well, to get something like

/models/:slug_:id

However, no matter what I do, I can’t seem to get the to_param method be called, so the path_helpers are constantly returning the ID field.

So, here’s the model I have:

  @primary_key {:id, :binary_id, autogenerate: true}
  schema "posts" do
    field :title, :string

And this is the to_param I’ve been trying to wrangle:

defimpl Phoenix.Param, for: CMS.Post do
  def to_param(%{id: id, title: _title}) do
    "my_#{id}"
  end
end

For just testing, I want to see the to_param to be called so I can generate URLs in the Helpers. However, to no avail:


%App.CMS.Post{
  __meta__: #Ecto.Schema.Metadata<:built, "posts">,
  id: "foobar",
}

PheltWeb.Router.Helpers.posts_path(%URI{}, :show, slate)
"/posts/foobar"

You’ll see, while I expect a /posts/my_foobar, I keep seeing /posts/foobar

I do keep seeing this error in VSCode:

the Phoenix.Param protocol has already been consolidated, an implementation for CMS.Post has no effect. If you want to implement protocols after compilation or during tests, check the "Consolidation" section in the Protocol module documentation

I’m not sure if that’s related, but maybe? I was able to find this forum issue.

I tried the steps to “stop” consolidation on one of the answers, but didn’t seem to help. I saw this is fixed on master but what I little I was able to try, it still seemed broken with the same error message — or I was doing something very wrong.

I’m on Phoenix 1.5.7 and Ecto 3.5.6 if that helps.

I’m hoping I’m doing something utterly crazy dumb at this point…

Showing Posts 1 to 3

LostKobrakai

LostKobrakai

Why is the protocol for ˋCarto.Slateˋ and not ˋApp.CMS.Postˋ?

This one you shouldn‘t need to care for. Protocol consolidation happens per beam instance, so it only affects the elixir-ls node. If you don‘t see the same warning elsewhere there should be nothing to worry about.

cduruk

cduruk OP

I was removing some work-related naming :slight_smile: but missed a few, seems like it. I am sure the naming et al are all correct, though.

cduruk

cduruk OP

Well, seems like you got me. Nothing like a good-old asking someone the question to solve it myself.

I was implementing the protocol for Context.Model, not App.Context.Model. I changed it and voila!

Thanks a lot for the kind answer.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews