3

I'm having trouble accessing a helper method after upgrading to Rails 4.1.1. I have the following code in my application.

module ApplicationHelper

    def last_page_url
       session[:last_page]
    end

end

class Admin::ArticlesController < ApplicationController

    def update
       #....more code here
       return redirect_to self.last_page_url
    end

end

In Rails 4.0.x this code worked fine. After upgrading to Rails 4.1.1 I'm getting an error "undefined method 'last_page_url' whenever my update action runs. Why is this breaking now?

4

1 回答 1

1

我不确定为什么在升级到 Rails 4.1.1 后它停止工作,但正如@steel 所建议的,它与我的特定控制器中未包含的辅助方法有关。添加include ApplicationHelper到我的控制器的顶部会起作用,我可能可以通过将它添加到 ApplicationController 类中更进一步,因为我需要所有控制器都可以使用该方法。最后我选择了不同的解决方案:

首先,我把它last_page_url从类移到ApplicationHelperApplicationController类,这样我的所有控制器都可以访问它。然后我曾经helper_method使这个方法可用于我的所有视图。我的最终代码如下:

module ApplicationHelper

end

class ApplicationController < ActionController::Base
    # Prevent CSRF attacks by raising an exception.
    # For APIs, you may want to use :null_session instead.
    protect_from_forgery with: :exception

    def last_page_url
       session[:last_page]
    end
    helper_method :last_page_url

结尾

如果有人发现从 Rails 4.0 到 Rails 4.1 发生了什么变化,我会很想知道发生了什么。在这个特定的应用程序中,我只是在 development.rb 中使用默认的 Rails 4.1 设置。

于 2014-05-26T21:01:04.863 回答