rails new devise_modal -B -Tgem 'therubyracer', platforms: :rubygem "less-rails"gem 'twitter-bootstrap-rails', branch: 'bootstrap3' - for modal windows use bootstrapgem 'devise' authentication will be through devisebundle installrails g bootstrap:install static , "static" because nothing changes in the styles of bootstrap 'but we will notrails g devise:install; rails g devise User; rake db:migrate rails g devise:install; rails g devise User; rake db:migrate - install devise and create userrails g controller welcome index --no-helper --no-assetsroot 'welcome#index'users/sign_in and users/sign_up . <div class="modal hide fade in" id="sign_in"> <div class="modal-header"> <button class="close" data-dismiss="modal">x</button> <h2>Sign in</h2> </div> <div class="modal-body"> <%= form_for(User.new, url: session_path(:user), html:{id: 'sign_in_user', :'data-type' => 'json'}, remote: true) do |f| %> <div> <%= f.label :email %><br /> <%= f.email_field :email, autofocus: true %> </div> <div> <%= f.label :password %><br /> <%= f.password_field :password, autocomplete: "off" %> </div> <% if Devise.mappings[:user].rememberable? -%> <div> <%= f.check_box :remember_me %> <%= f.label :remember_me %> </div> <% end -%> <div> <%= f.submit "Sign in" %> </div> <% end %> </div> <div class="modal-footer"> </div> </div> <div class="modal hide fade in" id="sign_up"> <div class="modal-header"> <button class="close" data-dismiss="modal">x</button> <h2>Sign up</h2> </div> <div class="modal-body"> <%= form_for(User.new, url: registration_path(:user), html: {id: 'sign_up_user', :'data-type' => 'json'}, remote: true) do |f| %> <div> <%= f.label :email %><br /> <%= f.email_field :email, autofocus: true %> </div> <div> <%= f.label :password %><br /> <%= f.password_field :password, autocomplete: "off" %> </div> <div> <%= f.label :password_confirmation %><br /> <%= f.password_field :password_confirmation, autocomplete: "off" %> </div> <div><%= f.submit "Sign up" %></div> <% end %> </div> <div class="modal-footer"> </div> </div> <%= link_to "Sign in", "#sign_in", "data-toggle" => "modal", :class => 'btn btn-small' %> <%= link_to "Sign up", "#sign_up", "data-toggle" => "modal", :class => 'btn btn-small' %> <%= render 'shared/sign_in' %> <%= render 'shared/sign_up' %> <% if current_user %> <%= "Hello, #{current_user.email}" %> <%= link_to "Sign out", destroy_user_session_path, :method => :delete %> <% else %> <%= link_to "Sign in", "#sign_in", "data-toggle" => "modal", :class => 'btn btn-small' %> <%= link_to "Sign up", "#sign_up", "data-toggle" => "modal", :class => 'btn btn-small' %> <%= render 'shared/sign_in' %> <%= render 'shared/sign_up' %> <% end %> def resource_name :user end def resource @resource ||= User.new end def devise_mapping @devise_mapping ||= Devise.mappings[:user] end User.new , and instead of resource_name - :user . Also in app / views / shared / _sign_in.html.erb we Devise.mappings[:user] instead of devise_mapping . In general, you can generally get rid of this condition: <% if devise_mapping.rememberable? -%> based on whether we specify in the user’s model ( app / models / user.rb ) :rememberable . In addition, the app / views / shared / _sign_up.html.erb still had a helper devise_error_messages! which uses resource , but since the error text is taken from json 'and the answer, we simply remove from the form <%= devise_error_messages! %> <%= devise_error_messages! %> as unnecessary. config.http_authenticatable_on_xhr = true then returns 401.rails g controller Registrations --no-helper --no-assets --no-views devise_for :users, controllers: {registrations: 'registrations'} class RegistrationsController < Devise::RegistrationsController respond_to :html, :json end rails g controller Sessions --no-helper --no-assets --no-views devise_for :users, controllers: {sessions: 'sessions', registrations: 'registrations'} class SessionsController < Devise::SessionsController respond_to :html, :json end $ -> $("form#sign_in_user, form#sign_up_user").bind("ajax:success", (event, xhr, settings) -> $(this).parents('.modal').modal('hide') ).bind("ajax:error", (event, xhr, settings, exceptions) -> error_messages = if xhr.responseJSON['error'] "<div class='alert alert-danger pull-left'>" + xhr.responseJSON['error'] + "</div>" else if xhr.responseJSON['errors'] $.map(xhr.responseJSON["errors"], (v, k) -> "<div class='alert alert-danger pull-left'>" + k + " " + v + "</div>" ).join "" else "<div class='alert alert-danger pull-left'>Unknown error</div>" $(this).parents('.modal').children('.modal-footer').html(error_messages) ) respond_to :html, :json bundle install will start when you create the application, you will need to delete public/index.html and the path to the main one will look a little different: root to: 'welcome#index' Source: https://habr.com/ru/post/216837/
All Articles