How to send parameter when use redirect in Phoenix

tajihiro Source

I'm supposed to use assign/3 to send a message parameter like [1], when it comes to redirect/2. Then I would like to get message parameter in index action.

def index(conn, %{"member_id" => member_id}) do
  # Show index page.
  message = conn.params[:message]
end

def create(conn, _params) do
  case Casher.create_members(members) do
    {:ok, members} ->
    conn
      |> put_flash(:info, "Updated successfully.")
      |> assign(:message, message) #[1]
      |> redirect(to: page_path(conn, :index, member_id))
end

However it seems not to get parames in index action. How should I get a extra params like message. I don't like change parameters of index action.

elixirphoenix-framework

Answers

answered 8 months ago Dogbert #1

assign stores a value in the current request's connection struct. If you want to pass a value that should be read in the params of the redirected URL, you can pass it to the path generator function like this:

conn
  |> put_flash(:info, "Updated successfully.")
  |> redirect(to: page_path(conn, :index, member_id, message: message))

comments powered by Disqus