Rails:Michael Hartl 教程第 8 章 - 无法删除 Session
Rails: Michael Hartl Tutorial Chapter 8 - Can't Delete Session
在 Michael Hartl 的 Ruby Rails 教程(第 8 章)中实现会话时,我似乎无法使用使用方法 delete 的 link_to 标签来工作.
我想做的是删除本章中创建的 cookie,并将用户重定向到主页。我正在使用 Cloud 9/ C9 作为 IDE。
这是点击上述 link 后 console/server 日志中出现的内容:
Started DELETE "/logout" for 86.42.8.216 at 2017-06-29 12:24:57 +0000
Cannot render console from 86.42.8.216! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by SessionsController#delete as HTML
Parameters: {"authenticity_token"=>"dwH+Eu3big+otJ5nO9dzfJstEVLWSSIr1QrWQQ+g9WD7EdgHkUBVusE3Q1SBHh+SRIOsbNDH8WJwBGGoaZ9Xng=="}
No template found for SessionsController#delete, rendering head :no_content
Completed 204 No Content in 19ms (ActiveRecord: 0.0ms)
这是我的控制器:
class UsersController < ApplicationController
def new
@user = User.new
end
def show
@user = User.find(params[:id])
end
def create
@user = User.new(user_params) # Essentially passing in as argument the
# created user's attributes (name, email, etc)
if @user.save
# Handle a successful save.
redirect_to user_url(@user) # Using the named route 'user_URL' method
# to display show page for users, and passing
# in the user that was just created.
flash[:success] = "Welcome to the Sample App!"
log_in @user # Encrypts the ID of this newly created user. Refer to
# SessionHelper file
else
render 'new'
end
end
def destroy
log_out
redirect_to root_url
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end
以及路线:
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get 'login', to: 'sessions#new'
post 'login', to: 'sessions#create'
delete 'logout', to: 'sessions#delete'
# The following resounces method/keyword creates vitural URLs (/users e.g) and
# maps them to controller actions. This also makes the named routes for
# these controllers/actions available
resources :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
和视图 (header)
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if logged_in? %>
<li><%= link_to "Users", '#' %></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li>
<%= link_to "Log out", logout_path, method: :delete %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Log in", login_path %></li>
<% end %>
</ul>
</nav>
</div>
</header>
我已经搜索了相关问题,其中的答案提出了 Rail 的 application.html.erb 文件和 application.js 文件,所以我将 post 它们:
Application.html.erb
<!DOCTYPE html>
<html>
<head>
<title> <%= full_title(yield (:title)) %> </title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/r29/html5.min.js">
</script>
<![endif]-->
</head>
<body>
<!-- Header -->
<%= render 'layouts/header' %>
<!-- Body -->
<div class="container">
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>">
<%= message %>
</div>
<% end %>
<%= yield %>
</div>
<!-- Footer -->
<%= render 'layouts/footer' %>
<!-- Display debug information -->
<%= debug(params) if Rails.env.development? %>
</body>
</html>
Application.js
//= require jquery
//= require bootstrap
//= require jquery_ujs
//= require turbolinks
//= require_tree .
非常感谢任何帮助或建议。
从用户控制器剪切注销方法并将其粘贴到会话控制器
sessions_controller.rb
def destroy
log_out
redirect_to root_url
end
在 Michael Hartl 的 Ruby Rails 教程(第 8 章)中实现会话时,我似乎无法使用使用方法 delete 的 link_to 标签来工作.
我想做的是删除本章中创建的 cookie,并将用户重定向到主页。我正在使用 Cloud 9/ C9 作为 IDE。
这是点击上述 link 后 console/server 日志中出现的内容:
Started DELETE "/logout" for 86.42.8.216 at 2017-06-29 12:24:57 +0000
Cannot render console from 86.42.8.216! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by SessionsController#delete as HTML
Parameters: {"authenticity_token"=>"dwH+Eu3big+otJ5nO9dzfJstEVLWSSIr1QrWQQ+g9WD7EdgHkUBVusE3Q1SBHh+SRIOsbNDH8WJwBGGoaZ9Xng=="}
No template found for SessionsController#delete, rendering head :no_content
Completed 204 No Content in 19ms (ActiveRecord: 0.0ms)
这是我的控制器:
class UsersController < ApplicationController
def new
@user = User.new
end
def show
@user = User.find(params[:id])
end
def create
@user = User.new(user_params) # Essentially passing in as argument the
# created user's attributes (name, email, etc)
if @user.save
# Handle a successful save.
redirect_to user_url(@user) # Using the named route 'user_URL' method
# to display show page for users, and passing
# in the user that was just created.
flash[:success] = "Welcome to the Sample App!"
log_in @user # Encrypts the ID of this newly created user. Refer to
# SessionHelper file
else
render 'new'
end
end
def destroy
log_out
redirect_to root_url
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end
以及路线:
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get 'login', to: 'sessions#new'
post 'login', to: 'sessions#create'
delete 'logout', to: 'sessions#delete'
# The following resounces method/keyword creates vitural URLs (/users e.g) and
# maps them to controller actions. This also makes the named routes for
# these controllers/actions available
resources :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
和视图 (header)
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if logged_in? %>
<li><%= link_to "Users", '#' %></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li>
<%= link_to "Log out", logout_path, method: :delete %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Log in", login_path %></li>
<% end %>
</ul>
</nav>
</div>
</header>
我已经搜索了相关问题,其中的答案提出了 Rail 的 application.html.erb 文件和 application.js 文件,所以我将 post 它们:
Application.html.erb
<!DOCTYPE html>
<html>
<head>
<title> <%= full_title(yield (:title)) %> </title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/r29/html5.min.js">
</script>
<![endif]-->
</head>
<body>
<!-- Header -->
<%= render 'layouts/header' %>
<!-- Body -->
<div class="container">
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>">
<%= message %>
</div>
<% end %>
<%= yield %>
</div>
<!-- Footer -->
<%= render 'layouts/footer' %>
<!-- Display debug information -->
<%= debug(params) if Rails.env.development? %>
</body>
</html>
Application.js
//= require jquery
//= require bootstrap
//= require jquery_ujs
//= require turbolinks
//= require_tree .
非常感谢任何帮助或建议。
从用户控制器剪切注销方法并将其粘贴到会话控制器
sessions_controller.rb
def destroy
log_out
redirect_to root_url
end