我有一個索引視圖變得稍微笨重,所以我將所有的數據庫查詢都移到了演示者的嘗試和清理之中。Presenter未定義的局部變量或方法`params'
但是,使用PARAMS [:東西]有任何查詢,使演示錯誤出具有:
undefined local variable or method params for QuestionPresenter:0x007fd6d569c158
我曾嘗試PARAMS遷入ApplicationController的輔助方法和模型,但沒有成功。
如何讓這些參數可用於演示者?或者演講者不是想要處理這些參數?
老question_controller.rb
def index
if params[:tag]
@questions = @question.tagged_with(params[:tag]).paginate(page: params[:page], per_page: 20)
elsif params[:search]
@questions = @question.paginate(page: params[:page], per_page: 20).search(params[:search])
else
@newest = @questions.newest.paginate(page: params[:page], per_page: 2)
@unanswered = @question.unanswered.paginate(page: params[:page], per_page: 2).search(params[:search])
@votes = @question.by_votes.paginate(page: params[:page], per_page: 2).search(params[:search])
end
end
QuestionsController.rb(新的索引操作)
def index
@presenter = QuestionPresenter.new
end
question_presenter.rb
class QuestionPresenter
def initialize
@questions = Question
@tags = Tag
end
def questions
@questions.paginate(page: params[:page], per_page: 20).search(params[:search])
end
def tags
@tags.joins(:taggings).select('tags.*, count(tag_id) as "tag_count"').group(:tag_id).order(' tag_count desc')
end
def tagged_questions
@questions.tagged_with(params[:tag])
end
def newest
@questions.newest.paginate(page: params[:page], per_page: 20)
end
def unanswered
@questions.unanswered.paginate(page: params[:page], per_page: 20)
end
def votes
@questions.by_votes.paginate(page: params[:page], per_page: 20)
end
end
index.html.erb
<%= render partial: "questions/tag_cloud", locals: {tags: @presenter.tags} %>
<% if params[:search] %>
<%= render partial: "questions/questions", locals: {questions: @presenter.questions} %>
<% elsif params[:tag] %>
<%= render partial: "questions/questions", locals: {questions: @presenter.tagged_questions}%>
<% else %>
<%= render partial: "questions/tabbed_index", locals: {questions: @presenter.newest, unanswered: @presenter.unanswered, votes: @presenter.votes} %>
<% end %>
'params'不是一個變量,它是一個返回HashWithIndifferentAccess的方法。 –
你是對的,但在這種情況下並不重要:) – ZedTuX
最後,@Linki顯示了我正在解釋的內容,所以應該清楚。 – ZedTuX