gpt4 book ai didi

ruby-on-rails - 在 Controller 中使用 using 方法测试辅助方法

转载 作者:行者123 更新时间:2023-11-28 20:51:43 25 4
gpt4 key购买 nike

module ApplicationHelper

def sortable(column, title = nil)
title ||= column
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
sort_arrow = ""
if(column == sort_column)
if(direction == "desc")
sort_arrow = "▼"
elsif(direction == "asc")
sort_arrow = "▲"
end
end
link_to sort_arrow+title, {:sort => column, :direction => direction,
:search_form => @search_form.attributes
}, {:class => css_class}

end
end

以上是我想测试的方法,问题是 sort_column 方法在 Controller 上,因此我收到错误消息 method does not exist .我如何摆脱它或如何让 Controller 执行该方法?

编辑: Controller :

class ResultsController < ApplicationController
def sort_column
params[:sort] != nil ? params[:sort] : 'ID'
end
end

最佳答案

简单的方法:使用 helper_method 声明将 sortable 方法移动到 ResultsController

class ResultsController < ApplicationController
helper_method :sortable

def sort_column
params[:sort] != nil ? params[:sort] : 'ID'
end

def sortable(column, title = nil)
title ||= column
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
sort_arrow = ""
if(column == sort_column)
if(direction == "desc")
sort_arrow = "▼"
elsif(direction == "asc")
sort_arrow = "▲"
end
end
link_to sort_arrow + title,
{:sort => column, :direction => direction,
:search_form => @search_form.attributes},
{:class => css_class}

end
end

关于ruby-on-rails - 在 Controller 中使用 using 方法测试辅助方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43965555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com