1.1 URL params are mighty! Use them! Some helpers:

  • get current controller name & action name
<%= controller_name %>
<%= action_name %>
  • clean way to link to current path without helpers
<%= link_to "Refresh", controller: controller_name, action: action_name %>
  • even cleaner way to current path

<%= link_to “Refresh”, request.path %>

  • check if a controller/action name equals …
controller_name.eql?('x')
action_name.eql?('y')
  • see if we are now on a specific path
<%= current_page?(root_path) %>
  • get query_parameters for an url
  • localhost:3000/users?created_at=asc would return {"created_at"=>"asc"}
<%= request.query_parameters %>
<%= request.query_parameters.empty? %>

now you can do

<%= link_to 'clear search', request.path if request.query_parameters.any? %>
  • see all params that are applied to current URL
<%= params %>
  • controller and action params are always present in a request
  • BAD hacky way to see if any OTHER params are present:
  • better use the above request.query_parameters.present?
<%=(params.keys - ['controller'] - ['action']).present? %>
  • see if a particular params is present (true / false)
<%= params.key?(:messages_count) %>
  • display the value of a param (if present)
params[:messages_count].presence

1.2 Request-based manipulations

  • link_to current url via request with removing params
<%= link_to "Refresh", request.params.slice("query", "filter", "sort") %>
  • moving the above into a helper

#app/helpers/application_helper.rb

module ApplicationHelper
  def current_page_params
    # remove query params that you might have
    request.params.slice("query", "filter", "sort")
  end
end
<%= link_to "Refresh with helper", current_page_params %>
  • add params to above helper
<%= link_to "with foo", current_page_params.merge(foo: true) %>
  • ways to get link to current url
  • basic link to unless
<%= link_to_unless_current 'Inboxes', inboxes_path %>
  • link to controller & action
<%= link_to "Login",  controller: "user", action: "login" %>
  • link to with params
    <%= link_to "Profile", controller: "profiles", action: "show", id: @profile %>
    # => <a href="/profiles/show/1">Profile</a>
    
  • here, links are active only for admin user:
<% @posts.each do |post| %>
  <%= link_to_if @user.admin?, post.title, manage_post_path(post) %>
<% end %>
  • here, links are active if we are not on controller=inboxes, action=index
<%= link_to_unless controller_name.eql?('inboxes') && action_name.eql?('index'), 'Inboxes', inboxes_path %>
  • here, links are active if we are not on controller=inboxes
<%= link_to_unless controller_name.eql?('inboxes'), 'Inboxes', inboxes_path %>
  • link to either inboxes/index or inboxes/new
    <%=
    link_to_unless_current("New inbox", { controller: "inboxes", action: "new" }) do
      link_to("SCOTS", { controller: "inboxes", action: "index" })
    end
    %>
    

This is initially inspired by https://boringrails.com/tips/rails-link-to-unless-current