I have an Events page that is a list and I want a user to click on a specific Event to create a Request to be invited. I want the Request to be tied to that Event and want Event data (name, location, etc) to show on the new request form page. Events are one type of "object" and the Requests are another. One Event can have many Requests, but not the other way (so one-to-many). I can do a has_many/belongs_to relationship if that makes it better, or I can simply put an event_id in the Requests - I don't have a preference. This is a Phoenix 1.4 project, nothing special or custom, backed by a PostgreSQL DB.
What I can’t figure out is how to pass the Event, or the event_id, to the New Request form. Most everything in the Phoenix framework is really great, but I can’t figure out the right way to do this one simple thing.
What I have right now is more or less based on the basic guides:
project/lib/project_web/templates/tms/event/index.html.eex (Events list)
<table><tbody><tr><td><%= link event.title, to: Routes.tms_request_path(@conn, :new) %></td></tr></tbody></table>
This goes to lib/project_web/controllers/request_controller.ex (controller)
def new(conn, _params) do
changeset = TMS.change_request(%Request{})
render(conn, “new.html”, changeset: changeset)
end
This goes to lib/project/tms.ex (context)
def change_request(%Request{} = request) do
Request.changeset(request, %{})
end
Which goes to lib/project/tms/request.ex (schema)
def changeset(request, attrs) do
request
|> cast(attrs, [:name, :event_id, :email, :request_count]
|> validate_required([:name, :event_id, :email, :request_count]
lib/project_web/tms/request/new.html is a form with boxes for the attributes of the request
Everything with this works fine right now in that I can click on the link in the Events list and be taken to the New Requests form page and can save the request. Obviously, though there is no ID or association established this way. Obviously I want to do this the Elixir way and not hack something together. I've tried several different ways to pass event_id around but none of them seem to work, so I know I'm missing something but at this point, I can't figure out what that is.