How do I retrieve the values of a link in Ruby on Rails

n PHP, to retrieve the value of a link in PHP, all I have to do is use


Now in Ruby on Rails, fow do I do that? As an example, let say I have this link I want to retrieve its

id and use it in a form and this is the link

<%= link_to "Message", new_message_path %># This link will allow the viewer to message the profile owner

and this is the message script

<%= form_for(@message) do |f| %>

  <%= render 'shared/error_messages', object: @message %>

  <%= f.label :content %>
  <%= f.hidden_field :receiver, value:# The ID should be retrieved from the user id of the previous page %>
  <%= f.text_area :content, size:"20x15" %>
  <%= f.submit "Send message", class: "btn btn-primary" %>
  <% end %>

Query string params are available in a params hash in both your controller and view.

Normally, so as to keep logic out of your view layer, you would grab the parameter, or parameters that you are interested in in your controller and pass them through as an instance variable:

# controller
def index
  @link = params[:link]


// View
<%= @link %>

However, there is nothing stopping you referencing these params in the view directly:

// View
<%= params[:link] %>
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.