【问题标题】:Null values in deselect checkbox Rails取消选择复选框Rails中的空值
【发布时间】:2014-06-12 12:26:36
【问题描述】:

在我的应用程序中,如果用户必须取消选中他通知的所有复选框,请保存或null Array []

我在启动我的 Action UPDATE 后使用 params[: parameter1] [: parametro2_ids] | | = [],但是如果我尝试清除日志中的所有轨道,则返回:

NoMethodError (undefined method '[]' for nil: NilClass):。在正常逻辑中,它应该是功能性的。一个功能不勾选或者不勾选都一文不值怎么实现?

responsabilities_controller

class ResponsabilitiesController < ApplicationController
  def index
    @responsabilities = Responsability.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @responsabilities }
    end
  end

  def show
    @responsability = Responsability.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @responsability }
    end
  end


  def new
    @responsability = Responsability.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @responsability }
    end
  end


  def edit
    @responsability = Responsability.find(params[:id])
  end


  def create

    @responsability = Responsability.new(params[:responsability])

    respond_to do |format|
      if @responsability.save

        format.html {
          flash[:notice] = l(:notice_success)
          redirect_to :contoller => "uc_rh",:action => "index"
        }
      else
        format.html { render action: "new" }
        format.json { render json: @responsability.errors, status: :unprocessable_entity }
      end
    end
  end


  def update

      params[:responsability][:knowledge_ids] || [] if params[:responsability].present?
      params[:responsability][:competence_ids] || [] if params[:responsability].present?
      params[:responsability][:tool_ids] || [] if params[:responsability].present?

    @responsability = Responsability.find(params[:id])

    respond_to do |format|
      if @responsability.update_attributes(params[:responsability])

        format.html {
          flash[:notice] = l(:notice_success_edit)
          redirect_to :contoller => "responsabilities",:action => "edit"
        }
      else
        format.html { render action: "edit" }
        format.json { render json: @responsability.errors, status: :unprocessable_entity }
      end
    end
  end


  def destroy

    @responsability = Responsability.find(params[:id])
    @responsability.destroy

    respond_to do |format|
      format.html { redirect_to responsabilities_url }
      format.json { head :no_content }
    end
  end


  def nested_knowledges
    @responsability = Responsability.find(params[:id])

  end


  def nested_competences
    @responsability = Responsability.find(params[:id])
  end


  def nested_tools
    @responsability = Responsability.find(params[:id])
  end


end

【问题讨论】:

    标签: ruby-on-rails metaprogramming redmine


    【解决方案1】:

    尝试使用此代码

    params[:parameter1][:parametro2_ids] ||= [] if params[:parameter1].present?
    

    【讨论】:

    • ,感谢您的考虑,但我应用的控制器中的代码没有改变。不断弹出该错误...我可以使用某种方法来验证它们是否为空并从表中删除记录?
    • 使用 params[:responsability][:knowledge_ids] ||= [] if params[:responsability].present?现在代码返回 params[:parameter1][:parametro2_ids] 或 []
    • 你也可以尝试在方法的开头设置 return false 除非 params[:parameter1].present?
    • NoMethodError (undefined method '[]' for nil: NilClass) for params[:parameter1][:parametro2_ids] ||= [] 表示:你有 nil params[:parameter1] 并调用方法 [ :parametro2_ids] 用于 nil 类
    猜你喜欢
    • 2015-01-14
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多