Search code examples
twitter-bootstrapomniauthcancansimple-formsorcery

Sorcery and Simple Form implementation


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

Solution

  • 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.