2015-12-11 32 views
0

我在Rails 4應用程序中使用Ransack來搜索索引列表中的記錄。Rails4如何在控制器內傳遞變量

然後我在該索引列表上有一個鏈接到一個分數圖表頁面的按鈕。

我想使用scores graph索引中的@scores。

這裏是得分控制器:

def index 
    @search = current_user.scores.notarchived.search(params[:q]) 
    @scores = @search.result 
end 

def scoresgraph 
    @scores = params[:scores] 
end 

這是在索引頁按鈕:

<%= link_to 'Graph', scores_scoresgraph_path(:scores => @scores), :class => 'btn btn-xs btn-primary' %> 

但是,我越來越:未定義的方法`秩序」爲「#Score: :ActiveRecord_AssociationRelation:0x007fb5bdbf9a08" :字符串

回答

0

你應該提取裏面是什麼指數爲分離方法:

def index 
    @scores = score 
end 

def scoresgraph 
    @scores = score 
end 

private 

def search 
    @search ||= current_user.scores.notarchived.search(params[:q]) 
end 

def score 
    search.result 
end 

如果這不起作用,可能是因爲你沒有提到舊變量scoresgraph,所以你應該在的link_to更改爲:

<%= link_to 'Graph', scores_scoresgraph_path(q: 'same q as the index'), :class => 'btn btn-xs btn-primary' %> 
+0

我通過搜索PARAM Q值。謝謝! – Reddirt

相關問題