1

我一直在关注 Michael Hartl 的 Rails 教程,对于那些熟悉的人,我在“第 10 课:更新、显示和删除用户”(使用 LiveLessons 视频版本)。

我想我可能会提到我已经将'redirect_back_or'的def添加到我的sessions_helper中,并将Include SessionsHelper函数添加到我的Applications_controller中。

我希望我们能解决这个问题,我在下面提供了日志。注意:谁能解决这个问题本身就是一个天才,因为在 Stack over flow 上这似乎是一个未解之谜:-)

登录时的错误:

SessionsController#create 中的 NoMethodError

undefined method `redirect_back_or' for #<SessionsController:0x007fdcdf04ba78>

Rails.root: /Users/user/Sites/rails_projects/sample_app1
Application Trace | Framework Trace | Full Trace

app/controllers/sessions_controller.rb:15:in `create'

Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"/SEaqnrMf5X0pd4FVWbu8uWVAjTNw4LPKiXg+8Hl0PQ=",
 "session"=>{"email"=>"user",
 "password"=>"[FILTERED]"},
 "commit"=>"Sign in"}

session_conrtoller.rb

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by_email(params[:session][:email])
    if user && user.authenticate(params[:session][:password])
       sign_in user
       redirect_back_or user
    else
       flash.now[:error] = "Invalid email/password combination"
       render 'new'
    end
  end

  def destroy
    sign_out
    redirect_to root_path
  end
end

完整跟踪:

pp/controllers/sessions_controller.rb:15:in `create'
actionpack (3.2.11) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.11) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.11) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.11) lib/active_support/callbacks.rb:414:in `_run__1623305808682769517__process_action__2997016911314885603__callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.11) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.11) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.11) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.11) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.11) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.11) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.11) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.11) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.11) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.11) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:601:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.4) lib/rack/etag.rb:23:in `call'
rack (1.4.4) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.4) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.4) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.11) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `_run__3609576242730521204__call__105225653498443597__callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.11) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.11) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.4) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.4) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.4) lib/rack/lock.rb:15:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.11) lib/rails/engine.rb:479:in `call'
railties (3.2.11) lib/rails/application.rb:223:in `call'
rack (1.4.4) lib/rack/content_length.rb:14:in `call'
railties (3.2.11) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.4) lib/rack/handler/webrick.rb:59:in `service'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

编辑新增内容//应用程序控制器:

class ApplicationController < ActionController::Base
  protect_from_forgery
  include SessionsHelper
end

// 会话助手

module SessionsHelper

  def sign_in(user)
    cookies.permanent.signed[:remember_token] = [user.id, user.salt]
    current_user = user
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
     @current_user ||= user_from_remember_token
   end

   def signed_in?
     !current_user.nil? 
   end

   def sign_out
     cookies.delete(:remember_token)
     self.current_user = nil
   end 


   def current_user?(user)
     user == current_user
   end

   def deny_access
     store_location
     redirect_to signin_path, :notice => "Please sign in to access this page." 
   end

   def store_location
     session[:return_to] = request.fullpath

     def redirect_back_or(default)
         redirect_to(session[:return_to] || default)
         clear_return_to
   end

   def clear_return_to
     session[:return_to] = nil
   end

  end

   private

   def user_from_remember_token
     User.authenticate_with_salt(*remember_token)
   end

   def remember_token
     cookies.signed[:remember_token] || [nil, nil]
   end



end

更新:

问题已解决,感谢 Beerlington 注意到 Store 定位器连接。+1。

这个:

    module SessionsHelper

  def sign_in(user)
    cookies.permanent.signed[:remember_token] = [user.id, user.salt]
    current_user = user
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
     @current_user ||= user_from_remember_token
   end

   def signed_in?
     !current_user.nil? 
   end

   def sign_out
     cookies.delete(:remember_token)
     self.current_user = nil
   end 


   def current_user?(user)
     user == current_user
   end

   def deny_access
     store_location
     redirect_to signin_path, :notice => "Please sign in to access this page." 
   end

   def store_location
     session[:return_to] = request.fullpath

     def redirect_back_or(default)
         redirect_to(session[:return_to] || default)
         clear_return_to
   end

   def clear_return_to
     session[:return_to] = nil
   end

  end

   private

   def user_from_remember_token
     User.authenticate_with_salt(*remember_token)
   end

   def remember_token
     cookies.signed[:remember_token] || [nil, nil]
   end



end

应该:

module SessionsHelper

  def sign_in(user)
    cookies.permanent.signed[:remember_token] = [user.id, user.salt]
    current_user = user
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
     @current_user ||= user_from_remember_token
   end

   def signed_in?
     !current_user.nil? 
   end

   def sign_out
     cookies.delete(:remember_token)
     self.current_user = nil
   end 


   def current_user?(user)
     user == current_user
   end

   def deny_access
     store_location
     redirect_to signin_path, :notice => "Please sign in to access this page." 
   end

  def store_location
     session[:return_to] = request.fullpath
end 

     def redirect_back_or(default)
         redirect_to(session[:return_to] || default)
         clear_return_to
   end

   def clear_return_to
     session[:return_to] = nil
   end


   private

   def user_from_remember_token
     User.authenticate_with_salt(*remember_token)
   end

   def remember_token
     cookies.signed[:remember_token] || [nil, nil]
   end



end
4

2 回答 2

1

您的辅助方法应如下所示

def redirect_back_or(default)
  redirect_to(session[:return_to] || default)
  session.delete(:return_to)
end

not clear_return_to

于 2013-02-14T22:37:26.270 回答
0

您已def redirect_back_or(default)在 Helper 中定义。您不能像现在这样从控制器访问它。

试试这个来称呼它:@template.redirect_back_or(...)或者@view_contest.redirect_back_or(...)(其中一个应该可以工作 - 我忘记了哪个是哪个版本的 Rails)。

另一种选择是将该方法移动到控制器中。(更好,国际海事组织)

于 2013-02-14T22:30:21.857 回答