mardi 23 août 2016

Rendering js.erb in rails

i want to try my js.erb if it is working?

here my view posts/index to trigger

 <%= link_to "Like", {:controller => 'post', :id => post , :action =>'like'}, class: "btn btn-danger btn-xs", remote: true %>

my post controller

def like
@user = current_user
@post.liked_by(@user)
redirect_to posts_path
respond_to do |format|
  format.html {redirect_to posts_path}
  format.js { render :action => 'stablelike' }

end
end

and this is my js.erb to test

alert("working");

and this is my error: when i inspect my Chrome

AbstractController::DoubleRenderError at /post/like/85

Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".

app/controllers/posts_controller.rb, line 83

 ``` ruby
 78       @user = current_user
 79       @post.liked_by(@user)
 80       redirect_to posts_path
 81       respond_to do |format|
 82         format.html {redirect_to posts_path}

83 format.js { render :action => 'stablelike' } 84
85 end 86 end 87

Aucun commentaire:

Enregistrer un commentaire