Javascript Rails-在多态关联上处理Ajax destroy调用
我试图通过Ajax破坏一条评论。在我的应用程序中,我的评论模型使用多态关联。注释使用以下代码成功删除(在数据库中)。但是,当我调用destroy.js.erb时,它没有做任何事情,我认为问题在于dom_id与HTML id不匹配,因此它没有更新。我想我正在经历同样的事情,@mu_在文章中表达得太短了。我需要帮助解决这个问题。我不知道该解决方案是否涉及a)以某种方式将局部变量注释传递给destory.js.erb或b)另一个解决方案 routes.rbJavascript Rails-在多态关联上处理Ajax destroy调用,javascript,ruby-on-rails,ajax,ruby-on-rails-3,polymorphic-associations,Javascript,Ruby On Rails,Ajax,Ruby On Rails 3,Polymorphic Associations,我试图通过Ajax破坏一条评论。在我的应用程序中,我的评论模型使用多态关联。注释使用以下代码成功删除(在数据库中)。但是,当我调用destroy.js.erb时,它没有做任何事情,我认为问题在于dom_id与HTML id不匹配,因此它没有更新。我想我正在经历同样的事情,@mu_在文章中表达得太短了。我需要帮助解决这个问题。我不知道该解决方案是否涉及a)以某种方式将局部变量注释传递给destory.js.erb或b)另一个解决方案 routes.rb resources :feeds do
resources :feeds do
resources :comments
end
$('#<%= dom_id(@comment) %>')
.fadeOut ->
$(this).remove()
<div id=<%= dom_id(comment) %> class="comment">
<em>on <%= comment.created_at.strftime('%b %d, %Y at %I:%M %p') %></em>
<%= link_to "Remove", [@commentable, comment], :method => :delete, :remote => true %>
<%= simple_format comment.content %>
</div>
<div id="comments">
<%= render @comments %>
</div>
destroy.js.erb
resources :feeds do
resources :comments
end
$('#<%= dom_id(@comment) %>')
.fadeOut ->
$(this).remove()
<div id=<%= dom_id(comment) %> class="comment">
<em>on <%= comment.created_at.strftime('%b %d, %Y at %I:%M %p') %></em>
<%= link_to "Remove", [@commentable, comment], :method => :delete, :remote => true %>
<%= simple_format comment.content %>
</div>
<div id="comments">
<%= render @comments %>
</div>
$(“#”)
.淡出->
$(this.remove())
\u comment.html.erb
resources :feeds do
resources :comments
end
$('#<%= dom_id(@comment) %>')
.fadeOut ->
$(this).remove()
<div id=<%= dom_id(comment) %> class="comment">
<em>on <%= comment.created_at.strftime('%b %d, %Y at %I:%M %p') %></em>
<%= link_to "Remove", [@commentable, comment], :method => :delete, :remote => true %>
<%= simple_format comment.content %>
</div>
<div id="comments">
<%= render @comments %>
</div>
在…上
:delete,:remote=>true%>
feed/show.html.erb
resources :feeds do
resources :comments
end
$('#<%= dom_id(@comment) %>')
.fadeOut ->
$(this).remove()
<div id=<%= dom_id(comment) %> class="comment">
<em>on <%= comment.created_at.strftime('%b %d, %Y at %I:%M %p') %></em>
<%= link_to "Remove", [@commentable, comment], :method => :delete, :remote => true %>
<%= simple_format comment.content %>
</div>
<div id="comments">
<%= render @comments %>
</div>
评论控制器
class CommentsController < ApplicationController
def create
@comment = @commentable.comments.new(params[:comment])
if @comment.save
respond_to do |format|
format.html { redirect_to @commentable }
format.js
end
else
render :new
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
end
class FeedsController < ApplicationController
def show
@feed = Feed.find(params[:id])
@commentable = @feed
@comments = @commentable.comments
@comment = Comment.new
end
end
class CommentsController
为控制器馈电
class CommentsController < ApplicationController
def create
@comment = @commentable.comments.new(params[:comment])
if @comment.save
respond_to do |format|
format.html { redirect_to @commentable }
format.js
end
else
render :new
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
end
class FeedsController < ApplicationController
def show
@feed = Feed.find(params[:id])
@commentable = @feed
@comments = @commentable.comments
@comment = Comment.new
end
end
class FeedsController
从外观上看,@comment将在您访问destroy.js.erb时包含一条注释,因为您在此之前在控制器中设置了它,所以我认为它与引用的答案没有任何关系。我想知道这是否是由于您的id缺少引号造成的:
。。。我怀疑,如果您更正此错误以及任何其他相关的HTML验证错误,它将开始工作。谢谢。是的,这是一个问题。我还意识到我在erb文件中混合了咖啡脚本。一旦我解决了这两个问题,它就奏效了。谢谢你的帮助。@diasks2你能帮我吗:)