[Ruby on Rails] and I learned to delete data

Source: Internet
Author: User

First, you need to add a delete link on the index page and provide a delete confirmation that the user confirms the deletion and deletes the data directly.

Modify views

Modify the App/views/posts/index.html.erb as follows:

  

That adds the

<%= link_to "Delete", post,: Confirm = "Is you sure",:method=>:d elete)

Post is the strength variable to remove: Confirm, confirm whether to delete,: Method of HTTP request, here is the delete request. See [Ruby on Rails] and I learned route map for details.

Modify action

For the action of the delete request is destroy, modify the Destroy method of the App/controllers/posts_controller.rb file as follows:

  def destroy    @post = Post.find (Params[:id])    @post. Destroy    redirect_to Posts_path,:notice=> "Your post has been deleted "  end

  

Running the service

When you run the service, the Discovery confirmation box does not come out (this issue will be subject to follow), others are normal.

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