[Ruby on Rails] and I learned to modify the data

Source: Internet
Author: User
Tags ruby on rails

Modify views

Modify the index view (app/views/posts/index.html.erb) and add the edit link as follows:

That is, add the following logic:

  <p><%= link_to "Edit", Edit_post_path (POST)%></p>

Among them, Edit_post_path, is the previous "[Ruby on Rails] and I learned route map" referred to the edit action prefix added a shortcut after the _path suffix, can accept parameters, here accept the post instance object.

Then edit the view and copy the new view as an edit view. Modify the following title and submit link to make it the appropriate information for the edit. As follows:

  

Modify actions

So next, you need to modify the action, is the edit action? or update? Because the load is get, modify edit first, as follows:

  def edit    @post = Post.find (Params[:id])  end

The above just handles the GET request for edit, and the post request needs to modify the update action as follows:

  def update    @post = Post.find (Params[:id])    if @post. Update_attributes (post_params)      redirect_to posts_ Path,:notice=> "Your Post has been updated"    else      render "edit"
End End

Where Post_params is the strong type mentioned in the previous section.

Running the service

Rails s, then open http://localhost:3000/posts, Edit and submit, such as:

Reprint please specify this article from: http://www.cnblogs.com/Tommy-Yu/p/4142096.html, thank you!

[Ruby on Rails] and I learned to modify the 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.