Search code examples
ruby-on-railsruby

How to pass arguments to controller from routes in rails?


I have a controller action as follows

def method params
  //work with params
end

and I want to pass arguments to the controller from routes.rb

get '/link' , to: 'controller#method'

and I don't want parameters to appear in URL.


Solution

  • get '/link' , to: 'controller#method', as: :my_link
    

    Use route helper to pass param

    <%= link_to 'My Link', my_link_path(param_1: "abc", param_2: "xyz")%>
    

    url will look like

    http://localhost:300/link?param_1=abc&&param_2=xyz
    

    At controller#method you can get these params as

    params[:param_1] = "abc"
    params[:param_2] = "xyz"