rails101 第八章额外作业 实做“edit"和"delete"

1.先实做“delete”action。
(1)在app/controllers/posts_controller.rb 中写下如下代码:
def destroy
@group = Group.find(params[:group_id])
@post = Post.find(params[:id])

@post.destroy
redirect_to account_posts_path, alert: "Post deleted!"

end

(2)刷新网页测试。

2.实做“edit"action。
(1)在app/controllers/posts_controller.rb 中写下如下代码:
def edit
@group = Group.find(params[:group_id])
@post = Post.find(params[:id])
end

def update
@group = Group.find(params[:group_id])
@post = Post.find(params[:id])

if @post.update(post_params)
redirect_to account_posts_path, notice: "Update success!"
else
render :edit
end
end

(2)刷新网页测试,发现网页报错:


认真读报错,“a white screen"几个字提醒我们,原来是忘记建立一个edit.html.erb了!

(3)终端输入: touch app/views/posts/edit.html.erb
在该文件中填入和 app/views/posts/new.html.erb一样的内容。

(4)刷新网页测试。

Comments

comments powered by Disqus