将 Rails 参数哈希传递到命名路由的更有效方法

发布于 2024-10-09 12:53:25 字数 717 浏览 10 评论 0原文

我需要一种更有效的方法将参数哈希传递到命名路由,包括添加/删除/修改键/值对的能力。

添加键(:company 符号),同时保留 params 哈希的其余部分(手动指定每个符号/值):

# adds the company filter
link_to_unless params[:company]==company, company, jobs_path(:company=>company, :posted=>params[:posted],:sort=>params[:sort],:dir=>params[:dir])

删除键(消除 :company 符号),同时保留 params 哈希的其余部分(手动指定每个符号/值):

# create a link that removes the company filter
link_to_unless_current 'x', jobs_path(:posted=>params[:posted],:sort=>params[:sort],:dir=>params[:dir])

我想直接传递参数哈希,但这会引发异常:

link_to_unless params[:company]==company, company, jobs_path( params )

我希望有一些 DRYer 替代方案。

I need a more-efficient way to pass the params hash to a named route, including the ability to add/remove/modify a key/value pair.

Adding a key (the :company symbol), while preserving the remainder of the params hash (manually specify each symbol/value):

# adds the company filter
link_to_unless params[:company]==company, company, jobs_path(:company=>company, :posted=>params[:posted],:sort=>params[:sort],:dir=>params[:dir])

Removing a key (eliminates the :company symbol), while preserving the remainder of the params hash (manually specify each symbol/value):

# create a link that removes the company filter
link_to_unless_current 'x', jobs_path(:posted=>params[:posted],:sort=>params[:sort],:dir=>params[:dir])

I thought of just passing the params hash directly, but that throws an exception:

link_to_unless params[:company]==company, company, jobs_path( params )

I'm hoping for some DRYer alternatives.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幸福还没到 2024-10-16 12:53:25

重构辅助函数:

def options(hash)
    o={:employer=>params[:employer],:location=>params[:location],:posted=>params[:posted],:starting=>params[:starting],:sort=>params[:sort],:dir=>params[:dir],:fav=>params[:fav]}
    # add the contents of hash, overwriting entries with duplicate keys 
    o.merge!(hash)
end

重构视图代码以传递哈希而不是键/值对;更大的灵活性:

<%= link_to_unless params[:employer]==employer, employer, jobs_path( options({:employer=>employer}) ) %>

<%= link_to_unless_current '✗', jobs_path( options({:employer=>nil}) ) %>

Refactored the helper function:

def options(hash)
    o={:employer=>params[:employer],:location=>params[:location],:posted=>params[:posted],:starting=>params[:starting],:sort=>params[:sort],:dir=>params[:dir],:fav=>params[:fav]}
    # add the contents of hash, overwriting entries with duplicate keys 
    o.merge!(hash)
end

Refactored the view code to pass hash instead of key/value pair; greater flexibility:

<%= link_to_unless params[:employer]==employer, employer, jobs_path( options({:employer=>employer}) ) %>

and

<%= link_to_unless_current '✗', jobs_path( options({:employer=>nil}) ) %>
久随 2024-10-16 12:53:25

这种方法有效,但似乎不是最佳的:

helper:

def options(key, value)
    # create a hash of all params
    o={:employer=>params[:employer],:posted=>params[:posted],:starting=>params[:starting],:sort=>params[:sort],:dir=>params[:dir]}
    # set value
    o[key]=value
    # return hash
    o
end

view:

# change symbol's value
<%= link_to_unless params[:employer]==employer, employer, jobs_path( options(:employer, employer) ) %>

# remove symbol
<%= link_to_unless_current '✗', jobs_path( options(:employer, nil) ) %>

似乎我应该能够直接使用 params 哈希,但这现在可以工作。

This approach works, but doesn't seem optimal:

helper:

def options(key, value)
    # create a hash of all params
    o={:employer=>params[:employer],:posted=>params[:posted],:starting=>params[:starting],:sort=>params[:sort],:dir=>params[:dir]}
    # set value
    o[key]=value
    # return hash
    o
end

view:

# change symbol's value
<%= link_to_unless params[:employer]==employer, employer, jobs_path( options(:employer, employer) ) %>

# remove symbol
<%= link_to_unless_current '✗', jobs_path( options(:employer, nil) ) %>

It seems like I should be able to work with the params hash directly, but this will work for now.

瑾夏年华 2024-10-16 12:53:25

重构版本。将其放入目标控制器中,这样它就不会是全局的:

# new_params: new parameters you wish to pass on
# white_list: array of symbols, representing additional keys from existing params which you wish to pass on
def options(new_params, white_list = [])
  white_list += [ :employer,:location,:posted,:starting,:sort,:dir,:fav ]
  white_list.uniq!
  new_params.reverse_merge( params.slice(white_list) )
end
helper_method :options

Refactored version. Put this in the target controller so it won't be global:

# new_params: new parameters you wish to pass on
# white_list: array of symbols, representing additional keys from existing params which you wish to pass on
def options(new_params, white_list = [])
  white_list += [ :employer,:location,:posted,:starting,:sort,:dir,:fav ]
  white_list.uniq!
  new_params.reverse_merge( params.slice(white_list) )
end
helper_method :options
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文