cfrostte
cfrostte

Reputation: 271

How can I add a query string parameter to a link generated in Ruby?

I want to send a data from an X view to a Y view, using a link. Then, in the Y view, I want to display the data I send from the X view:

/app/views/x.html.erb:

<%= link_to 'A', y_path('data if A') %>
<%= link_to 'B', y_path('data if B') %>

/app/views/y.html.erb:

#I show the data that I receive as parameter in the link

What do I have to do to do this?

Upvotes: 0

Views: 58

Answers (1)

Gerry
Gerry

Reputation: 10497

First assign the correct value to your links:

# x.html.erb

<%= link_to 'A', y_path(value: 'data if A') %>
<%= link_to 'B', y_path(value: 'data if B') %>

Then assign the value you get from the link in the controller:

# y_controller.rb

def y
  @value = params[:value]
end

Finally, show the value using @value in your view:

# y.html.erb

<%= @value %>

Upvotes: 1

Related Questions