According to "Ruby on Rails" and I learned the route map, we know that can be accessed GET
/posts/
:id
(.
:format
)
to display specific objects.
1. Modify the action
Modify the APP/CONTROLLERS/POSTS_CONTROLLER.RB show this action. Here's a puzzle, what if I get the parameters inside the URL?
can be accessed via the params built-in variable! As follows:
def show @post = Post.find (Params[:id]) end
2. Modify the View
Modify the App/views/show.html.erb view file as follows:
3. Run the serverExecute the instructions rails s can. Open the link http://localhost:3000/posts/1, such as:
But this will require the user to remember the ID, not very good, improve the following index page:
Link_to connected to the field, URL map shortcut, in [Ruby on Rails] and I learned the route map mentioned, show prefix is post, as follows:
post
GET
/posts/
:id
(.
:format
) posts
#show
After the improvement, the effect is as follows:
Reprint please specify this article from: http://www.cnblogs.com/Tommy-Yu/p/4141339.html, thank you!
[Ruby on Rails] and I learned to show the specified data