Sending a specific variable value for the link_to method in Rails

Is there a way to send a specific variable value for the link_to method in Rails? In my case, I want the @result variable to be 5 instead of 10 when the user clicks the link_to button.

<%= link_to t('meters'), full_report_path, class: 'btn btn-sm btn-success' %>

def full
  @result = 10
end
+4
source share
1 answer

You just need to send the parameters using link_to so that it is available in this method.

<%= link_to t('meters'), full_report_path(:result => 5), class: 'btn btn-sm btn-success' %>

in your method

def full
  if params[:result].present? 
    @result = params[:result]
  else 
    @result = 10
  end
end

You can try by checking if the method is called from the link, checking if the parameters are present, if they are 5 else 10. Thats it.

http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to you can find here.

+5
source

Source: https://habr.com/ru/post/1620884/


All Articles