CodeSmith
CodeSmith

Reputation: 2233

Undefined helper method after upgrading to Rails 4.1.1

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?

Upvotes: 3

Views: 306

Answers (1)

CodeSmith
CodeSmith

Reputation: 2233

I'm not really sure why this stopped working after upgrading to Rails 4.1.1, but as @steel suggested, it had something to do with the helper method not being included in my particular controller. Adding include ApplicationHelper to the top of my Controller would have worked and I probably could have taken it a step further by adding it to the ApplicationController class since I needed that method available to all controllers. In the end I opted for a different solution:

First, I moved the last_page_url from the ApplicationHelper to the ApplicationController class so all of my controllers could access it. Then I used helper_method to make this method available to all my views. My final code is as follows:

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

end

If anyone ever figures out something changed from Rails 4.0 to Rails 4.1 I would be interested in learning what happened. In this particular application I'm just using the default Rails 4.1 settings in my development.rb.

Upvotes: 1

Related Questions