Reputation: 381
I am trying to have a dashboard in my app that lists all the reviews the user has. The problem that i have is when i click on the post that the user gave a review in , it takes me to the index page of all the posts instead of the show page of the specific post. This is the line of code i am having issue with <td><%= link_to review.post.title , posts_path(@post) %></td>
. Here's my code:
views/pages/dashboard.html.erb
<div class="align-left">
<div class="col-md-2">
<h5><%= @user.name %></h5>
</div>
<div class="col-md-5">
<h3>My Posts</h3>
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Created</th>
<th></th>
</tr>
</thead>
<tbody>
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= time_ago_in_words(post.created_at) %> ago</td>
<td><%= link_to "Edit", edit_post_path(post) %>|<%= link_to "Destroy", post_path(post), method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<br>
<h3>My Reviews</h3>
<table class="table table-hover">
<thead>
<tr>
<th>Place</th>
<th>Created</th>
<th></th>
</tr>
</thead>
<tbody>
<% @reviews.each do |review| %>
<tr>
<td><%= link_to review.post.title , posts_path(@post) %></td>
<td><%= time_ago_in_words(review.created_at) %> ago</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
the Rake route file
Upvotes: 0
Views: 287
Reputation: 814
There is a instance variable @posts not @post on dashboard.html.erb page. And you can get post by review.post, like:-
<td><%= link_to review.post.title , post_path(review.post) %></td>
Also, Instead of call association again and again you can do this:-
<% @reviews.each do |review| %>
<tr>
<% review_post = review.post %>
<td><%= link_to review_post.title , post_path(review_post) %></td>
<td><%= time_ago_in_words(review.created_at) %> ago</td>
</tr>
<% end %>
Upvotes: 1
Reputation: 10251
You have not check your routes properly:
posts_path
is for index
method and for show
method it is post_path
with id
or object.
Use:
<td><%= link_to review.post.title , post_path(@post) %></td>
Upvotes: 0
Reputation: 8888
Should be <%= link_to review.post.title, post_path(@post) %>
,
or just <%= link_to review.post.title, @post %>
not <%= link_to review.post.title, posts_path(@post) %>
.
Upvotes: 1