11155
(-- --)
June 29, 2014, 7:56am
1
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 ?
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
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
Hey Arup,
what version of rails are you using?
Also have you read this:
try to google your error might help you.
all the best,
Andre