1

长期读者第一次使用。我正在整理我的第一个 RoR 应用程序,并将我的应用程序应该使用的所有内容隔离到:-

  • 巫术
  • Omniauth
  • 可以可以
  • twitter-bootstrap(转换为 sass)

和简单的形式。

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

在我的一生中,无法将简单的表单与巫术“登录”集成(这似乎是最简单的任务)而不会在“remember_me”字段上出现错误。

简单表单没有 simple_form_tag(仅 simple_form_for)选项,该选项在会话控制器新方法的登录表单上效果最佳。相反,我必须在该方法中创建一个@user 实例,但随后在“remember_me”字段“未定义方法‘remember_me’”上出现错误

任何帮助将不胜感激。

我的意思是大大!提前感谢:)

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
4

2 回答 2

1

文档说:

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登录表单中,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| %>

更新:我成功更改了示例应用程序的参考实现simple_form_for以用于创建新的用户会话。

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

form_for如果你好奇的话,这里是 Rails 文档。

于 2012-03-22T12:50:59.407 回答
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 :sessionsroutes.rb档案里有。

于 2012-09-16T19:55:28.813 回答