I've been trying to figure out how to pass a variable between two views and I've looked at all the examples on stack overflow and I can't seem to make it work.
I have this in my users -> index.html.erb
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td><%= user.id %></td>
<td><%= link_to 'Profile', user %></td>
<td><%= link_to 'Connect', new_relationship_path, :id => user.id %><td>
</tr>
<% end %>
I'm trying to pass user.id to my relationships -> new.html.erb view.
in my controller I have:
class RelationshipsController < ApplicationController
def new
@id = params[:id]
end
end
and finially I have relationships -> new.html.erb
<section id="main">
<h1>Sign Up</h1>
<td><%= @id %></td>
</section>
I believe :id isn't being passed correctly. What am I missing from all the other examples? I get no errors, just nothing is displayed.
This
link_to 'Connect', new_relationship_path, :id => user.id
is passing the :id as an html_option to link_to, so it will be used as an "id" attribute on your link element. What you want instead is to pass it as a parameter to the route helper:
link_to 'Connect', new_relationship_path(:id => user.id)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With