Passing arguments with redirect to a function, that renders

Hi guys! I am creating a small app, and I have an issue. I have created a function that takes user’s provided username and checks, whether it is in the database. If it is, then it will be redirected to :main, where it is rendering the given file. Code looks like that:

def login(conn, %{“user” => userp}) do
changeSet = RewardappWeb.User.changeset(%RewardappWeb.User{}, userp)
#IO.inspect(userp)
users = Rewardapp.Repo.all(RewardappWeb.User)
#IO.inspect(changeSet)

#VALIDATION, IF GIVEN USER BELONGS TO DATABASE

value = userp["user"]

#map of lists
listOfUsers = Rewardapp.Repo.all(RewardappWeb.User)

IO.inspect(Enum.find(listOfUsers, fn x -> x.name == value end))

userSpec = Enum.find(listOfUsers, fn x -> x.name == value end)

IO.inspect(userSpec)

case userSpec do
  nil ->
    conn
    |> put_flash(:error, "Could not find the user")
    #render(conn, "index.html", users: users, changeSet: changeSet)
    |> redirect(to: Routes.grant_path(conn, :index), users: users, changeSet: changeSet)
  _ ->
    conn
    |> put_flash(:info, "Logged in")
    #render(conn, "start.html", changeSet: changeSet, users: users, userp: userp)
    |> redirect(to: Routes.grant_path(conn, :main), users: users, changeSet: changeSet, userp: userp)
end

end

and here is main function

def main(conn, params) do
changeSet = RewardappWeb.User.changeset(%RewardappWeb.User{}, %{})
users = Rewardapp.Repo.all(RewardappWeb.User)
render(conn, “start.html”, changeSet: changeSet, users: users)
end

Highly ask for any help guys!!!

Hey @MatOsinski, unless I’m missing something, I don’t see an actual issue mentioned in your post. What is your issue?

Also, can you take a moment and edit your post to fix the code formatting issues?

2 Likes

Hello @benwilson215. I have managed to fix my issue, I have used put_session instead of passing additional arguments :slight_smile: Also, I may say, the issue is now closed. However, thank you for your attention!