Best practice for reusing html template?

Hello I have resources like so in my router.ex file

    resources "/groups", GroupController, only: [:index, :show] do
      resources "/addresses", AddressController
      resources "/shipments", ShipmentController

when creating a shipment, I want to reuse the address/new form. I was thinking inside the “new” template within ShipmentController doing a render call, but I wasn’t sure how that would look since group_address_path() is out of the scope of ShipmentController. Is there something considered best practice for addressing reusing a form from another controller?

Hi @sf193,

you can use render(@view_module, @view_template, assigns) to render (reuse) template of another module’s template file.

And group_address_path() is not actually of scope here. You can actually access the path’s helpers in all controllers, views or template.

Could you elaborate more on what you are trying to do?


hey @blisscs sure!

I want my shipments schema to look like this

field :sender_address, Address
field :other_info, :string
field :reciever_address, Address

when creating a new shipment, the process I want to take the user through is creating a new address --> then going to another form that submits other_info

From what I understand you want your schema to has a structure something like below (if I am right?) -

schema "shipments" do
  has_one :sender_address, Address
  field :other_info, :string
  has_one :receiver_address, Address

There are many possibilities to accomplish what you want. I am assuming you are using Phoenix to generate html also.

First possibility could be, in your form_for/4 section in the shipment form, you can have :inputs_for/4 for our case (inputs_for :sender, do .... end). and then use cast_assoc/3 in your changeset/2 function just make it saves the same time as we save our shipment.

Second possibility, you could use your frontend to make user flow from step1, then step 2 and then 3. then once the third step is finished send the the nested json object to server and make it save as once.

like mentioned above that there are many ways to implement this.

I hope this help. Do you have your code written some where, I think if we see your code or if you give us more info on what you want I think we can help you more.


Hey @blisscs
I’ve changed things up a bit since then, my code looks like so for shipments

schema "shipments" do

    has_many :routes, Route
   field :label, :string, null: false


and on routes side

  schema "routes" do
    field :label, :string, null: false
    field :address, :string
    field :date, :string
    field :groups, :string
    field :checklist, {:array, :string}
    belongs_to :shipment, Shipment # on_delete set in database via migration


I want to do it so that when I create a new shipment, i have a link that takes you to a form for creating a new route. It doesn’t seem like this is possible because I get

unknown field route. Only fields, embeds and associations (except :through ones) are supported in changesets

I’m coming up empty as to how to get around this problem, while still keeping my new/edit html as a form ala the generate.html template

Why not include Routes inside your Shipment form?

In this case, what you’re looking for is inputs_for/4:

Also, check the :append option.

What’s new in Ecto free ebook touches on this subject. Even if it’s for Ecto 2.1, 95% of the content still applies to Ecto 3.

Read this page, then search Shared Templates Across Views. It will tell you how to reuse template.