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 serviceRails 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