Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 链接到删除关联未删除?_Jquery_Ruby On Rails_Ruby_Cocoon Gem - Fatal编程技术网

Jquery 链接到删除关联未删除?

Jquery 链接到删除关联未删除?,jquery,ruby-on-rails,ruby,cocoon-gem,Jquery,Ruby On Rails,Ruby,Cocoon Gem,我们如何修复嵌套的属性:\u result\u 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 =

我们如何修复嵌套的属性:\u result\u 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 %>
stats\u controller中,我将其作为参数:
results\u属性:[:id,:result\u值,:date\u值,:bad,:\u 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

可能问题在于,您没有包括与gem一起出现的javascript,以便按预期调用链接,您是使用rubyonrails3还是使用rubyonrails4?在rails中提问时,这一点非常重要,因为它们有很多不同之处

检查位于
app/assets/application.js
中的
application.js
文件,确保添加了以下行:

//= require cocoon
如果您已经添加了资产,请检查页面的源代码,并确认为cocoon定义的javascript正在页面中加载


如果已经这样做了,请更新问题并添加您在单击链接时看到的rails日志,同时让我知道您正在使用哪个版本的rails。如果
链接到添加关联
有效,则javascript加载正确

对什么不起作用仍然有点困惑。如果单击
链接到\u remove\u关联
没有从页面中明显删除任何内容,则您缺少正确的包装类。默认情况下,它应该是
。嵌套字段
,但可以通过显式指定它(如所示)来推翻它


但是,如果项目被直观地删除,并且您认为应该立即将其发送到服务器,那么您就误解了cocoon的工作原理:您编辑了一个表单,该表单仅在提交表单时保存(发送到服务器)。因此,
link\u to\u remove\u association
仅可见将删除嵌套的子项,并编辑表单的内容(设置
\u destroy
标志),因此在保存/提交表单时,嵌套的子项将被删除

可能添加控制器并检查日志,以查看请求是否按预期命中rails。控制器上的代码是什么?添加了控制器代码@rorra:-]添加了控制器代码@maxcal,当我单击“删除”时没有发出任何请求,终端没有更改,什么也没有发生。然后问题出现在客户端,浏览器控制台中有任何有用的信息吗?谢谢Rorra<代码>/=require cocoon在那里。当我点击链接时,没有rails日志。什么也没发生。可能是我添加的javascript造成了问题?我把它添加到问题中供你参考。再次感谢:-]@AnthonyGalli.com根据您的描述,我认为问题在于客户端的javascript,因为服务器上没有请求。cocoon在点击链接时会做什么,它会隐藏当前选项,并且在后台有一个名为_destroy的隐藏检查字段,该字段被标记(如果该字段已经存在于数据库中,如果该字段刚刚添加,那么div就被隐藏)。你能告诉我这个字段是否已经在数据库中持久化了,如果你点击链接,它会隐藏div和/或标记隐藏的字段吗?另外,请检查浏览器的javascript控制台,确保没有任何js异常,最后,删除除cocoon js之外的所有js,只是为了再次检查正在加载的其他js是否与cocoon发生冲突,尽管很难创建导致cocoon冲突的自定义代码,但很可能有其他js代码导致错误,并避免cocoon加载/执行其代码。对于未来的读者,请确保将
/=require cococoon
放在application.js中的
/=require jquery
之后,否则它将无法工作。我遇到了类似的问题,因为我忘记了我的部分中的
.nested fields
div非常感谢Nathanvda!我把“嵌套”放在顶部而不是“嵌套字段”=]哦,忘了-我爱你!哈哈!我还忘了在我的部分中使用.nestedfields div!StackOverFlow永不失败\o/
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, :update, :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
$( 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();
    }
})
});
//= require cocoon