2016-07-17 12 views
1

ポストのコメントを更新しようとしていますが、正しく編集するために編集フォームの部分を取得できますが、保存しようとしてもコンソールエラーはありません。サーバーログにエラーがあり、レコードは保存されません。rails 4 ajax編集フォームが起動しない

$('comment-form').hide(); 
$('#comment-<%= @post.id %>-<%= @comment.id %>').find('.comment-content').html('<%= @comment.content %>'); 

routes.rbを

resources :users do 
end 

resources :posts do 
    resources :comments 
end 


class Post < ActiveRecord::Base 
    belongs_to :user 
    has_many :comments, dependent: :destroy 
end 

class User < ActiveRecord::Base 
    has_many :posts, dependent: :destroy 
    has_many :comments, dependent: :destroy 
end 

class Comment < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :post 
end 
update.js.erb

edit.js.erb

$('#comment-<%= @post.id %>-<%= @comment.id %>').find('.comment-content').html("<%= j render partial: 'comments/comment_form', locals: { comment: comment, post: post } %>"); 

コメント/ _comment_form.html.erb

<div class="comment-form col-sm-11"> 
      <%= form_for([post, comment], :remote => true) do |f| %> 
      <%= f.text_area :content, class: "comment_content", id: "comment_content_#{post.id}" %> 
     </div> 

     <div class="col-sm-1"> 
     <%= f.submit "Save", class: "btn btn-primary btn-xs" %> 
     <% end %> 
</div> 

以下は、edit_post_comment_pathリンクがクリックされたときのサーバーログの最後のものです。文字通り、編集フォームの部分でsaveがクリックされたときに何も起こらず、update.js.erbのテストアラートは起動しません。

Started GET "/posts/471/comments/30/edit" for 72.231.138.82 at 2016-07-17 01:25:27 +0000 
Processing by CommentsController#edit as JS 
    Parameters: {"post_id"=>"471", "id"=>"30"} 
    User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]] 
    RailsSettings::SettingObject Load (0.2ms) SELECT "settings".* FROM "settings" WHERE "settings"."target_id" = ? AND "settings"."target_type" = ? [["target_id", 2], ["target_type", "User"]] 
    Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? ORDER BY "posts"."created_at" DESC LIMIT 1 [["id", 471]] 
    Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."user_id" = ? AND "comments"."id" = ? LIMIT 1 [["user_id", 2], ["id", 30]] 
    Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT 1 [["id", 30]] 
    User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]] 
    Rendered comments/_comment_form.html.erb (1.2ms) 
    Rendered comments/edit.js.erb (13.8ms) 
Completed 200 OK in 102ms (Views: 34.4ms | ActiveRecord: 1.7ms) 

私は正確に発射されている(update_attributes方法を含む)と同様の作成、破棄、およびカスタムアクションを持っているので、私はこのフォームを送信することができない理由はわかりません。何か助けていただければ幸いです。私はとても新しいです。方法=>:効果なしに、入れて、タイプミスのためのおかげで@Emuをキャッチ私は編集フォームを更新しました

class CommentsController < ApplicationController 
    before_action :set_post 
    before_action :user_signed_in?, only: [:create, :destroy] 
    before_action :authenticate_user!, only: [:create, :edit, :new, :destroy, :update] 
    before_action :correct_user, only: [:edit, :update, :destroy] 
    ... 

    def edit 
    @comment = Comment.find(params[:id]) 
    respond_to do |format| 
     format.js { render 'comments/edit', locals: {comment: @comment, post: @post } } 
    end 
    end 

    def update 
    @comment = Comment.find(params[:id]) 
    respond_to do |format| 
     format.js 
    end 
    if @comment.update_attributes(comment_params) 
     @comment.toggle!(:edited) 
     flash[:success] = "Comment edited!" 
     redirect_to root_path 
    else 
     render 'edit' 
    end 
    end 

    private 

    def comment_params 
    params.require(:comment).permit(:content, :user_id, :post_id) 
    end 

    def set_post 
    @post = Post.find(params[:post_id]) 
    end 

    def correct_user 
    @comment = current_user.comments.find_by(id: params[:id]) 
    redirect_to root_url if @comment.nil? 
    redirect_to root_url if @comment.userlocked? 
    end 

end 

:コメントのコントローラアクションと

更新。

+0

を逃し、

また
<%= form_for([post,comment ], :remote=>true, :method => :put) do |f| %> 

することができますあなたのコメントコントローラを投稿する – mrvncaragay

+0

あなたのコントローラーで 'render js 'をする必要があります – uzaif

+0

@ Marv-C、それを追加しました。 @uzaif、コメントコントローラ 'render js 'がどのように/どこに追加されるのか少し説明できますか?これは初めてのajaxでフォームをレンダリングしようとしています。 –

答えて

0

あなたが編集フォームを提出しているとして、あなたは次のようにform方法を定義する必要があります。update.js.erbに次の行が"." or "#"

$('.comment-form').hide(); 
関連する問題