2015-11-25 23 views

回答

1

儘管Devise允許controller configuration,但通過模態登錄的表示方式在視圖內處理得更爲恰當。

默認情況下,Devise的視圖打包在gem中。要訪問視圖模板,你可以運行下面的命令:

rails generate devise:views 

運行上面的發電機將複製所有的設計的視圖模板在app/views/devise目錄,允許您根據需要修改標記。

有關配置視圖的更多詳細信息可在Devise's documentation中找到。

希望它有幫助!

0

你只需要重寫設計的控制器,例如:

控制器(控制器/ registrations_controller.rb)

class RegistrationsController < Devise::RegistrationsController 

# POST /resource 
def create 
    build_resource(sign_up_params) 

    resource.save 
    yield resource if block_given? 

    if resource.persisted? 
    if resource.active_for_authentication? 
     set_flash_message :notice, :signed_up if is_flashing_format? 
     sign_up(resource_name, resource) 
     respond_with resource, location: after_sign_up_path_for(resource) 
    else 
     set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format? 
     expire_data_after_sign_in! 
     respond_with resource, location: after_inactive_sign_up_path_for(resource) 
    end 
    else 
    clean_up_passwords resource 
    set_minimum_password_length 
    respond_with resource 
    end 
end 

protected 

    # Signs in a user on sign up. You can overwrite this method in your own 
    # RegistrationsController. 
    def sign_up(resource_name, resource) 
    sign_in(resource_name, resource) 
    end 

    # The path used after sign up. You need to overwrite this method 
    # in your own RegistrationsController. 
    def after_sign_up_path_for(resource) 
    after_sign_in_path_for(resource) 
    end 

    # The path used after sign up for inactive accounts. You need to overwrite 
    # this method in your own RegistrationsController. 
    def after_inactive_sign_up_path_for(resource) 
    scope = Devise::Mapping.find_scope!(resource) 
    router_name = Devise.mappings[scope].router_name 
    context = router_name ? send(router_name) : self 
    context.respond_to?(:root_path) ? context.root_path : "/" 
    end 

    # The default url to be used after updating a resource. You need to overwrite 
    # this method in your own RegistrationsController. 
    def after_update_path_for(resource) 
    signed_in_root_path(resource) 
    end 

    # Authenticates the current scope and gets the current resource from the  session. 
    def authenticate_scope! 
    send(:"authenticate_#{resource_name}!", force: true) 
    self.resource = send(:"current_#{resource_name}") 
    end 

    def sign_up_params 
    params.require(:user).permit(:name, :email, :password, :password_confirmation) 
    end 

    def account_update_params 
    devise_parameter_sanitizer.sanitize(:account_update) 
    end 

    def translation_scope 
    'devise.registrations' 
    end 
end 

助手(助手/ registrations_helper.rb)

module RegistrationsHelper 

    def resource_name 
    :user 
    end 

    def resource 
    @resource ||= User.new 
    end 

    def devise_mapping 
    @devise_mapping ||= Devise.mappings[:user] 
    end 
end 

路線(routes.rb中)

devise_for :users, :controllers => { :registrations => 'registrations' } 

模態(視圖/用戶/ _new_user.html.erb)

<div class="modal fade" id="form-validation-modal" tabindex="-1" role="dialog"> 
    <div class="modal-dialog"> 
    <div class="modal-content"> 
     <%= form_for(resource, 
        :as => resource_name, 
        :url => user_registration_path(resource), 
        :html => {:id => "sign_up_user"}) do |f| %> 

     <%= devise_error_messages! %> 
     <div class="modal-header"> 
      <h4 class="modal-title" id="myModalLabel"> 
      Create a new user 
      </h4> 
     </div> 
     <div class="modal-body"> 
      <div class="form-group"> 
       <%= render 'shared/error_messages' %> 

       <%= f.label :name, "Name:"%> 
       <%= f.text_field :name, class: "form-control" %> 
      </div> 
      <div class="form-group"> 
       <%= f.label :email, "Email:"%> 
       <%= f.email_field :email, class: "form-control" %> 
      </div> 
     </div> 
     <div class="modal-footer"> 
      <button id="dismiss" type="button" class="btn btn-default" data-dismiss="modal" style="float:left">Cancel</button> 
      <%= f.submit "Save user", class: "btn btn-success btn-medium", style: "float:right" %> 
     </div> 
     <% end %> 
    </div> 
    </div> 
</div> 
相關問題