【问题标题】:Problems setting up AuthLogic设置 AuthLogic 的问题
【发布时间】:2011-02-19 18:54:00
【问题描述】:

我正在尝试使用 AuthLogic 在我的用户表中设置一个简单的登录。每次我尝试,登录失败,我不知道为什么。我确定这是一个简单的错误,但我已经用它碰了壁。

#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 %>

我让 Faker 为我的用户表生成了一些数据,但我无法登录!每次我尝试它只是重定向到索引。我哪里错了?谢谢大家。

-----更新------

我刚才用 form_for 实现了 Jakub Hammpl 的建议 - 我遇到了一个新错误。

ActionView::TemplateError (called id for nil, which would mistakenly be 4 -
1: <% form_for @user_session do |f| %>
2: <% if flash[:notice] -%>
3: <p class="notice"><%= flash[:notice] %></p>
4: <% end -%>

app/views/index/_user_login.html.erb:1
app/views/layouts/index.html.erb:65
app/controllers/index_controller.rb:3:in `index'

Rendered rescues/_trace (86.0ms)
Rendered rescues/_request_and_response (1.0ms)
Rendering rescues/layout (internal_server_error)

我根本没有改变控制器。感谢所有回复这个话题的人——这对我非常有帮助。我现在可以做些什么来克服这个障碍?

-----更新#2-----

这是我的应用程序控制器。

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

其中哪一项应更改为@user_session?

【问题讨论】:

  • 是否在索引控制器的index 操作中分配了@user_session?

标签: ruby-on-rails session login authlogic


【解决方案1】:

如果可以的话,最好使用form_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 %>

除此之外很难说。您的日志文件是否提供任何详细信息(也称为错误)?还可以尝试将表格上的错误添加到您的 flash 中,以便您查看出了什么问题。

由于您上次更新时似乎没有设置@user_session,请继续创建一个:&lt;% form_for UserSession.new do |f| %&gt;

【讨论】:

    【解决方案2】:

    在您的情况下, params[:user_session] 是空的,因为它没有在您的视图中设置。我认为 Jakub Hampl 建议使用 form_for 是最好的方法,但您也可以通过将输入名称设置为 user_session[login]user_session[password] 来保留 form_tag,或者您可以将操作中的行更改为 @user_session = UserSession.new(:login =&gt; params[:login], :password =&gt; params[:password]) .

    【讨论】:

    • 这是正确的。您可以通过检查该 POST 请求的日志来确认这一点。它将提交正确的字段,但它们不像控制器中预期的那样限定在 user_session 范围内。
    猜你喜欢
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    相关资源
    最近更新 更多