jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – link_to_remove_association没有删除?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我们如何修复nested_attribute:_result_fields.html.erb,以便用户点击“删除”时实际删除它?而现在点击它什么也没做.
<%= f.text_field :result_value,class: 'form-control',placeholder: 'Enter Result' %>
<%= f.date_SELEct :date_value,:order => [:month,:day,:year],:with_css_classes => true,:class => "date-SELEct" %>
<%= f.check_Box :bad %>
<%= link_to_remove_association f do %>
  delete
<% end %>

统计数据有很多结果

统计/ _form

<div id="results">
  <%= f.fields_for :results do |result| %>
  <%= render 'result_fields',:f => result %>
  <% end %>
</div>

  <span class="label label-priMary">
    <%= link_to_add_association f,:results do %>
    <span class="glyphicon glyphicon-plus"></span> Result
    <% end %>
  </span>

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

型号:

class Stat < ActiveRecord::Base
  has_many :results
  accepts_nested_attributes_for :results,:reject_if => :all_blank,:allow_destroy => true
end

class Result < ActiveRecord::Base
  belongs_to :stat
end

我正在使用cocoon gem.

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

按要求

class StatsController < ApplicationController
  before_action :set_stat,only: [:show,:edit,:update,:destroy,:like]
  before_action :logged_in_user,only: [:create,:destroy]
  before_action :correct_user,only: [:edit,:destroy]

  def index
    if params[:tag]
      @stats = Stat.tagged_with(params[:tag])
    else
      @stats = Stat.joins(:results).all
      @averaged_stats = current_user.stats.averaged
      @instance_stats = current_user.stats.instance
    end
  end

  def show
    @stat = Stat.find(params[:id])
    @commentable = @stat
    @comments = @commentable.comments
    @comment = Comment.new
    @notable = @stat
    @notes = @notable.notes
    @note = Note.new
    @correct_user = current_user.stats.find_by(id: params[:id])
  end

  def new
    @stat = current_user.stats.build 
  end

  def edit
  end

  def create
    @stat = current_user.stats.build(stat_params)
    if (params[:commit] == 'conceal')
      @stat.conceal = true
      @stat.save
      redirect_to @stat,notice: 'Stat was successfully created'
    elsif
      @stat.save
      track_activity @stat
      redirect_to @stat,notice: 'Stat was successfully created'
    else
      flash.Now[:danger] = 'required Fields: "Averaged or Instance","Enter Action","Enter Metric",and "+ Result"'
      render 'new'
    end
  end

  def update
    if @stat.update(stat_params)
      redirect_to stats_url,notice: 'Goal was successfully updated'
    else
      render action: 'edit'
  end
end

  def destroy
    @stat.destroy
    @result.destroy
    redirect_to stats_url
  end

  def like
    @stat = Stat.find(params[:id])
    @stat_like = current_user.stat_likes.build(stat: @stat)
    if @stat_like.save
      @stat.increment!(:likes)
      flash[:success] = 'Thanks for liking!'
    else
      flash[:error] = 'Two many likes'
    end  
      redirect_to(:BACk)
  end

  private
    def set_stat
      @stat = Stat.find(params[:id])
    end

    def correct_user
      @stat = current_user.stats.find_by(id: params[:id])
      redirect_to root_url,notice: "Not authorized to edit this stat" if @stat.nil?
    end

    def stat_params
      params.require(:stat).permit(:categories,:like,:action,:metric,:date,:comment,:private_submit,:tag_list,results_attributes: [:id,:result_value,:date_value,:bad,:_destroy])
    end
end

stat.js

$( document ).ready(function() {
  $('.date-format-switcher').click(function(event){
    event;
    if ($(this).attr('id') == 'stat_categories_instance') {
      $('.day').show();
     } else if ($(this).attr('id') == 'stat_categories_averaged') {
        $('.day').hide();
    }
})
  $('.add-form-padding').on('cocoon:after-insert',function(e,insertedItem) {
    if($('#stat_categories_instance').is(':checked')) {
      $('.day').show();
    } else {
        $('.day').hide();
    }
})
});

解决方法

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

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

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

大佬总结

以上是大佬教程为你收集整理的jquery – link_to_remove_association没有删除?全部内容,希望文章能够帮你解决jquery – link_to_remove_association没有删除?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。