Rails 3.1 Jquery Ajax CRUD 操作

发布于 2024-12-06 16:40:03 字数 2449 浏览 6 评论 0原文

我正在开发此处列出的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期以后 2024-12-13 16:40:03

我已经更新了 Github 上的示例以支持销毁操作。在您的示例中,您没有删除 destroy.js.coffee 中的 dom 元素。再次查看 Github 上的完整代码或查看以下代码片段:

app/views/comments/_comment.html。 erb

<div id=<%= dom_id(comment) %> class="comment">
  <strong><%= comment.name %></strong>
  <em>on <%= comment.created_at.strftime('%b %d, %Y at %I:%M %p') %></em>
  <%= link_to "Remove", comment, :method => :delete, :remote => true %>
  <%= simple_format comment.content %>
</div>

应用程序/views/comments/destroy.js.coffee

$('#comments_count').html '<%= comments_count %>'

$('#<%= dom_id(@comment) %>')
  .fadeOut ->
    $(this).remove()

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

<div id=<%= dom_id(comment) %> class="comment">
  <strong><%= comment.name %></strong>
  <em>on <%= comment.created_at.strftime('%b %d, %Y at %I:%M %p') %></em>
  <%= link_to "Remove", comment, :method => :delete, :remote => true %>
  <%= simple_format comment.content %>
</div>

app/views/comments/destroy.js.coffee

$('#comments_count').html '<%= comments_count %>'

$('#<%= dom_id(@comment) %>')
  .fadeOut ->
    $(this).remove()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文