简体   繁体   中英

How do I use an instance variable assigned in a helper_method from a helper?

I've been drying up one of our controllers in our rails 2.3 app, and I've run up against a problem using an instance variable assigned in a helper_method. Originally, the situation was like this:

home_controller.rb:
class HomeController < ActionController::Base
  def index
  end

  def popular
    @popular_questions = PopularQuestion.paginate :page => params[:page],
                                                  <some complex query>
  end
end

home_helper.rb:
module HomeHelper
  def render_popular_questions
    @popular_questions = PopularQuestion.paginate :page => 1, 
                                                  <some complex query>
    render :partial => 'popular'
  end
end

home/index.html.haml
-cached do
  .popular=render_popular_questions

home/popular.html.haml
=render :partial => 'popular'

home/_popular.html.haml
-if @popular_questions.length > 0
  <show stuff>

hitting either / or /popular showed the appropriate box of popular questions.

Now, since the query was pretty much duplicated, and since paginate will use the correct page by default, I refactored this as:

home_controller.rb:
class HomeController < ActionController::Base
  helper_method :get_popular_questions

  def index
  end

  def popular
    get_popular_questions
  end

  private

  def get_popular_questions
    @popular_questions = PopularQuestion.paginate :page => params[:page],
                                                  <some complex query>
  end
end

home_helper.rb:
module HomeHelper
  def render_popular_questions
    get_popular_questions
    render :partial => 'popular'
  end
end

now when I go to /, I get

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.length

being raised in line 1 of home/_popular.html.haml

It seems that variables set from within helper_methods called from within helpers aren't accessible to the template. Have I made a mistake somewhere? If not, how do I use an instance variable assigned in a helper_method from a helper?

Pass them as parameters and local-variables:

home_controller.rb:
class HomeController < ActionController::Base
  helper_method :get_popular_questions

  def index
  end

  def popular
    @popular_questions = get_popular_questions
  end

  private

  def get_popular_questions
    # remember that the final statement of a method is also the return-value
    PopularQuestion.paginate :page => params[:page],
                                              <some complex query>
  end
end

home_helper.rb:
module HomeHelper
  def render_popular_questions
    questions = get_popular_questions
    render :partial => 'popular', :locals => {:questions => questions}
  end
end

now in your partial, use "questions" instead of "@popular_questions" Just make sure that the main template for "popular" also need to populate this local variable too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM