elixir通过时价值
登录功能应以表格中的用户提供的变量,然后检查数据库中的给定用户是否存在。如果是,他将被重定向到主站点,如果没有,他将被重定向到同一表格,以再次提交他的名字。我有两个问题。
- 两个put_flash函数不起作用。
- 我不知道如何从登录中传递其他值(而重定向有userp:userp值,该值在:main中没有提供,而不是在渲染中提供:main)。如何添加这些其他论点?现在,它只能保留用户名,但是后来我希望它从数据库中处理整个用户规范(现在正在工作)。以下是代码
登录函数:
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 ->
put_flash(conn, :error, "Could not find the user")
#render(conn, "index.html", users: users, changeSet: changeSet)
redirect(conn, to: Routes.grant_path(conn, :index), users: users, changeSet: changeSet)
_ ->
put_flash(conn, :info, "Logged in")
#render(conn, "start.html", changeSet: changeSet, users: users, userp: userp)
redirect(conn, to: Routes.grant_path(conn, :main), users: users, changeSet: changeSet, userp: userp)
end
end
主函数:
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
索引函数:
def index(conn, params) do
changeSet = RewardappWeb.User.changeset(%RewardappWeb.User{}, %{})
render(conn, "index.html", changeSet: changeSet)
end
Router.ex看起来像这样:
get "/main", GrantController, :main
get "/", GrantController, :index
post "/users", GrantController, :login
谢谢您的任何帮助!!!
login function should take the variable provided by the user in the form, and then is checking if the given user exists in the database. If yes, he will be redirected to main site, if not, he will be redirected to the same form to submit again his name. I have two problems.
- Both put_flash functions don't work.
- I do not know how to pass additional values from login (while redirect there is userp: userp value, which is not provided in the :main, and not in the render in the :main). How could I add these additional arguments? Right now it only keeps user name, but later on I want it to handle the whole user specification from the database (it is working right now). Below is code
login function:
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 ->
put_flash(conn, :error, "Could not find the user")
#render(conn, "index.html", users: users, changeSet: changeSet)
redirect(conn, to: Routes.grant_path(conn, :index), users: users, changeSet: changeSet)
_ ->
put_flash(conn, :info, "Logged in")
#render(conn, "start.html", changeSet: changeSet, users: users, userp: userp)
redirect(conn, to: Routes.grant_path(conn, :main), users: users, changeSet: changeSet, userp: userp)
end
end
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
index function :
def index(conn, params) do
changeSet = RewardappWeb.User.changeset(%RewardappWeb.User{}, %{})
render(conn, "index.html", changeSet: changeSet)
end
the router.ex looks like this:
get "/main", GrantController, :main
get "/", GrantController, :index
post "/users", GrantController, :login
Thanks you in advance for any helping!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Elixir 是一种函数式语言。你不能就地改变“类”,然后在改变后使用它们,或者类似的事情。
您的
put_flash(conn, ...)
调用将创建一个修改后的conn
并将其扔到以太中。然后,
redirect(conn, ...)
调用将修改原始 conn(不带 flash 的 conn,而不是带 flash 的 conn),这就是您的登录函数最终返回的内容。这就是重定向有效的原因,但没有提示 - 它没有添加到您返回的 conn 中。
所以你想做的是像
I.E.您将 flash 添加到 conn,然后重定向结果,这就是您的函数应该返回的内容。
我希望这有帮助。
至于向重定向添加其他参数,这取决于您要添加的内容。如果它是查询参数,那么您可以执行
添加数据的另一种方法是将其放入会话中。
我猜你真正想要的是添加数据,然后在你正在渲染的 html 中使用这些数据。重定向不是这样工作的。它基本上是告诉浏览器,转到另一个位置并从头开始执行请求过程。
在某些情况下,您可能能够渲染不同的视图模板,而不是重定向并实现您想要的效果。
但这里是否可能确实取决于您的具体情况。
Elixir is a functional language. You can't mutate "classes" in place and then use them after mutation, or anything like that.
Your
put_flash(conn, ...)
call will create a modifiedconn
and throw it into the ether.Then the
redirect(conn, ...)
call will modify the original conn (the one without the flash, not the one with the flash) and that's what your login function actually ends up returning.So that's why the redirect works, but there is no plash - it wasn't added to the conn you're returning.
So what you want to do is something like
I.E. you add the flash to the conn, then redirect the result, and that's what your function should return.
I hope that helps.
As for adding additional arguments to the redirect, it depends on what you want to add. If it's query params, then you could do
The other way to add data would be to put it in session.
I'm guessing what you actually want is add data that is then used in the html you're rendering. Redirection doesn't work that way. It's basically telling the browser, go to this other location and go through the request process from the beginning.
In some cases, you might be able to render a different view template, rather than redirecting and achieve what you want.
But whether that's possible here really depends on your specific case.