问题描述
我有dailyposts及功放的多态关联;在我的Rails应用程序的意见
一切工作正常,在数据库中,但是当我尝试阿贾克斯添加到销毁行动(我用这个教程的)....,除非我刷新页面无法正常工作。 (但在destroy.js.erb我的提醒工作)
我知道我的错误是在destroy.js.erb
新到Rails ...请帮助:)
这是我的code ... 的
路线
资源:dailyposts做
资源:评论
结束
控制器
## Dailyposts
类DailypostsController<的ApplicationController
respond_to代码:HTML,:JS
高清节目
@user = User.find_by_username(PARAMS [:用户名])
@dailypost = Dailypost.find_by_id(PARAMS [:ID])
@commentable = @dailypost
@comments = @ commentable.comments.arrange(:为了=>:created_at)
@comment = Comment.new
结束
结束
##评论
类CommentsController<的ApplicationController
的before_filter:load_commentable
respond_to代码:HTML,:JS
DEF创建
@comment = @ commentable.comments.create(PARAMS [:评论])
@ comment.user = CURRENT_USER
如果@ comment.save
respond_to代码做|格式|
的format.html {redirect_to时@commentable}
format.js
结束
其他
redirect_to时@commentable,声明:注释不能为空。
结束
结束
DEF破坏
@comment = Comment.find(PARAMS [:ID])
@commentable = @ comment.commentable
如果@ comment.destroy
respond_to代码做|格式|
的format.html {redirect_to时@commentable}
format.js
结束
结束
结束
私人
高清load_commentable
资源,ID = request.path.split('/')[1,2]
@commentable = resource.singularize.classify.constantize.find(id)的
结束
结束
VIEWS
Dailypost show.html.erb
< DIV CLASS =排液>
< DIV CLASS =span12>
< DIV>
<%=原(dailypost_with_links(@dailypost))%>
< / DIV>
<%=渲染评论/表%>
< DIV ID =意见>
&其中;%= nested_comments @comments%GT;
< / DIV>
< / DIV>
< / DIV>
_comment.html.erb
<节类=意见>
< DIV CLASS =用户>
&其中;%=的link_to comment.user.username,comment.user%GT;
&其中;%= comment.content%GT;
< / DIV>
##在这里,我通过远程:适用于阿贾克斯
<%如果CURRENT_USER(comment.user)%&GT?;
<%=的link_to content_tag(:我,类图标垃圾桶图标),[@commentable,注释],方法:删除,
数据:{确认:你确定吗? },
标题:删除,遥控器:真%> |
<%结束%GT;
< /节>
destroy.js.erb
##警报工作
警报(Ajax的工作原理!');
$('#<%= dom_id(@comment)%>')。remove()方法;
日志
开始删除/ dailyposts / 11 /评论/ 133为127.0.0.1,在2013年8月4日23时〇六分31秒-0700
处理由CommentsController#破坏的JS
参数:{dailypost_id=>中11,标识=>中133}
Dailypost负载(0.3ms)选择dailyposts。* FROMdailypostsWHEREdailyposts。ID=? ORDER BY dailyposts.created_at DESC LIMIT 1 [ID,11]
用户负载(0.3ms)选择用户。*从用户WHERE的用户。remember_token='sgFH2XeZWEXCcjxiAwgfXg限制1
评论加载(为0.2ms)选择注释。* FROM意见WHERE意见。ID=? LIMIT 1 [ID,133]
Dailypost负荷(0.1毫秒)选择dailyposts。* FROMdailypostsWHEREdailyposts。ID= 11 ORDER BY dailyposts.created_at DESC LIMIT 1
(0.1毫秒)开始交易
评论加载(0.1毫秒)选择注释。* FROM意见WHERE(comments.ancestry像'133 /%'或comments.ancestry ='133')
SQL(0.4ms)DELETE FROM意见WHERE意见。ID=? [ID,133]
(2.3ms)提交事务
渲染评论/ destroy.js.erb(为0.7ms)
完成200 OK的25ms的(浏览次数:10.3ms | ActiveRecord的:3.9ms)
试试这个:
Dailypost show.html.erb
< DIV ID =意见>
&其中;%=渲染@comments%GT;
< / DIV>
_comment.html.erb
< DIV ID =<%= dom_id(评论)%>>
&其中;%= comment.content%GT;
< / DIV>
create.js.erb
$('<%= escape_javascript(渲染(:部分=> @comment))%>')。appendTo('#评论)隐藏()。淡入();
destroy.js.erb
$('#<%= dom_id(@comment)%>')删除()。
I have a polymorphic association with dailyposts & comments in my rails app
Everything works fine in the database, but when I try to add Ajax to the Destroy Action (I used this tutorial http://railscasts.com/episodes/136-jquery-ajax-revised)....it doesn't work unless I refresh the page. (but my alert works in destroy.js.erb)
I know my error is in destroy.js.erb
New to Rails...Please help :)
This is my code...
ROUTES
resources :dailyposts do
resources :comments
end
CONTROLLERS
##Dailyposts
class DailypostsController < ApplicationController
respond_to :html, :js
def show
@user = User.find_by_username(params[:username])
@dailypost = Dailypost.find_by_id(params[:id])
@commentable = @dailypost
@comments = @commentable.comments.arrange(:order => :created_at)
@comment = Comment.new
end
end
##Comments
class CommentsController < ApplicationController
before_filter :load_commentable
respond_to :html, :js
def create
@comment = @commentable.comments.create(params[:comment])
@comment.user = current_user
if @comment.save
respond_to do |format|
format.html { redirect_to @commentable }
format.js
end
else
redirect_to @commentable, notice: "Comment can't be blank."
end
end
def destroy
@comment = Comment.find(params[:id])
@commentable = @comment.commentable
if @comment.destroy
respond_to do |format|
format.html { redirect_to @commentable }
format.js
end
end
end
private
def load_commentable
resource, id = request.path.split('/')[1, 2]
@commentable = resource.singularize.classify.constantize.find(id)
end
end
VIEWS
Dailypost show.html.erb
<div class="row-fluid">
<div class="span12">
<div>
<%= raw(dailypost_with_links(@dailypost)) %>
</div>
<%= render "comments/form" %>
<div id="comments">
<%= nested_comments @comments %>
</div>
</div>
</div>
_comment.html.erb
<section class="comments">
<div class ="user">
<%= link_to comment.user.username, comment.user %>
<%= comment.content %>
</div>
##Here I am passing remote: true for ajax
<% if current_user?(comment.user) %>
<%= link_to content_tag(:i, "", class: "icon-trash icons"), [@commentable, comment], method: :delete,
data: { confirm: "Are you sure?" },
title: "Delete", remote: true %> |
<% end %>
</section>
destroy.js.erb
##alert is working
alert('ajax works!');
$('#<%= dom_id(@comment) %>').remove();
LOGS
Started DELETE "/dailyposts/11/comments/133" for 127.0.0.1 at 2013-08-04 23:06:31 -0700
Processing by CommentsController#destroy as JS
Parameters: {"dailypost_id"=>"11", "id"=>"133"}
Dailypost Load (0.3ms) SELECT "dailyposts".* FROM "dailyposts" WHERE "dailyposts"."id" = ? ORDER BY dailyposts.created_at DESC LIMIT 1 [["id", "11"]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'sgFH2XeZWEXCcjxiAwgfXg' LIMIT 1
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT 1 [["id", "133"]]
Dailypost Load (0.1ms) SELECT "dailyposts".* FROM "dailyposts" WHERE "dailyposts"."id" = 11 ORDER BY dailyposts.created_at DESC LIMIT 1
(0.1ms) begin transaction
Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE (comments.ancestry like '133/%' or comments.ancestry = '133')
SQL (0.4ms) DELETE FROM "comments" WHERE "comments"."id" = ? [["id", 133]]
(2.3ms) commit transaction
Rendered comments/destroy.js.erb (0.7ms)
Completed 200 OK in 25ms (Views: 10.3ms | ActiveRecord: 3.9ms)
Try this:
Dailypost show.html.erb
<div id="comments">
<%= render @comments %>
</div>
_comment.html.erb
<div id="<%= dom_id(comment) %>">
<%= comment.content %>
</div>
create.js.erb
$('<%= escape_javascript(render(:partial => @comment))%>').appendTo('#comments').hide().fadeIn();
destroy.js.erb
$('#<%= dom_id(@comment) %>').remove();
这篇关于Ajax和多态协会(销毁和放大器;创建)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!