Using same form for show, create, edit

How should I structure the form.html template to use it in show, create and edit forms.

in New and Edit I use

<%= form_for @changeset, Routes.company_path(@conn, :create), [class: "form-horizontal"], fn f -> %>
   <%= render "form.html", conn: @conn, fp: f %>

but how can I pass proper arguments for Show:

<%= form_for @conn, Routes.company_path(@conn, :create), [class: "form-horizontal"], fn f -> %>
   <%= render "form.html", conn: @conn, fp: @company %>

Is this ok? How do you create show templates (read only data)?


Hello and welcome,

It’s not usual to have a form on a show page, but on new and edit.

The main reason is You don’t have a changeset to deal with…

Also I pass an action to form partial instead of fp… Something like this in a new page

  render "form.html", 
    action: Routes.admin_location_player_path(@conn, :create, @location),
    changeset: @changeset,
    cancel: Routes.admin_location_path(@conn, :show, @location)

and something like this in the form

<%= form_for @changeset, @action, fn f -> %>
1 Like

Thank you for the answer. Yes I found out that there is no changeset. I tried to pass a dummy one, or create one from data fetched in controller but no luck.
I tried to use same form with disabled fields/fieldset. As the design will be the same I tried to go this path, but I see it is not a standard way to do it.

Also new should point to create, and edit should point to update, with a resource.

In case You really want, You can pass a fake changeset, and disable all fields on show.

Because on the show action You have a resource, You can build a changeset from it.

But it’s not common to use a disabled form to display a resource.

1 Like