Ruby on rails 设置AuthLogic的问题
我正在尝试使用AuthLogic在我的用户表中设置一个简单的登录。每次我尝试,登录失败,我不知道为什么。我相信这是一个简单的错误,但我已经有一段时间用它撞到了砖墙Ruby on rails 设置AuthLogic的问题,ruby-on-rails,session,login,authlogic,Ruby On Rails,Session,Login,Authlogic,我正在尝试使用AuthLogic在我的用户表中设置一个简单的登录。每次我尝试,登录失败,我不知道为什么。我相信这是一个简单的错误,但我已经有一段时间用它撞到了砖墙 #user_sessions_controller def create @user_session = UserSession.new(params[:user_session]) if @user_session.save flash[:notice] = "Login successful!" else
#user_sessions_controller
def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:notice] = "Login successful!"
else
flash[:notice] = "We couldn't log you in. Please try again!"
redirect_to :controller => "index", :action => "index"
end
end
#_user_login.html.erb (this is the partial from my index page where Users log in)
<% form_tag user_session_path do %>
<p><label for="login">Login:</label>
<%= text_field_tag "login", nil, :class => "inputBox", :id => "login",
</p>
<p><label for="password">Password: </label>
<%= password_field_tag "password", nil, :class => "inputBox", :id => "password",
</p>
<p><%= submit_tag "submit", :class => "submit" %></p>
<% end %>
以下哪一项应更改为@user\u session?一个好主意是使用
form\u for
,如果可能的话:
<% form_for @user_session do |f| %>
<p>
<%= f.label :username %>
<%= f.text_field :username, :class => :inputBox %>
</p>
<p>
<%= f.label :password %>
<%= f.password_field :password, :class => :inputBox %>
</p>
<p><%= f.submit "submit", :class => :submit %></p>
<% end %>
:inputBox%>
:inputBox%>
:提交%>
除此之外,很难说。您的日志文件是否提供任何详细信息(即错误)?还可以尝试将表中的错误添加到flash中,以便查看出了什么问题
因为在上次更新中似乎没有设置@user\u会话,所以只需继续创建一个:
在您的情况下,params[:user\u session]是空的,因为它没有在您的视图中设置。我认为Jakub Hampl建议使用form_for是最好的方法,但您也可以使用form_标记,将输入名称设置为user_session[login]
和user_session[password]
,或者您可以将操作中的行更改为@user_session=UserSession.new(:login=>params[:login],:password=>params[:password])
.这是正确的。您可以通过检查该POST请求的日志来确认这一点。它将提交正确的字段,但它们的作用域与控制器中预期的用户会话不同。是否在索引控制器的
索引操作中分配@user\u会话?
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
def require_user
unless current_user
store_location
flash[:notice] = "You must be logged in to access this page"
redirect_to login_path
return false
end
end
def require_no_user
if current_user
store_location
flash[:notice] = "You must be logged out to access this page"
redirect_to root_url
return false
end
end
<% form_for @user_session do |f| %>
<p>
<%= f.label :username %>
<%= f.text_field :username, :class => :inputBox %>
</p>
<p>
<%= f.label :password %>
<%= f.password_field :password, :class => :inputBox %>
</p>
<p><%= f.submit "submit", :class => :submit %></p>
<% end %>