我有两个问题,无法弄清楚原因!我遵循了http://ruby.railstutorial.org/chapters/
的教程Problem #1: My login and logout don't seem to be working
Problem #2: My private function doesn't see sign_in? -- I believe however is because sign_in? is declared in the sessionHelpers and I declared this function in the customerController which would explain why I can't see but don't know how to make it see.
这是我的代码! 模型/ customer.rb 控制器/会话
class SessionsController < ApplicationController
def new
end
def create
customer = Customer.find_by_email(params[:session][:email])
if customer && customer.authenticate(params[:session][:password])
# Sign the user in and redirect to the user's show page.
sign_in customer
redirect_back_or customer
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
sign_out
redirect_to root_path
end
end
控制器/客户
class CustomersController < ApplicationController
before_filter :signed_in_customer, only: [:edit, :update]
before_filter :correct_customer, only: [:edit, :update]
def new
@customer = Customer.new
end
def create
@customer = Customer.new(params[:customer])
if @customer.save
sign_in @customer
flash[:success] = "Welcome to Where you Where"
redirect_to @customer
else
render 'new'
end
end
def show
@customer = Customer.find(params[:id])
end
def edit
@customer = Customer.find(params[:id])
end
def update
@customer = Customer.find(params[:id])
if @customer.update_attributes(params[:customer])
flash[:success] = "Profile updated"
sign_in @customer
redirect_to @customer
else
render 'edit'
end
end
def index
@customers = Customer.all
end
private
def signed_in_customer
unless signed_in?
store_location
redirect_to signin_path, notice: "Please sign in."
end
end
def correct_customer
@customer = Customer.find(params[:id])
redirect_to(root_path) unless current_customer?(@customer)
end
end
助手/会话
module SessionsHelper
def sign_in(customer)
cookies.permanent[:remember_token] = customer.remember_token
self.current_customer = customer
end
def sign_in?
!current_customer.nil?
end
def current_customer=(customer)
@current_customer = customer
end
def current_customer?(customer)
customer = current_customer
end
def current_customer
@current_customer ||= Customer.find_by_remember_token(cookies[:remember_token])
end
def sign_out
self.current_customer = nil
cookies.delete(:remember_me)
end
def redirect_back_or(default)
redirect_to(session[:return_to] || default)
session.delete(:return_to)
end
def store_location
session[:return_to] = request.fullpath
end
end
再次感谢,抱歉拼错了
更新
应用程序控制器
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :pages
def pages
@pages = Page.all
end
include SessionsHelper
end
尝试编辑客户时出现错误
undefined method `signed_in?' for #<CustomersController:0xb56089ec>
app/controllers/customers_controller.rb:45:in `signed_in_customer'
注意::我刚刚做了db:reset并创建了一个新帐户。我是成功的但是标题中的链接显示我正在注销。
这里是头文件
<header class="header">
<div class="menu">
<div class="center">
<div class="logo">
<nav>
<ul>
<% if sign_in? %>
<div>Welcome <%= current_customer.full_name %> </div>
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Customers", customers_path %></li>
<li><%= link_to "Profile", current_customer %></li>
<li><%= link_to "Settings", edit_customer_path(current_customer) %></li>
<li><%= link_to "Sign out", signout_path, method: "delete" %></li>
<% else %>
<li><%= link_to "Home", '#' %></li>
<li><%= link_to "Sign in", '#' %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</div>
</header>
答案 0 :(得分:0)
确保在ApplicationController中包含帮助程序
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
end
这意味着在您的示例中从ApplicationController继承的任何控制器(例如CustomersController)都能够访问帮助程序中的方法
我没有查看你的所有代码,但请告诉我这是否解决了你的问题,如果不让我知道,我会有更长的看法