Reputation:
I am new to rails and I am creating a basic blog application.
When I have created a post it has an author. If you locate the author it displays a list of posts that have been created by that author on their own individual page. Each of these links can be selected.
I want to now create a link on each post page which displays the name of the author and then allows you to select this and go back to the author's individual page.
So far I have added the following code to the pages view:
<%= link_to 'Back to List of Authors', authors_path %>
I'm now not sure how to change it so that instead if saying 'Back to List of Authors'
it displays the authors name and allows it to be selected and takes the user back to the author's individual page. Currently it takes you back to the complete list of authors and not in individual author.
Any advice will be appreciated.
Upvotes: 0
Views: 489
Reputation: 1194
You need to have a link to the specific author. authors_path is the path to the index action for the Authors controller (which lists all authors). You can do it as:
<%= link_to @author.name, author_path(@author.id) %>
or, more succinctly:
<%= link_to @author.name, @author %>
That will give the path to the show action for the Authors controller.
Check out http://guides.rubyonrails.org/routing.html (and the other guides are good) for more info.
Edit: Like other answers mentioned, assuming @author is your Author object.
Upvotes: 0
Reputation: 7705
<%= link_to "Back to author #{@author.name}", author_path(@author) %>
assuming @author is Author object
Upvotes: 0
Reputation: 64137
Something like this should work:
<%= link_to "Back to #{@post.author.name} page", @post.author %>
This is working under the assumption that you have a instance variable @post
that has the post data, and author belongs_to post.
Of course a more verbose way is to explicitly state the route like so:
<%= link_to "Back to #{@post.author.name} page", author_path(@post.author) %>
Upvotes: 2