0
votes

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

1
why you use redirect two times in action likeuzaif
oh i see thats my errorDeez Nuuts
i dont have error now but the alert not working?Deez Nuuts
uzaif can i feed you back?Deez Nuuts
@GeromeJohnPangan, Inspect the network tab and see if the XHR was successful.Arun Kumar Mohan

1 Answers

3
votes

In a controller action, neither render nor redirect_to stops the execution of the action. That is why you're getting a DoubleRenderError. Remove the first redirect_to and everything should be fine.

def like
  @user = current_user
  @post.liked_by(@user) # I am unsure what you're trying to accomplish here
  respond_to do |format|
    format.html { redirect_to posts_path }
    format.js { render :stablelike }
  end
end

If this doesn't work, probably you have not setup stablelike action to respond to xhr requests. You can do that by

def stablelike
  respond_to  do |format|
    # other formats
    format.js # by default looks for `stablelike.js.erb`
  end
end