0

コメント付きの投稿を含むアプリがあります。

class Comment < ActiveRecord::Base
  belongs_to :post
end

既存のコメントを編集しようとすると、フォームが空白になります。

comments_form.html.erb:

 <%= form_for([@post, @post.comments.build]) do |post_comment_form| %>
    <div class="field">
      <%= post_comment_form.label :commenter %><br />
      <%= post_comment_form.text_field :commenter %>
    </div>
    <div class="field">
      <%= post_comment_form.label :body %><br />
      <%= post_comment_form.text_area :body %>
    </div>
    <div class="actions">
      <%= post_comment_form.submit %>
    </div>
<% end %>

コメント_コントローラー.rb:

def edit
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])
  end

  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(params[:comment])
    redirect_to post_path(@post)
  end

  def update
    #@post = Post.find(params[:post_id])
    #@comment = @post.comments.find(params[:id])

    respond_to do |format|
      #if @post.comments.update_attributes(params[:comment])
          if @comment.update_attributes(params[:comment])
        format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

コメントを編集するときにデータがフォームに表示されるようにするにはどうすればよいですか?

4

1 に答える 1

0

の新しいアクションをに変更します

def new
  @post = Post.new
  @comment = @post.comments.build
end 

編集アクションは通常どおりです

def edit
  @post = Post.find(params[:post_id])
  @comment = @post.comments.find(params[:id])
end

フォームビルダーは次のようになります

 <%= form_for([@post, @comment]) do |post_comment_form| %>
于 2012-09-25T21:15:39.297 に答える