Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send parameter when use redirect in Phoenix

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.

like image 606
tajihiro Avatar asked Feb 11 '18 15:02

tajihiro


1 Answers

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))
like image 146
Dogbert Avatar answered Nov 14 '22 17:11

Dogbert