簡體   English   中英

rescue_from NoMethodError

[英]rescue_from NoMethodError

有問題解決這個問題。

試圖做一個

rescue_from NoMethodError, :with => :try_some_options

但它不起作用。

編輯:為了測試我正在做一個簡單的重定向

def try_some_options
 redirect_to root_url
end

編輯2:我的控制器示例。 添加(例外),如下所示。

我知道我收到錯誤的原因。 使用Authlogic和authlogic_facebook_connect插件。 當從facebook插件創建用戶時,如果用戶在本地注冊,則不創建與用戶相關聯的“MyCar”模型。 因為我確實調用了用戶模型並在網站的不同部分引用了用戶汽車,所以我想做一些類似於你在下面看到的內容並最終將它放在我的application_controller中。

class UsersController < ApplicationController
 before_filter :login_required, :except => [:new, :create]
 rescue_from NoMethodError, :with => :try_some_options

 ...

 def show
    store_target_location
    @user = current_user
  end

 def create
  @user = User.new(params[:user])
  if @user.save
    MyCar.create!(:user => @user)
    flash[:notice] = "Successfully created profile."
    redirect_to profile_path
  else
    render :action => 'new'
  end
 end
 ...

 protected

 def try_some_options(exception)
    if logged_in? && current_user.my_car.blank?
       MyCar.create!(:user => current_user)
       redirect_to_target_or_default profile_path
    end
 end
 ...
end

編輯3:因為我知道錯誤出現的原因現在已經被黑了,但是想知道如何使用rescue_from NoMethodError

class UsersController < ApplicationController
 before_filter :login_required, :except => [:new, :create]
 before_filter :add_car_if_missing

 def add_car_if_missing
   if logged_in? && current_user.my_car.blank?
     MyCar.create!(:user => current_user)
   end
 end
end

我只是在試圖找出解決同一問題的方法時閱讀了你的帖子。 最后我做了以下幾點:

class ExampleController < ApplicationController
  rescue_from Exception, :with => :render_404
  ...

private

  def render_404(exception = nil)
    logger.info "Exception, redirecting: #{exception.message}" if exception
    render(:action => :index)
  end
end

這對我很有用。 這是一個捕捉所有情況,但它可能會幫助你。 祝一切順利。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM