0

I wrote some like that:

login('POST', []) ->
    ....
            case Client:check_password(Req:post_param("password")) of
                true ->
                    {redirect, [{controller, "chat"}, {action, "live"}], [{username, Name}, {gender, Gender}]};
    ....
    end.

how to let the live action in chat controller receive the name and gender information? I wrote:

live('GET', [Req]) ->
   Name=Req:post_param("username"),
   ....
   {ok, [{username, Name}, {gender, Gender}]}

But it doesn't work, could anyone can help me>

halfelf
  • 9,737
  • 13
  • 54
  • 63
user1684696
  • 21
  • 4
  • 6

1 Answers1

0

What you are doing in your redirect is most likely not what you intended.

If you check the controller API documentation you will notice that the second proplist refers to request headers, not parameters ({redirect, Location, Headers::proplist()}). This means that you could access the values in your live/2 like this Req:header("HEADERNAME").

Secondly, in the definition of your live/2 controller function, you are assuming that you have captured a URL parameter (which you reference as Req). This is not reflected in your redirect in the login controller function. So what you can do is change the definition of your live/2 to live('GET', [Username, Gender]), this means you would have to call it like this /live/a-username/the-users-gender, then change the redirect to {redirect, [{controller, "chat"}, {action, "live"}, {username, Name}, {gender, Gender}]}.

However, I would recommend that you use sessions instead. Implement a before_/1 method on your controller and make sure that your module definition contains both the request and session_id parameters (check the documentation). Now you can just use the sessions API to pass around any session related values (it also has the benefit that you can clean up your controller functions - add a third parameter and simply distinguish between requests based on before_/1 results).

For example you could then have:

live('GET', [], undefined) -> 
    % redirect to the login page
live('GET', [], UserObject) ->
    {ok, [{username, UserObject:username()}, {gender, UserObject:gender()}]}.
Moritz
  • 36
  • 1