gem 'sorcery', '~> 0.13.0'
登录页面
<%= simple_form_for @user, url: backend_login_path do |f| %>
<div class="form-group">
<div class="form-label">
用户名
</div>
<div class="form-input">
<%= f.input :name, placeholder: "用户名", label: false, input_html: { required: 'required' } %>
</div>
</div>
<div class="form-group">
<div class="form-label">
密码
</div>
<div class="form-input">
<%= f.input :password, placeholder: "密码", label: false, input_html: { required: 'required' } %>
</div>
</div>
<div class="form-group">
<div class="form-label">
</div>
<div class="form-input">
<input type="submit" value="登录" />
</div>
</div>
<% end %>
登录路由
get 'login', to: 'auth#new'
post 'login', to: 'auth#create'
delete 'logout', to: 'auth#destroy'
登录
def new
@user = User.new
end
def create
@user = login(user_params[:name], user_params[:password])
if @user
session[:current_user_id] = @user.id
redirect_to backend_friend_links_path
else
flash.now[:alert] = 'Login failed'
redirect_to action: 'new'
end
end
def destroy
logout
redirect_to(:users, notice: 'Logged out!')
end
private
def user_params
user_params ||= params.require(:user).permit(:name, :password)
end