rails,devise gem - 如何配置设计控制器?

时间:2015-11-25 05:08:08

标签: ruby-on-rails devise bootstrap-modal

我想在设计宝石中使用带有登录(和注册等)视图的模态窗口。 但我无法访问此视图。

如何配置设计gem控制器以使用模态?

2 个答案:

答案 0 :(得分:1)

虽然Devise允许controller configuration,但在视图中更正确地处理了通过模态登录的表示方面。

默认情况下,Devise的视图打包在gem中。要访问视图模板,可以运行以下命令:

$scope.homeclicked = function  {
    $scope.homechecked = true;
    $scope.thebandchecked = false;
    $scope.gallerychecked = false;
}

运行上面的生成器会将所有Devise的视图模板复制到rails generate devise:views 目录中,允许您根据需要修改标记。

有关配置视图的更多详细信息,请参阅Devise's documentation

希望它有所帮助!

答案 1 :(得分:0)

您只需要覆盖Devise控制器,例如:

控制器(controllers / 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' }

模态(views / users / _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>