Rails 3.1 Jquery Ajax CRUD 操作
我正在开发此处列出的 Rails 3.1 Jquery Ajax 演示应用程序 https://github.com/samnang/ajax_rails31_demo 。我正在尝试添加代码来删除评论。我修改了 comments_controller.rb 并添加了销毁操作。我还添加了一个指向 app/views/_comment.html.erb 的链接以包含“销毁”链接。我能够删除评论,并且评论计数减 1,但评论部分不会更新删除条目。只有刷新页面后页面才能正确显示。这是我的代码
app/controllers/comments_controller.rb
class CommentsController < ApplicationController
respond_to :html, :js
def index
@comments = Comment.all
end
def create
@comment = Comment.new(params[:comment])
@comment.save
respond_with @comment, :location => comments_url
end
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
respond_with @comment, :location => comments_url
end
end
app/views/comments/index.html.erb
<% title "Comments for Ajax in Rails 3.1" %>
<div id="comments_count"><%= comments_count %></div>
<div id="comments">
<%= render @comments %>
</div>
<h3>Add your comment:</h3>
<%= form_for Comment.new, :remote => true do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :content, "Comment" %><br />
<%= f.text_area :content, :rows => '12', :cols => 35 %>
</p>
<p><%= f.submit %></p>
<% end %>
app/views/comments/_comment.html.erb
<div class="comment">
<strong><%= comment.name %></strong>
<em><%= comment.created_at.strftime('%b %d, %Y at %I:%M %p') %></em>
<%= simple_format comment.content %>
<%= link_to "Destroy", comment,:remote => true, :confirm => "You Sure", :method => :delete %>
</div>
app/vews/comments/create.js.coffee
$('<%= escape_javascript(render(:partial => @comment))%>')
.appendTo('#comments')
.hide()
.fadeIn()
$('#new_comment')[0].reset()
$('#comments_count').html '<%= comments_count %>'
app/views/comments/ destroy.js.coffee
$('#new_comment')[0].reset()
$('#comments_count').html '<%= comments_count %>'
destroy jquery 部分是我认为我搞砸的地方。如何重新加载部分以删除已删除的评论?
I am working on Rails 3.1 Jquery Ajax demo app listed here https://github.com/samnang/ajax_rails31_demo. I am trying to add code to delete comments. I have modified comments_controller.rb and added a destroy action. I also added a link to the app/views/_comment.html.erb to include a Destroy link. I am able to get the comment to delete and the comment count is decremented by 1, but the comment partial does not update removing the entry. Only upon page refresh does the page display correctly. Here is my code
app/controllers/comments_controller.rb
class CommentsController < ApplicationController
respond_to :html, :js
def index
@comments = Comment.all
end
def create
@comment = Comment.new(params[:comment])
@comment.save
respond_with @comment, :location => comments_url
end
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
respond_with @comment, :location => comments_url
end
end
app/views/comments/index.html.erb
<% title "Comments for Ajax in Rails 3.1" %>
<div id="comments_count"><%= comments_count %></div>
<div id="comments">
<%= render @comments %>
</div>
<h3>Add your comment:</h3>
<%= form_for Comment.new, :remote => true do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :content, "Comment" %><br />
<%= f.text_area :content, :rows => '12', :cols => 35 %>
</p>
<p><%= f.submit %></p>
<% end %>
app/views/comments/_comment.html.erb
<div class="comment">
<strong><%= comment.name %></strong>
<em><%= comment.created_at.strftime('%b %d, %Y at %I:%M %p') %></em>
<%= simple_format comment.content %>
<%= link_to "Destroy", comment,:remote => true, :confirm => "You Sure", :method => :delete %>
</div>
app/vews/comments/create.js.coffee
$('<%= escape_javascript(render(:partial => @comment))%>')
.appendTo('#comments')
.hide()
.fadeIn()
$('#new_comment')[0].reset()
$('#comments_count').html '<%= comments_count %>'
app/views/comments/destroy.js.coffee
$('#new_comment')[0].reset()
$('#comments_count').html '<%= comments_count %>'
The destroy jquery portion is where I believe I am messing up. How do I reload the partial to remove the deleted comment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经更新了 Github 上的示例以支持销毁操作。在您的示例中,您没有删除 destroy.js.coffee 中的 dom 元素。再次查看 Github 上的完整代码或查看以下代码片段:
app/views/comments/_comment.html。 erb
应用程序/views/comments/destroy.js.coffee
I have updated my example on Github to support destroy action. In your example you didn't remove dom element in your destroy.js.coffee. Check out full code on Github again or look at below snippets:
app/views/comments/_comment.html.erb
app/views/comments/destroy.js.coffee