简体   繁体   中英

How to put sign_up form on the home page without Devise?

I came with How to display a custom sign_in form anywhere in your app , that uses Devise to place any form anywhere.

But we don't want to use Devise.

I have tried using:

<%= render 'sessions/new',  :locals => {:session => Session.new} %>

And in the form:

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

But it didn't work.

I've also tried load the session, it did not work too.

Does someone have any hint?

before_filter :load_categories
  protected

  def load_categories
    @sessions = Session.new
  end

inside routes.rb

  resources :sessions, only: [:new, :create]

inside app/controllers/session_controller.rb

class SessionsController < ApplicationController
  def new
    # just put empty here
    # rails will automatically render views/sessions/new.html.erb
    # makesure you have this file  
  end

  # I put create and delete session in case you need it later
  def create
    user = User.find_by_username(params[:session][:username])
    if user && user.authenticate(params[:session][:password])
        login user
        flash[:success] = "Anda berhasil login"
        redirect_to yours_path
    else
        flash.now[:error] = 'sorry cannot login'
        render 'new'
    end
  end

  def destroy
    session[:user_id] = nil
    flash[:success] = "Log out"
    redirect_to root_url
  end
end

inside views/sessions/new.html.erb (I put sample for login page so you can check)

<%= form_for(:session, url: sessions_path) do |f| %>
<h2>Please sign in</h2>
  <p>
    <%= f.label :username, "User name", class: "sr-only" %><br>
    <%= f.text_field :username, class: "form-control", placeholder: "User name" %>
  </p>
  <p>
    <%= f.label :password, class: "sr-only" %><br>
    <%= f.password_field :password, class: "form-control", placeholder: "Password" %>
  </p>
  <div class="checkbox">
    <label>
      <input type="checkbox" value="remember-me"> Remember me
    </label>
  </div>
  <%= f.submit "Log in", class: "btn btn-lg btn-primary btn-block" %>
  <h4>
    <%= link_to "Reset Password?", new_password_reset_path %>
  </h4>
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM