2016-12-05 5 views
1

私は自分のアプリケーションの投稿を好きなようにボタンを作成しましたが、ページを更新せずにカウントを+1して "ハース"ボタンを押すか、ない。私は、次のコードを試してみましたが、私はこのエラーを持っている:ここではRailsとajax:act_as_votableの振る舞い

jquery.self-bd7ddd3….js?body=1:10255 PUT http://localhost:3000/posts/12/like 500 (Internal Server Error) 

は私の_posts.html.erbファイルです:

ここ
<div class="vote-rv"> 
      <% if current_user.liked? post %> 
      <%= link_to "", dislike_post_path(post), class: "dislike fa fa-heart", method: :put, remote: true %> 
      <% else %> 
      <%= link_to "", like_post_path(post), class: "like fa fa-heart-o", method: :put, remote: true %> 
      <% end %> 
      <span class="badge" data-id="<%= post.id %>"><%= post.get_likes.size %></span> 
      </div> 

ここ

resources :travels, :shallow => true do 
     # Posts 
     resources :posts do 
      member do 
       put "like" => "posts#like" 
       put "dislike" => "posts#dislike" 
      end 
     end 
    end 

私のroutes.rbをあります私のposts_controller.rbです(好き嫌いの方法)

def like 
     @post.like_from current_user 
     respond_to do |format| 
      format.html { redirect_to :back } 
      format.json { head :no_content } 
      format.js { render :layout => false } 
     end 
    end 

    def dislike 
     @post.dislike_by current_user 
     respond_to do |format| 
      format.html { redirect_to :back } 
      format.json { head :no_content } 
      format.js { render :layout => false } 
     end 
    end 

何が問題なの?

ありがとうございました!

+1

ですが、あなたのjs.erbファイルはどこですか(ajaxリクエストへの応答)ですか?あなたの投稿のビューにfilder –

+0

@ J.Dのlike.js.erbとdislike.js.erbファイルがあるはずです。私は何かが不足していると確信しましたが、これらのファイルに何を入れなければなりませんか? –

答えて

0

のファイルを1つ(like.js.erbdislike.js.erbという2つ)含む必要があります。これは、コントローラのlikeメソッドとdislikeメソッドの両方に応答します。

以下は、like.js.erb ajaxファイルの例です。これはあなたの_posts.html.erbファイルと同じフォルダに、ビュー/記事の下に行く:dislike.js.erbファイルについては

$('.like').bind('ajax:success', function() { 
    $('<%= post.id %>').html('<%= escape_javascript post.get_likes.size %>'); 
    $('#like_<%= post.id %>').removeClass('fa-heart-o').addClass('fa-heart'); 
}); 

、あなたは同様のことを行いますが、周りのクラス名を切り替えて、post.get_dislikes.sizepost.get_likes.sizeを変更し、追加する必要がありますあなたの意見でその行あなたが実際に嫌悪数

あなたはまた、心臓のAjaxの変更が正常に動作するようにのようなビューにIDを追加する必要が見たい場合:

<%= link_to "", like_post_path(post), id: "like_<%= post.id %>", class: "like fa fa-heart-o", method: :put, remote: true %> 

チャンあなたの好きな方法と嫌いな方法は:

@post.liked_by current_user 
respond_to do |format| 
    format.html { redirect_to :back } 
    format.js 
end 

@post.disliked_by current_user 
respond_to do |format| 
    format.html { redirect_to :back } 
    format.js 
end 
+0

_posts.html.erbが旅行フォルダにある場合はどうなりますか?私は両方のjsファイルを_post.html.erbファイルがあるtravalフォルダまたは投稿フォルダに入れますか? –

+0

jsファイルを '_posts.html.erb'と同じフォルダに入れます。 –

+0

私はあなたの先例コードを試していますが、既にページを更新して変形を見る必要があります。このエラーはまだあります:' UT http:// localhost:3000/posts/14/dislike 500(内部サーバーエラー) ' –