[Ruby on Rails] and I learned to show the specified data

Source: Internet
Author: User
Tags ruby on rails

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 server

Execute 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.