2012-03-10 68 views
1

长时间读者首次使用的用户。 我把我一起先RoR应用程序和我隔绝一切我的应用程序应该使用到: -魔法和简单的形式实现

  • 法术
  • Omniauth
  • 惨惨
  • Twitter的引导(转换为SASS)

和简单的形式。

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

不能为我的生活整合(看起来是最简单的任务)简单的形式与巫术“登录”,而不会在'remember_me'字段上得到错误。

简单的表单没有simple_form_tag(只有simple_form_for)选项,它最适合来自会话控制器新方法的登录表单。相反,我必须在该方法中创建@user实例,但在'remember_me'字段中找到错误'undefined method`remember_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 

回答

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

更新:我成功地改变了示例应用程序使用simple_form_for创建新的用户会话的reference implementation

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

如果您好奇,下面是关于form_for的Rails文档。

+0

关于如何使用omniauth-identity的任何想法?我试过'code'simple_form_for(:user,url:“/ auth/identity/register”)'/ code' – gittinOld 2012-03-31 03:44:06

+0

关于如何使用这与omniauth身份?我试过'simple_form_for(:用户,网址:“/身份证/身份证/注册“)'。 user_controller'def new @user = env ['omniauth.identity'] || User.new end'。 model/user.rb'class User gittinOld 2012-03-31 03:51:11

+0

如果可以,请尝试删除您的第一条评论,因为它没有像你期望的那样经历。我不知道为什么这样可以正常工作,并给你Heroku上的路由错误。作为调试步骤,您应该在本地和Heroku上运行'rake routes'。我真的建议你把这个问题作为一个新问题发布,让他人有机会在他们帮助你时赢得全部声誉。如果有帮助,也可以点赞所选的答案。 – jrhorn424 2012-03-31 20:52:34

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: [email protected] 
    password: test 
    remember_me: '1' 
commit: Login 
action: create 
controller: sessions 

另外,I“m如果这个错误在控制台:

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

但我在routes.rb文件有resources :sessions