2016-07-17 3 views
1

게시글에 대한 의견을 업데이트하려고하는데 부분 편집 양식을 올바르게 렌더링 할 수는 있지만 저장하려고 시도하면 콘솔 오류가 발생하지 않습니다. 오류가 서버 로그에 기록되고 레코드가 저장되지 않습니다.레일 4 아약스 편집 폼이 발사되지 않음

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> 

update.js.erb

$('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 

아래는 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) 

I이 유사한 파괴하고 제대로 발사된다 (AN update_attributes 방법 포함) 사용자 지정 작업, 그래서 나는이 양식을 제출할 수없는 이유를 모르겠어요 만듭니다. 어떤 도움을 주시면 감사하겠습니다. 주석 컨트롤러 액션과

업데이트 : 효과 없음에, PUT 및 오타 캐치 @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

을 누락 update.js.erb에 또한

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

, 수 당신은 당신의 컨트롤러에 – mrvncaragay

+0

을 게시해야합니다. 컨트롤러에'render js '가 있어야합니다. – uzaif

+0

@ Marv-C, 방금 추가했습니다. @uzaif, 설명 컨트롤러에서'render js '가 어떻게 추가 될지 조금 더 설명해 주시겠습니까? 이것은 아약스로 양식을 렌더링하려고 시도한 첫 번째 사례입니다. –

답변

0

, 당신의 방법을 정의해야 form 같은 : 다음 줄은 "." or "#"

$('.comment-form').hide(); 
관련 문제