joseratts

joseratts

Help defining URL Rest structure for nested element

Hello everyone I’m creating an example phoenix app just for learning. My example app is a real estate web portal, so I’m gonna publish places (houses, apartments, etc). I have defined the following entities
Place (house, apartment) 1 → n rooms

So there is a 1 to n relationship between house and rooms and is a composition (so the rooms doesn´t exist if the house doesn’t exist).

My question is, Should be the url structure like following

/houses/house_id/rooms/room_id

If so assuming I have created both entities and liveviews

mix phx.gen.live Places House houses address:string rooms:integer image_path:string
mix phx.gen.live Places Room rooms name:string image:string

and for the last one I created a migration to improve the relationship between houses and rooms

...
    create table(:rooms) do
      add :name, :string
      add :image, :binary
      add :house_id, references(:houses, on_delete: :nothing)

      timestamps(type: :utc_datetime)
    end

    create index(:rooms, [:house_id])
  end

Are the following router.ex configuration OK? fulfilled the right URL structure ?

#Houses
live "/houses", HouseLive.Index, :index
live "/houses/new", HouseLive.Index, :new
live "/houses/:id/edit", HouseLive.Index, :edit
live "/houses/:id", HouseLive.Show, :show
live "/houses/:id/show/edit", HouseLive.Show, :edit
#Rooms
live "/houses/:house_id/rooms", RoomLive.Index, :index
live "/houses/:house_id/rooms/new", RoomLive.Index, :new
live "/houses/:house_id/rooms/:id/edit", RoomLive.Index, :edit
live "/houses/:house_id/rooms/:id", RoomLive.Show, :show
live "/houses/:house_id/rooms/:id/show/edit", RoomLive.Show, :edit

Any suggestion or comment will be welcome I just keep learning and following in love with the language and the framework. Thanks in advance

First Post!

sodapopcan

sodapopcan

This is really a matter of taste though there are at least a couple of things to consider.

If you have breadcrumbs, then having houses/:house_id/rooms/:room_id can certainly be useful. I personally really dislike this structure though. First, if the room were to change to a new house, its URL changes, and second, if I happen to know the id of a room and want to just type the URL in my address bar, I now need to go find its house id as well. Both problems could be solve by having rooms/:id redirect to houses/:house_id/rooms/:room_id, though that’s a little heavy-handed if you don’t strictly need it. And of course if you have a lot of routes where having them both at the top level becomes unmanageable, that’s a different story as well.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement