<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="169445" data-post-id="169445">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="thojanssens1" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/thojanssens1/120/17642_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  thojanssens1
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="thiagomajesk" data-post="31" data-topic="27249">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/thiagomajesk/48/31904_2.png" class="avatar"> thiagomajesk:</div>
<blockquote>
<p>I’m not 100% sure about this but I think is just the Phoenix <a href="https://github.com/phoenixframework/phoenix/blob/3f8d0d058069b1f0a2af089a6c4c2d9efed73796/guides/request_lifecycle.md" rel="noopener nofollow ugc">request lifecycle </a> that makes the flash messages end up in the session…</p>
</blockquote>
</aside>
<p>The session must be used sparingly. Getting the data from <code>conn.private</code> and putting it into session before the response is sent is specific to the built-in Flash functionality. It’s not part of the general request lifecycle; it would be terrible if it were.</p>
<p><code>fetch_flash/2</code> returns a <a href="https://hexdocs.pm/plug/Plug.Conn.html#register_before_send/2" rel="noopener nofollow ugc"><code>register_before_send/2</code></a> function, which is a callback that gets called before the response is sent. And from there the data from <code>conn.private</code> is stored in session.</p>
<p>After some research, a simple solution using the built-in Flash mechanism to implement full-PRG (so including redirecting after invalid form submission), are the following steps:</p>
<ol>
<li>Invalid form is submitted, <code>create_*</code> action is called</li>
<li>Submitted <strong>form parameters</strong> are put into Flash, if changeset is invalid</li>
<li>Redirect to the <code>new_*</code> action</li>
<li>Rebuild the changeset based on parameters that were stored in Flash (if present)</li>
<li>Form displays all errors</li>
</ol>
<p>Sample code (example is a form allowing to add/edit/remove items for a user):</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def new_user_items(conn, %{"user_id" =&gt; user_id}) do
  changeset =
    Business.get_user!(user_id, preload: [:items])
    |&gt; Business.change_user(get_flash(conn, :create_user_items_params) || %{})  #  &lt;---
    |&gt; Map.put(:action, :update)

  render(conn, "new_user_items.html", changeset: changeset, user_id: user_id)
end
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir">def create_user_items(conn, %{"user_id" =&gt; user_id, "user" =&gt; user_params}) do
  user = Business.get_user!(user_id)

  case Business.add_items(user, user_params) do
    {:ok, _} -&gt;
      redirect(conn, to: Routes.registration_path(conn, :next_action, user_id))
    {:error, %Ecto.Changeset{} = changeset} -&gt;
      conn
      |&gt; put_flash(:create_user_items_params, user_params)  #  &lt;---
      |&gt; redirect(to: Routes.registration_path(conn, :new_user_items, user_id))  #  &lt;---
  end
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="169445" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/is-prg-a-valid-technique-in-phoenix/27249/32">Post #31</a>
	                </div>
	            </div>
              <div id="likers-container-169445" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="169445"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #31"></div>
  </section>
</div>
    <div class="postbit" id="169449" data-post-id="169449">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="thiagomajesk" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/thiagomajesk/120/31904_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  thiagomajesk
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="thojanssens1" data-post="32" data-topic="27249">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/thojanssens1/48/17642_2.png" class="avatar"> thojanssens1:</div>
<blockquote>
<p>The session must be used sparingly. Getting the data from <code>conn.private</code> and putting it into session before the response is sent is specific to the built-in Flash functionality. It’s not part of the general request lifecycle; it would be terrible if it were.</p>
</blockquote>
</aside>
<p>Maybe I was not clear enough: I didn’t mean it’s part of Plug’s request lifecycle - It’s actually part of Phoenix’s browser pipeline, but I’m not going to get cought in discussing semantics if I can avoid it.<br>
In my experience whether you should or shouldn’t use the session has to be decided in a case-by-case basis.</p>
<aside class="quote no-group" data-username="thojanssens1" data-post="32" data-topic="27249">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/thojanssens1/48/17642_2.png" class="avatar"> thojanssens1:</div>
<blockquote>
<p><code>fetch_flash/2</code> returns a <a href="https://hexdocs.pm/plug/Plug.Conn.html#register_before_send/2" rel="noopener nofollow ugc"> <code>register_before_send/2</code> </a> function, which is a callback that gets called before the response is sent. And from there the data from <code>conn.private</code> is stored in session.</p>
</blockquote>
</aside>
<p>Maybe I should have been a little more specific, but If you see my previous comment I specifically linked to the code inside the <code>register_before_send</code> function, since that section is only called because <code>fetch_flash</code> is include in the pipeline <img src="https://forum.elixirforum.com/images/emoji/apple/sweat_smile.png?v=15" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20">.</p>
<p>About using just <code>put_flash</code>: though, I wouldn’t go this route to write a lib (since this behaviour doesn’t exist for Plugs) I guess it’s perfectly fine if you are using Phoenix and want a “quick and dirty” solution just to pass data from one request to another.<br>
Also my purpose with Briefcase is to do a little bit more out-of-the-box. I hope this clears things up <img src="https://forum.elixirforum.com/images/emoji/apple/slightly_smiling_face.png?v=15" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:" loading="lazy" width="20" height="20">.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="169449" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/is-prg-a-valid-technique-in-phoenix/27249/33">Post #32</a>
	                </div>
	            </div>
              <div id="likers-container-169449" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="169449"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>