Sorcery and Simple Form implementation

百般思念 提交于 2020-01-03 02:33:09

问题


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

  • Sorcery
  • Omniauth
  • CanCan
  • twitter-bootstrap (converted to sass)

and Simple Forms.

Clean, clear and simple....Not.

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 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'"

Any help would be greatly appreciated.

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

回答1:


The documentation says:

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.

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)

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.

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

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

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.

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




回答2:


I'm trying to do this as well and getting stuck. It seems that this should work but it is causing an error when I submit the login form. Here's what my form looks like:

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

Not sure how to make this work. My debug output is showing that the form is trying to send the correct data:

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

Also, I"m getting this error in the console:

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

But I have resources :sessions in my routes.rb file.



来源:https://stackoverflow.com/questions/9650075/sorcery-and-simple-form-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!