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.

Last Post!

sodapopcan

sodapopcan

Yes, just remove the underscore. As the warning said, an underscore indicates you will not use the variable. It is a little bit more than a convention since you get warnings, but as you can see _params actually still does bind the variable. On the other hand, a _ by itself actually throws the variable away—trying to reference _ won’t work. But it’s convention to mostly name your throw-away variables so it’s clear what they are.

As far as list_rooms_by_house goes, if you want to list by house_id, you aren’t gaining anything by putting the into a %House{} struct like that. If you want to first ensure the house exists then fetch the house from the database. There are differing opinion here but as you have it, I would just change the function to list_rooms_by_house_id(house_id). You could also fetch the house and preload the rooms.

def get_house_with_rooms(id) do
  House
  |> Repo.get(id)
  |> Repo.preload([:rooms])

What you do heavily depends on how your app works so it’s hard to go over everything. But, for instance, if you have users that can only access certain houses, then putting house_id in a hidden field is a security concern. You would want to first load the house and ensure the user is allow to read/write it (whatever the permissions may be). Then I would have a create_room(house, rooms_attrs) function (whether that lives in a Houses or Rooms context is up to you and a different discussion). Merely putting the house_id in a hidden field would allow a user to change that to whatever id they wanted. If that’s not a security concern then it’s no big deal, but if it is then you definitely shouldn’t do this.

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New

We're in Beta

About us Mission Statement