Trinity76
Trinity76

Reputation: 665

Rails refactoring link_to if params present

I use Rails 5.1 and have this code snippet. How can I refactor it to make it more readable?

In 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 %>

Upvotes: 0

Views: 17

Answers (1)

Mikhail Katrin
Mikhail Katrin

Reputation: 2384

You could use ternary operator

<%= 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) %>

Upvotes: 1

Related Questions