0

So, I have two models: Customer, and Shop. Both have very different functionality, so separating them by roles will not benefit me as I see it.

I would like to put both registration forms on one page, and both sign in forms on one page. Say, for example, the left side would be like "Hey, log in as a customer!" And the other side it would say "Yo dawg, I heard you're a shop, login over here". I'm having difficulty separating the forms themselves, and I'm not entirely sure what I should do for the controller aspect as well.

From what I understand, the link to the login page has to be under the scope of the devise_for bit in the routes file. Seeing as the view/controller that I plan on holding the code for signing in or registering is not limited to one model, it doesn't seem to make sense doing it that way.

As far as I know, my main trouble will be figuring out the devise mapping, however, I think it may be more complex than that.

If there is any help or any resources I can be directed to, I would much appreciate it. Thanks.

SirUncleCid
  • 187
  • 1
  • 7
  • I know you said you don't like the idea of a role, but that might be your best bet. They could still be separate models but inherit from `User`, then you can define how each model works based on the role (ie. `User::Customer` / `User::Shop`). This would allow logging in from the same form without any crazy controller/routing logic. – Colto Mar 11 '16 at 17:15
  • Check out this answer with some good advice for this problem. http://stackoverflow.com/questions/5437948/using-devise-for-two-different-models-but-the-same-login-form – Colto Mar 11 '16 at 17:18
  • @ColtonFent I would like to use two different forms, however. – SirUncleCid Mar 11 '16 at 17:25

1 Answers1

0

So, I had figured it would be a little more complicated, but seemed to be rather simple.

So, I just routed to a custom controller action:

get '/login' => 'pages#login'

The action itself:

def login
  @shop = Shop.new
  @user = User.new
  render 'devise/sessions/new'
end

And the form:

<div class="col-md-offset-1 col-md-4">
  <h2>Mechanic Login</h2>
  <%= simple_form_for(@shop, as: :shop, url: session_path(:shop)) do |f| %>
    <div class="form-inputs">
      <%= f.input :email, required: false, autofocus: true %>
      <%= f.input :password, required: false %>
      <%= f.input :remember_me, as: :boolean if Devise.mappings[:shop].rememberable? %>
    </div>

    <div class="form-actions">
      <%= f.button :submit, "Log in", id: 'shop_submit' %>
    </div>
  <% end %>
</div>
<div class="col-md-offset-2 col-md-4">
  <h2>User Login</h2>
  <%= simple_form_for(@user, as: :user, url: session_path(:user)) do |f| %>
    <div class="form-inputs">
      <%= f.input :email, required: false, autofocus: true %>
      <%= f.input :password, required: false %>
      <%= f.input :remember_me, as: :boolean if Devise.mappings[:user].rememberable? %>
    </div>

    <div class="form-actions">
      <%= f.button :submit, "Log in", id: 'user_submit' %>
    </div>
  <% end %>
</div>
SirUncleCid
  • 187
  • 1
  • 7