Im super new to programming and trying to get to grips with rails, I’m having trouble getting my head around how to route links to certain pages in my app.
I have a table of users reviews and venues, where a user has many reviews and a venue has many reviews.
The reviews are displayed as partials on the appropriate venues show page and each partial contains info on the user who wrote it.
review partial
<div class="review">
<% link_to @user do %>
<div class="reviewer_details">
<div class="reviewer_details_photo">
<%= image_tag review.user.avatar.url(:thumb) %>
</div>
<%= review.user.username %>
</div>
<% end %>
<div class="review_content">
<div class="review_partial_star_rating" style="width: <%= review.rating*20 %>px;"></div>
<h2 class="review_partial_title"><%= review.title %></h2>
</div>
<% if can? :update, review %>
<div class="review_partial_option">
<%= link_to 'edit', edit_review_path(review) %>
</div>
<% end %>
<div class="clearall"></div>
</div>
The line <% link_to @user do %> is redirecting to the current page (the venues show page), I want it to redirect to the users show page.
What am I missing here? and where can I read up on this to get it right? I’m confused as when to use @user, :user, user or User.
Thanks for any help its much appreciated!
This is a great start – looks like you just need to learn a bit more Ruby and you’ll get what’s going on here. You’ve got two questions, one about view helpers and the other about variables, symbols and constants. I’ll take a stab at each.
In your case, the
link_totag should be used something like this:Assuming that you’re using default Rails routing, you’re using the
link_tohelper to point to theusers_controller#showmethod for@user.The second part of your question is as follows:
Useris a constant for all of the User objects you want to create. Think of it as a blueprint for all users.User.newcreates an empty, unsaved user object for you to fill with the information you need.useris a variable. It will only be available within the context of whatever you’re working on. If you were to sayuser = User.newin your controller, thatuserobject would not be available in your view.@useris an instance variable. It works largely the same as standard variable, but if you were to define@user = User.newin your controller, you could use that object in the view.:useris a user symbol. It’s a little bit harder to define what Rails will use symbols for, but you don’t need it in this situation.I’d recommend that you check out Why’s Poignant Guide to Ruby, or read some other intro books to learn a bit more about the structure of the language – it’ll really help to bring clarity to why you’d want each of these.
Additionally, I use the http://www.railsapi.com for fast searching of the Ruby and Rails API.