# Jquery code is not executed

**URL:** <https://discuss.rubyonrails.org/t/jquery-code-is-not-executed/69204>\
**Category:** rubyonrails-talk\
**Created:** [June 29, 2014, 7:56am UTC](https://discuss.rubyonrails.org/t/jquery-code-is-not-executed/69204 "2014-06-29T07:56:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 29, 2014, 7:56am UTC](https://discuss.rubyonrails.org/t/jquery-code-is-not-executed/69204/1 "2014-06-29T07:56:32Z")

</div>

Hi,

Here is the

views/articles/show.html.erb file

\<%= render @article %\> \<h3\>Comments\</h3\> \<div id="comments"\> &nbsp;&nbsp;\<%= render @article.comments %\> \</div\> \<%= link\_to "new comment", new\_article\_comment\_path(@article, :format =\> :js), :remote =\> true, :id =\> 'new\_comment\_link' %\>

and views/comments/new.js.erb

$("\<%= escape\_javascript render(file: 'comments/new.html.erb') %\>").insertAfter('#comments'); $('#new\_comment').hide().slideDown(); $('#new\_comment\_link').hide();

When I am clicking on the \*new comment link\* from the url - [http://localhost:3000/articles/2/](http://localhost:3000/articles/2/) ... I am getting the Jquery code back as show in the url - [http://i.imgur.com/uRdWlsr.png](http://i.imgur.com/uRdWlsr.png)

You can check the same in Github also - [https://github.com/aruprakshit/Blog-app/tree/master/app/views](https://github.com/aruprakshit/Blog-app/tree/master/app/views).

Could you tell me why the Jquery is not being executed ?

---

<div class="post-metadata">

**Author:** ![Andre\_Orvalho1](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@Andre\_Orvalho1](https://discuss.rubyonrails.org/u/Andre_Orvalho1)\
**Post date:** [July 1, 2014, 11:28am UTC](https://discuss.rubyonrails.org/t/jquery-code-is-not-executed/69204/2 "2014-07-01T11:28:48Z")

</div>

Hows the controller rendering the response?

you might be missing a block like this on your controller action:

respond\_to do |format|

format.js do

end

end

---

<div class="post-metadata">

**Author:** ![Arup\_Rakshit](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@Arup\_Rakshit](https://discuss.rubyonrails.org/u/Arup_Rakshit)\
**Post date:** [July 5, 2014, 4:10am UTC](https://discuss.rubyonrails.org/t/jquery-code-is-not-executed/69204/3 "2014-07-05T04:10:19Z")

</div>

Here is the controller :-

class CommentsController \< ApplicationController &nbsp;&nbsp;before\_filter :load\_article, :except =\> :destroy &nbsp;&nbsp;before\_filter :authenticate, :only =\> :destroy &nbsp;&nbsp; &nbsp;&nbsp;def new &nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.js &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;def create &nbsp;&nbsp;&nbsp;&nbsp;@comment = @article.comments.new(comment\_params) &nbsp;&nbsp;&nbsp;&nbsp;if @comment.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to @article, notice: 'Thanks for your comment' &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to @article, alert: 'Unable to add comment' &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;def destroy &nbsp;&nbsp;&nbsp;&nbsp;@article = current\_user.articles.find(params[:article\_id]) &nbsp;&nbsp;&nbsp;&nbsp;@comment = @article.comments.find(params[:id]) &nbsp;&nbsp;&nbsp;&nbsp;@comment.destroy &nbsp;&nbsp;&nbsp;&nbsp;redirect\_to @article, notice: 'Comment Deleted' &nbsp;&nbsp;end

&nbsp;&nbsp;private

&nbsp;&nbsp;def load\_article &nbsp;&nbsp;&nbsp;&nbsp;@article = Article.find(params[:article\_id]) &nbsp;&nbsp;end

&nbsp;&nbsp;def comment\_params &nbsp;&nbsp;&nbsp;&nbsp;params.require(:comment).permit(:name, :email, :body) &nbsp;&nbsp;end end

---

<div class="post-metadata">

**Author:** ![Andre\_Orvalho1](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@Andre\_Orvalho1](https://discuss.rubyonrails.org/u/Andre_Orvalho1)\
**Post date:** [July 7, 2014, 7:41am UTC](https://discuss.rubyonrails.org/t/jquery-code-is-not-executed/69204/4 "2014-07-07T07:41:51Z")

</div>

Hey Arup,

what version of rails are you using?

Also have you read this:

> **[ActionController::RequestForgeryProtection](https://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection.html)**
>
> Controller actions are protected from Cross-Site Request Forgery (CSRF) attacks by including a token in the rendered HTML for your application.

try to google your error might help you.

all the best,

Andre
