设计2.0.4,Rails 3.2.3 - 创建用户帐户后,我被重定向到root,无法访问其他路由

时间:2022-11-26 05:55:07

I'm coming from Sinatra to Rails and still quite new to Rails. My problem is that after I create a User account I am just directed to the index.html page in the /public folder and can't seem to access any other routes, I can't sign_out the user and I can't add another user.

我是从Sinatra来到Rails,还是Rails的新手。我的问题是,在我创建一个用户帐户后,我只是被定向到/ public文件夹中的index.html页面,似乎无法访问任何其他路由,我无法签署用户,我无法添加另一个用户。

I am using the devise gem to manage my user model and authentication. After installing the gem I followed the instructions on the devise github page.

我正在使用devise gem来管理我的用户模型和身份验证。安装gem之后,我按照devise github页面上的说明进行操作。

ie:

即:

rails generate devise:install

I also added to the 'config/environments/development.rb' file

我还添加了'config / environments / development.rb'文件

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

and I added to the 'config/routes.rb' file

然后我添加到'config / routes.rb'文件中

root :to => "home#index"

and I added to the 'app/views/layouts/application.html.erb' file

然后我添加到'app / views / layouts / application.html.erb'文件中

<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

Then I ran

然后我跑了

rails generate devise User

And finally

最后

rake db:migrate

Here is my user model

这是我的用户模型

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
  # attr_accessible :title, :body
end

Then I navigated to "/users/sign_up" and I entered an email and password after which I was redirected to the index.html page in the public folder.

然后我导航到“/ users / sign_up”并输入了一个电子邮件和密码,之后我被重定向到公共文件夹中的index.html页面。

The problem is I just seem to be stuck there. '/users/sign_out' yields

问题是我似乎被困在那里。 '/ users / sign_out'收益率

Routing Error

No route matches [GET] "/users/sign_out"
Try running rake routes for more information on available routes.

And running 'rake routes' yields

并运行'rake routes'收益率

new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                    root        /                              home#index

Seems like '/users/sign_up' should be a working route.

似乎'/ users / sign_up'应该是一条工作路线。

I am wondering if the problem is that there is no email service setup and this account is trying to be validated trough email? If so, how do I disable that?

我想知道问题是没有电子邮件服务设置,并且此帐户正在尝试通过电子邮件进行验证?如果是这样,我该如何禁用它?

Thanks! And let me know if you need more information or for me to clarify something.

谢谢!如果您需要更多信息或让我澄清一些事情,请告诉我。

===============UPDATE===================

=============== UPDATE ===================

The 'users/edit' route does work and I think possibly the problem lies in the fact that the route that is setup for 'users/sign_out' is a DELETE route. I forget the terminology about this, but I know that there is some sort of trickery in making a DELETE route out of a GET route. So is this where my problem lies?

'用户/编辑'路线确实有效,我想可能问题在于为'users / sign_out'设置的路由是DELETE路由。我忘记了关于这个的术语,但我知道在使用GET路由的DELETE路由时会有某种诡计。这就是我的问题所在吗?

3 个解决方案

#1


1  

You can do one of two things here.

你可以在这里做两件事之一。

Create a link to do the logout, something like this:

创建一个用于注销的链接,如下所示:

<%= link_to "Logout", destroy_user_session_path, method: :delete %>

<%= link_to“Logout”,destroy_user_session_path,method :: delete%>

Or add an additional route like this to your routes.rb file:

或者在routes.rb文件中添加这样的其他路由:

    devise_for :users, :skip => [:sessions]
    as :user do
        get 'signin' => 'devise/sessions#new', :as => :new_user_session
        post 'signin' => 'devise/sessions#create', :as => :user_session
        get 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
    end

The above example is from https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes

以上示例来自https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes

#2


0  

To sign out, you should be calling:

要退出,您应该致电:

destroy_user_session_path

Devise stores sessions, and DELETE just removes your session.

设计存储会话,DELETE只删除您的会话。

For the routing error, can you post up your HomeController?

对于路由错误,您可以发布HomeController吗?

And for the edit, are you calling:

对于编辑,你打电话:

edit_user_registration_path

#3


0  

A few moments ago I was having this problem and instead of looking for sites and more sites, it was when i stop here and one of the codes shown above is also shown below worked fine for me thanks

刚才我遇到了这个问题,而不是寻找网站和更多的网站,这是当我停在这里,上面显示的其中一个代码也显示在下面工作正常我感谢

devise_for :users, :skip => [:sessions]

as :user do
 get 'signin' => 'devise/sessions#new', :as => :new_user_session
 post 'signin' => 'devise/sessions#create', :as => :user_session
 get 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end

#1


1  

You can do one of two things here.

你可以在这里做两件事之一。

Create a link to do the logout, something like this:

创建一个用于注销的链接,如下所示:

<%= link_to "Logout", destroy_user_session_path, method: :delete %>

<%= link_to“Logout”,destroy_user_session_path,method :: delete%>

Or add an additional route like this to your routes.rb file:

或者在routes.rb文件中添加这样的其他路由:

    devise_for :users, :skip => [:sessions]
    as :user do
        get 'signin' => 'devise/sessions#new', :as => :new_user_session
        post 'signin' => 'devise/sessions#create', :as => :user_session
        get 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
    end

The above example is from https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes

以上示例来自https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes

#2


0  

To sign out, you should be calling:

要退出,您应该致电:

destroy_user_session_path

Devise stores sessions, and DELETE just removes your session.

设计存储会话,DELETE只删除您的会话。

For the routing error, can you post up your HomeController?

对于路由错误,您可以发布HomeController吗?

And for the edit, are you calling:

对于编辑,你打电话:

edit_user_registration_path

#3


0  

A few moments ago I was having this problem and instead of looking for sites and more sites, it was when i stop here and one of the codes shown above is also shown below worked fine for me thanks

刚才我遇到了这个问题,而不是寻找网站和更多的网站,这是当我停在这里,上面显示的其中一个代码也显示在下面工作正常我感谢

devise_for :users, :skip => [:sessions]

as :user do
 get 'signin' => 'devise/sessions#new', :as => :new_user_session
 post 'signin' => 'devise/sessions#create', :as => :user_session
 get 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end