jquery – link_to_remove_association没有删除?

前端之家收集整理的这篇文章主要介绍了jquery – link_to_remove_association没有删除?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们如何修复nested_attribute:_result_fields.html.erb,以便当用户点击“删除”时实际删除它?而现在点击它什么也没做.
  1. <%= f.text_field :result_value,class: 'form-control',placeholder: 'Enter Result' %>
  2. <%= f.date_select :date_value,:order => [:month,:day,:year],:with_css_classes => true,:class => "date-select" %>
  3. <%= f.check_Box :bad %>
  4. <%= link_to_remove_association f do %>
  5. Delete
  6. <% end %>

统计数据有很多结果

统计/ _form

  1. <div id="results">
  2. <%= f.fields_for :results do |result| %>
  3. <%= render 'result_fields',:f => result %>
  4. <% end %>
  5. </div>
  6.  
  7. <span class="label label-primary">
  8. <%= link_to_add_association f,:results do %>
  9. <span class="glyphicon glyphicon-plus"></span> Result
  10. <% end %>
  11. </span>

在stats_controller中我将它作为params:results_attributes:[:id,:result_value,:date_value,:bad,:_ destroy]

型号:

  1. class Stat < ActiveRecord::Base
  2. has_many :results
  3. accepts_nested_attributes_for :results,:reject_if => :all_blank,:allow_destroy => true
  4. end
  5.  
  6. class Result < ActiveRecord::Base
  7. belongs_to :stat
  8. end

我正在使用cocoon gem.

如果您需要进一步的代码或解释,请告诉我.谢谢!

按要求

  1. class StatsController < ApplicationController
  2. before_action :set_stat,only: [:show,:edit,:update,:destroy,:like]
  3. before_action :logged_in_user,only: [:create,:destroy]
  4. before_action :correct_user,only: [:edit,:destroy]
  5.  
  6. def index
  7. if params[:tag]
  8. @stats = Stat.tagged_with(params[:tag])
  9. else
  10. @stats = Stat.joins(:results).all
  11. @averaged_stats = current_user.stats.averaged
  12. @instance_stats = current_user.stats.instance
  13. end
  14. end
  15.  
  16. def show
  17. @stat = Stat.find(params[:id])
  18. @commentable = @stat
  19. @comments = @commentable.comments
  20. @comment = Comment.new
  21. @notable = @stat
  22. @notes = @notable.notes
  23. @note = Note.new
  24. @correct_user = current_user.stats.find_by(id: params[:id])
  25. end
  26.  
  27. def new
  28. @stat = current_user.stats.build
  29. end
  30.  
  31. def edit
  32. end
  33.  
  34. def create
  35. @stat = current_user.stats.build(stat_params)
  36. if (params[:commit] == 'conceal')
  37. @stat.conceal = true
  38. @stat.save
  39. redirect_to @stat,notice: 'Stat was successfully created'
  40. elsif
  41. @stat.save
  42. track_activity @stat
  43. redirect_to @stat,notice: 'Stat was successfully created'
  44. else
  45. flash.now[:danger] = 'required Fields: "Averaged or Instance","Enter Action","Enter Metric",and "+ Result"'
  46. render 'new'
  47. end
  48. end
  49.  
  50. def update
  51. if @stat.update(stat_params)
  52. redirect_to stats_url,notice: 'Goal was successfully updated'
  53. else
  54. render action: 'edit'
  55. end
  56. end
  57.  
  58. def destroy
  59. @stat.destroy
  60. @result.destroy
  61. redirect_to stats_url
  62. end
  63.  
  64. def like
  65. @stat = Stat.find(params[:id])
  66. @stat_like = current_user.stat_likes.build(stat: @stat)
  67. if @stat_like.save
  68. @stat.increment!(:likes)
  69. flash[:success] = 'Thanks for liking!'
  70. else
  71. flash[:error] = 'Two many likes'
  72. end
  73. redirect_to(:back)
  74. end
  75.  
  76. private
  77. def set_stat
  78. @stat = Stat.find(params[:id])
  79. end
  80.  
  81. def correct_user
  82. @stat = current_user.stats.find_by(id: params[:id])
  83. redirect_to root_url,notice: "Not authorized to edit this stat" if @stat.nil?
  84. end
  85.  
  86. def stat_params
  87. params.require(:stat).permit(:categories,:like,:action,:metric,:date,:comment,:private_submit,:tag_list,results_attributes: [:id,:result_value,:date_value,:bad,:_destroy])
  88. end
  89. end

stat.js

  1. $( document ).ready(function() {
  2. $('.date-format-switcher').click(function(event){
  3. event;
  4. if ($(this).attr('id') == 'stat_categories_instance') {
  5. $('.day').show();
  6. } else if ($(this).attr('id') == 'stat_categories_averaged') {
  7. $('.day').hide();
  8. }
  9. })
  10. $('.add-form-padding').on('cocoon:after-insert',function(e,insertedItem) {
  11. if($('#stat_categories_instance').is(':checked')) {
  12. $('.day').show();
  13. } else {
  14. $('.day').hide();
  15. }
  16. })
  17. });

解决方法

如果link_to_add_association有效,则正确加载javascript.

对于什么不起作用仍然有点困惑.如果单击link_to_remove_association不会从页面删除任何内容,则缺少正确的包装类.默认情况下,它应该是.nested-fields,但可以通过明确指定它来推翻(如documented).

但是如果项目被视觉删除,并且您认为它应该立即发送到服务器,那么您就会误解cocoon的工作原理:您编辑一个表单,该表单仅在您提交表单时保存(发送到服务器).因此link_to_remove_association只能看到删除嵌套子项,并编辑表单内容(设置_destroy标志),因此在保存/提交表单时,嵌套子项将被删除.

猜你在找的jQuery相关文章