使用多个模型时设计路由错误 - RAILS 4.2.0

时间:2016-07-21 06:33:03

标签: ruby-on-rails devise

实际上我正在为我的应用程序使用两个模型,即用户和管理员,我在使用设计宝石时按照每个步骤进行处理。

我想要多次登录。当用户登录时,必须重定向到相应的配置文件,当管理员登录时,必须重定向到他自己的个人资料。

我不知道我在哪里弄错了。

家/ index.html.erb

<ul>
    <li>User:<%= link_to 'User', new_user_session_path, target: "_blank" %></li>
    <li>Admin:<%= link_to 'Admin', new_admin_session_path, target: "_blank" %></li>
</ul>

当我转到用户链接时,它会给我路由错误,如下所示;

image1.png

当我转到管理员链接时,它会给我发送路由错误,如下所示;

image2.png

的routes.rb

Rails.application.routes.draw do

root "home#index"

  devise_for :users, controllers: {
        sessions: 'users/sessions',
        registrations: 'users/registrations'
      }   

  devise_scope :user do
    authenticated  do
      root to: 'aslani#index', as: 'authenticated_user_root'
    end

    unauthenticated do
      root to: 'aslani#index', as: 'unauthenticated_user_root'
    end
  end

  devise_for :admins, controllers: {
        sessions: 'admins/sessions',
        registrations: 'admins/registrations'
      }

  devise_scope :admin do
    authenticated  do
      root to: 'yaseen#index', as: 'authenticated_admin_root'
    end

    unauthenticated do
      root to: 'yaseen#index', as: 'unauthenticated_admin_root'
    end
  end

end

阿斯拉尼/ index.html.erb

        <% if user_signed_in? %>
          I am Aslani.
           <%= link_to 'Log out', destroy_user_session_path, method: :delete %>
        <% else %>
          <%= link_to 'Log In', new_user_session_path %>
          <%= link_to 'Sign Up', new_user_registration_path %>
        <% end %>

草/ index.html.erb

    <% if admin_signed_in? %>
       I am Kola.
      <%= link_to 'Log out', destroy_admin_session_path, method: :delete %>
    <% else %>
      <%= link_to 'Log In', new_admin_session_path %></li>
      <%= link_to 'Sign Up', new_admin_registration_path %>
    <% end %>

应用程序/控制器/用户/ sessions_controller.rb

 class Users::SessionsController < Devise::SessionsController
       def new
        super
       end

       def create
          self.resource = warden.authenticate!(auth_options)
          set_flash_message(:notice, :signed_in) if  is_navigational_format?
          sign_in(resource_name, resource)
          if !session[:return_to].blank?
            redirect_to session[:return_to]
            session[:return_to] = nil
          else
             respond_with resource, :location =>      after_sign_in_path_for(resource)
          end
     end
   end

欢迎任何建议。

提前谢谢。

2 个答案:

答案 0 :(得分:1)

当您在用户名空间下使用会话控制器时,

devise_for :users, controllers: {
        sessions: 'users/sessions',
        registrations: 'users/registrations'
}   

您的sessions_controller路径应为

app/controllers/users/sessions_controller.rb

    class Users::SessionsController < Devise::SessionsController
       def new
        super
       end

       def create
          self.resource = warden.authenticate!(auth_options)
          set_flash_message(:notice, :signed_in) if  is_navigational_format?
          sign_in(resource_name, resource)
          if !session[:return_to].blank?
            redirect_to session[:return_to]
            session[:return_to] = nil
          else
             respond_with resource, :location =>      after_sign_in_path_for(resource)
          end
     end
   end

你在阙中的表格应该在app/views/users/sessions/new.html.erb

  <h2>Log in</h2>

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <% if devise_mapping.rememberable? -%>
    <div class="field">
      <%= f.check_box :remember_me %>
      <%= f.label :remember_me %>
    </div>
  <% end -%>

  <div class="actions">
    <%= f.submit "Log in" %>
  </div>
<% end %>

<%= render "users/shared/links" %>

答案 1 :(得分:0)

如果您将路径中的控制器更改为

devise_for :users, controllers: { registrations: 'registrations', sessions: 'sessions', passwords: 'passwords', confirmations: 'confirmations', }

并将控制器保持为app / controllers / session_controller.rb将完成这项工作。