我是一名编程新手。这是我的解决方案,但它无法登录。总是显示“无效的电子邮件/密码组合”。如何修改我的代码?如果有一些好的网络可以学习rails ? 谢谢
<% provide(:title, "Sign in") %>
<h1>Sign in</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(:session, url: sessions_path) do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :type %>
<%= f.radio_button :type, 1, :checked => true %> Student
<%= f.radio_button :type, 2 %> Teacher
<%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
<% end %>
<p>New students? <%= link_to "Sign up now!", signup_path %></p>
</div>
</div>
session Controller :
class SessionsController < ApplicationController
def new
end
def create
if [:type] == 1
user = User.find_by_email(params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
redirect_to user
#sign_in user
# Sign the user in and redirect to the user's show page.
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
else
teacher = Teacher.find_by_email(params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
redirect_to teacher
#sign_in user
# Sign the user in and redirect to the user's show page.
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
end
def destroy
sign_out
redirect_to root_url
end
def signed_in_user
unless signed_in?
flash[:notice] = "Please sign in"
redirect_to signin_url
end
end
end
最佳答案
当从表单提交一个值时,您的 Controller 将在 params
哈希中接收它作为字符串。您没有调用 params 哈希。您还在测试一个整数,所以 [:type] == 1
总是返回 false 因为您需要检查 params[:type]
其值为 "1"
或 "2"
。
有很多 Rails 教程,但最好从官方指南开始,网址为 guides.rubyonrails.org
关于html - rails :how to verify what kind of user log in?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20220915/