保华的Rails学习笔记

我的文章页修改删除加分题作业

1、在app/controllers/posts_controller.rb添加edit、update和destroy

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
    else
      render :edit
    end

  end

  def destroy
    @group = Group.find(params[:group_id])
    @post = Post.find(params[:id])
    if @post.destroy
      flash[:alert] = "删除成功"
      redirect_to account_posts_path
    end
  end

2、新建文件app/views/posts/edit.html.erb并添加以下内容:

<div class="col-md-4 col-md-offset-4">
  <h2>编辑讨论版</h2>
  <hr>

  <%= simple_form_for [@group,@post] do |f| %>
    <div class="form-group">
      <%= f.input :content %>
    </div>
    <div class="form-actions">
      <%= f.submit "Submit"%>
    </div>
  <% end %>



</div>