巫术和简单形式的实现

时间:2012-03-10 20:49:57

标签: twitter-bootstrap omniauth cancan simple-form sorcery

长时间读者第一次使用。 我正在整理我的第一个RoR应用程序,并且我已经隔离了我的应用程序应该使用的所有内容: -

  • Omniauth
  • 惭惭
  • twitter-bootstrap(转换为sass)

和简单表格。

干净,清晰,简单....不。

不能为我的生活整合(看似最简单的任务)简单的形式与魔法“登录”而不会在'remember_me'字段上出错。

简单表单没有simple_form_tag(只有simple_form_for)选项,该选项在会话控制器新方法的登录表单上最有效。相反,我必须在该方法中创建一个@user实例,但是然后在'remember_me'字段中获取错误“undefined method`recleme_me'”

非常感谢任何帮助。

我的意思很棒!提前巨大的thanx:)

sessions/new.html.erb

<% provide :title, "Log in" %>
<h1>Log in</h1>
<%= simple_form_for @user, :html => { :class => 'form-horizontal' } do |f| %>
  <fieldset>
    <legend>Login</legend>

    <%= f.input :email, input_html: { :maxlength => 100 } %>
    <%= f.input :password, input_html: { :maxlength => 20 } %>
    <%= f.input :remember_me, as: :boolean %>


    <div class="form-actions">
      <%= f.submit nil, :class => 'btn btn-primary' %>
      <%= link_to 'Cancel', users_path, :class => 'btn' %>
    </div>
  </fieldset>
<% end %>

    class SessionsController < ApplicationController
  def new
    @user = User.new
  end

2 个答案:

答案 0 :(得分:1)

documentation说:

form_tag(url_for_options = {}, options = {}, &block)
Starts a form tag that points the action to an url configured with url_for_options just 
like ActionController::Base#url_for. The method for the form defaults to POST.

这表明form_tag旨在将数据直接发送到另一个URL,由控制器操作处理。实际上,在RailsTutorial signin form中,Michael Hartl使用form_for函数而不是form_tag

form_for(:session, url: sessions_path)

基本上,我们的想法是您以某种方式发送数据以由控制器处理,而不是写入数据库。由于我们没有用户会话的模型,因此我们必须使用:session符号而不是@session,并告诉表单将​​数据发布到哪个(哪个URL)。

我怀疑,虽然我不确定,这也适用于simple_form_for。类似的东西:

<%= simple_form_for(:session, url: sessions_path) do |f| %>

更新:我成功更改了示例应用的reference implementation,以使用simple_form_for来创建新的用户会话。

当然,假设Sessions控制器正在处理您的登录,并且它有一个响应POST的方法(可能是create)。该控制器操作是您应该处理Sorcery登录的地方。

如果您感到好奇,以下是form_for的Rails文档。

答案 1 :(得分:0)

我也试图这样做并且卡住了。看来这应该有效,但是当我提交登录表单时它会导致错误。这是我的表单的样子:

<h1>Log in</h1>

<%= simple_form_for(:session, url: sessions_path) do |f| %>

    <%= f.error_notification %>

    <%= f.input :email %>
    <%= f.input :password %>
    <%= f.input :remember_me, :as => :boolean %>

    <div class="form-actions">
    <%= f.submit "Login", :class => 'btn btn-primary' %>
    </div>
<% end %>

<%= link_to 'Forgot Password?', new_password_reset_path %>

不确定如何使这项工作。我的调试输出显示表单正在尝试发送正确的数据:

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ✓
authenticity_token: wGdDEmG91p7RHzWZKLGgMQvKD+XupS1Z557vNDDG6GM=
session: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  email: lee@example.com
  password: test
  remember_me: '1'
commit: Login
action: create
controller: sessions

另外,我在控制台中收到此错误:

gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms)

但我的resources :sessions文件中有routes.rb