Jquery code is not executed

45 views
Skip to first unread message

Arup Rakshit

unread,
Jun 29, 2014, 3:57:16 AM6/29/14
to rubyonra...@googlegroups.com
Hi,

Here is the

views/articles/show.html.erb file

<%= render @article %>
<h3>Comments</h3>
<div id="comments">
<%= 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/ ... I am getting the Jquery code back
as show in the url - http://i.imgur.com/uRdWlsr.png

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

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

--
Posted via http://www.ruby-forum.com/.

and...@benjamin.dk

unread,
Jul 1, 2014, 7:28:48 AM7/1/14
to rubyonra...@googlegroups.com
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

Arup Rakshit

unread,
Jul 5, 2014, 12:10:46 AM7/5/14
to rubyonra...@googlegroups.com
Here is the controller :-

class CommentsController < ApplicationController
before_filter :load_article, :except => :destroy
before_filter :authenticate, :only => :destroy

def new
respond_to do |format|
format.js
end
end

def create
@comment = @article.comments.new(comment_params)
if @comment.save
redirect_to @article, notice: 'Thanks for your comment'
else
redirect_to @article, alert: 'Unable to add comment'
end
end

def destroy
@article = current_user.articles.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to @article, notice: 'Comment Deleted'
end

private

def load_article
@article = Article.find(params[:article_id])
end

def comment_params
params.require(:comment).permit(:name, :email, :body)
end
end

---------------------
app/views/articles/show.html.erb
-------------------------

<%= render @article %>
<h3>Comments</h3>
<div id="comments">
<%= render @article.comments %>
</div>
<%= link_to "new comment", new_article_comment_path(@article, :format => :js),
:remote => true, :id => 'new_comment_link' %>

---------------
app/views/comments/new.js.erb

$("<%= escape_javascript render(file: 'comments/new.html.erb')
%>").insertAfter('#comments');
$('#new_comment_link').hide();

I am getting the below error :-

Started GET "/articles/1/comments/new.js" for 127.0.0.1 at 2014-07-05 09:22:20
+0530
Processing by CommentsController#new as JS
Parameters: {"article_id"=>"1"}
Article Load (0.2ms) SELECT "articles".* FROM "articles" WHERE
"articles"."id" = ? LIMIT 1 [["id", 1]]
Rendered comments/new.html.erb (35.1ms)
Rendered comments/new.js.erb (37.5ms)
Security warning: an embedded <script> tag on another site requested protected
JavaScript. If you know what you're doing, go ahead and disable forgery
protection on this action to permit cross-origin JavaScript embedding.
Completed 500 Internal Server Error in 51ms

ActionController::InvalidCrossOriginRequest (Security warning: an embedded
<script> tag on another site requested protected JavaScript. If you know what
you're doing, go ahead and disable forgery protection on this action to permit
cross-origin JavaScript embedding.):
actionpack (4.1.1)
lib/action_controller/metal/request_forgery_protection.rb:217:in
`verify_same_origin_request'


--
================
Regards,
Arup Rakshit
================
Debugging is twice as hard as writing the code in the first place. Therefore,
if you write the code as cleverly as possible, you are, by definition, not
smart enough to debug it.

--Brian Kernighan

André Orvalho

unread,
Jul 7, 2014, 3:42:17 AM7/7/14
to rubyonra...@googlegroups.com
Hey Arup,

what version of rails are you using?


try to google your error might help you.

all the best,

Andre
Reply all
Reply to author
Forward
0 new messages