巫术和简单形式的实现 [英] Sorcery and Simple Form implementation

查看:85
本文介绍了巫术和简单形式的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长期阅读者的首次用户. 我正在整理我的第一个RoR应用程序,并隔离了我的应用程序应使用的所有内容:-

long time reader first time user. I'm putting together my first RoR application and I've isolated everything my app should use down to:-

  • 法术
  • Omniauth
  • CanCan
  • twitter-bootstrap(转换为sass)

和简单表单.

清洁,清晰和简单....不.

Clean, clear and simple....Not.

在我的一生中,无法将简单的表单(似乎是最简单的任务)与法术登录"集成在一起,而不会在"remember_me"字段上出错.

Cannot for the life of me integrate (what would seem to be the most simplest of tasks) simple forms with a Sorcery "Login" without getting errors on the 'remember_me' field.

简单表单不具有simple_form_tag(仅simple_form_for)选项,该选项最适合会话控制器new方法的登录表单.相反,我必须在该方法中创建一个@user实例,但随后在"remember_me"字段未定义的方法'remember_me'"上获取错误

Simple forms doesn't have a simple_form_tag (only simple_form_for) option which would work best on a login form from the sessions controller new method. Instead I have to create a @user instance in that method, but then get errors on the 'remember_me' field "undefined method `remember_me'"

任何帮助将不胜感激.

我的意思是太好了!提前大谢谢:)

I mean Greatly! Huge thanx in advance :)

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

推荐答案

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

This indicates that form_tag is intended to send data directly to another URL, handled by a controller action. Indeed, in the RailsTutorial signin form, Michael Hartl uses the form_for function instead of form_tag.

form_for(:session, url: sessions_path)

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

Basically, the idea is that you're sending data to be handled by the controller in some way, instead of writing to the database. Since we don't have a model for user sessions, we have to use the :session symbol instead of @session and tell the form where (which URL) it should POST the data to.

我不确定,虽然我不确定,但它也应与simple_form_for一起使用.像这样:

I suspect, though I'm not sure, that this should work with simple_form_for as well. Something like:

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

更新:我已成功将示例应用程序的参考实现更改为使用simple_form_for创建新的用户会话.

Update: I successfully changed the reference implementation of the sample app to use simple_form_for for creating new user sessions.

当然,这是假定Sessions控制器正在处理您的登录名,并且它具有响应POST的方法(可能是create).该控制器操作是您应该在Sorcery登录中进行处理的地方.

That is, of course, assuming that the Sessions controller is handling your login, and that it has a method that responds to POST (probably create). That controller action is where you should be handling the Sorcery login.

这是 form_for ,如果您好奇的话.

Here's the Rails documentation for form_for, if you're curious.

这篇关于巫术和简单形式的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆