Я высушил один из наших контроллеров в нашем приложении rails 2.3 и столкнулся с проблемой, используя переменную экземпляра, назначенную в helper_method.Первоначально ситуация была такой:
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>
нажатие либо / или / популярного показало соответствующий блок популярных вопросов.
Теперь, так как запрос был в значительной степени продублирован, и так как paginateбудет использовать правильную страницу по умолчанию, я изменил это как:
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
Теперь, когда я перехожу к /, я получаю
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
в строке 1 home / _popular.html.haml
Кажется, что переменные, установленные из helper_methods, вызываемых из помощников, не доступны для шаблона.Я где-то ошибся?Если нет, то как мне использовать переменную экземпляра, назначенную в helper_method от помощника?