如果params存在,Rails重构link_to

时间:2018-02-15 14:49:58

标签: ruby-on-rails

我使用Rails 5.1并拥有此代码段。 我怎样才能重构它以使其更具可读性?

在views / imagecapturings / index.html.erb

<% if params.has_key?(:select) %>
<%= link_to 'St. Gallen', digitized_in_stgallen_imagecapturings_path('select[year]' => params[:select][:year], 'select[month]' => params[:select][:month]) %>
<% else %>
<%= link_to 'St. Gallen', action: "digitized_in_stgallen", method: :get %>
<% end %>

1 个答案:

答案 0 :(得分:1)

您可以使用三元运算符

<%= params.has_key?(:select) ?
    link_to('St. Gallen', digitized_in_stgallen_imagecapturings_path('select[year]' => params[:select][:year], 'select[month]' => params[:select][:month])) :    
    link_to('St. Gallen', action: "digitized_in_stgallen", method: :get) %>