【问题标题】:Rails 4: How do I handle a submitted form where nothing was selected?Rails 4:如何处理未选择任何内容的提交表单?
【发布时间】:2013-07-19 16:45:24
【问题描述】:

对不起,如果标题有点混乱。我有一个Item 的表格,字段为name。有一个文本字段,用户可以在其中输入名称并提交。但是如果用户没有输入任何内容并点击提交,Rails 会给我一个param not found: item 错误,我不知道谁来解决这个问题。

items_controller.rb

def new
  @item = Item.new()

  respond_to do |format|
    format.html
    format.json { render json: @item }
  end
end

def create
  @item = Item.new(item_params)

  respond_to do |format|
    if @item.save
      format.html { redirect_to items_path }
      format.json { render json: @item, status: :created, location: @item }
    else
      format.html { render action: 'new', :notice => "Input a name." }
      format.json { render json: @item.errors, status: :unprocessable_entity }
    end
  end
end

private

def item_params
  params.require(:item).permit(:name)
end

app/views/items/new.html.haml

= form_for @item do |f|
  = f.label :name
  = f.text_field :name
  = f.submit "Submit"

params.require(:item) 部分是导致错误的原因。当 params[:item] 不存在时处理错误的约定是什么?

【问题讨论】:

  • 你应该不会得到这个错误,当你看到日志时发布的参数是什么?

标签: ruby-on-rails ruby-on-rails-4


【解决方案1】:

答案已经晚了,但我仍然会为其他人写。如rails guides 中所述,您需要在强参数中使用 fetch 而不是 require ,如果没有作为输入传递,通过使用 fetch 您可以提供默认值。比如:

params.fetch(:resource, {}) 

【讨论】:

    【解决方案2】:

    更新:

    脚手架 rails4 应用程序: https://github.com/szines/item_17751377

    如果用户在创建新项目时保持名称字段为空,它会起作用...

    看起来,它没有问题......

    Development.log 显示如果用户保留一个字段为空,参数将如下:

    "item"=>{"name"=>""}
    

    哈希中总有一些东西......

    正如 Mike Li 在评论中已经提到的那样,出了点问题......因为这个 params[:item] 不应该为空......

    您可以使用.nil? 检查是否有零,在这种情况下params[:item].nil? 将是true,如果它是nil。或者您可以使用 .present?正如 sytycs 已经写的那样。

    上一个答案:

    如果你有 :item 为空的情况,你应该直接使用 params[:item] 而不需要。

    def item_params
      params[:item].permit(:name)
    end
    

    strong_parameters.rb 源代码中有关 require 的更多信息:

    # Ensures that a parameter is present. If it's present, returns
    # the parameter at the given +key+, otherwise raises an
    # <tt>ActionController::ParameterMissing</tt> error.
    #
    #   ActionController::Parameters.new(person: { name: 'Francesco' }).require(:person)
    #   # => {"name"=>"Francesco"}
    #
    #   ActionController::Parameters.new(person: nil).require(:person)
    #   # => ActionController::ParameterMissing: param not found: person
    #
    #   ActionController::Parameters.new(person: {}).require(:person)
    #   # => ActionController::ParameterMissing: param not found: person
    def require(key)
      self[key].presence || raise(ParameterMissing.new(key))
    end
    

    【讨论】:

    • 这给了我一个 undefined method permit' 的 nil:NilClass` 错误。如果 params[:item] 不仅是空的,而且它本身也是 nil,我该怎么办?
    • 我搭建了一个 Rails 应用程序来找出您可能遇到的问题。你可以在这里找到:github.com/szines/item_17751377
    【解决方案3】:

    我个人没有切换到强参数,所以我不确定应该如何处理类似的事情:

     params.require(:item).permit(:name)
    

    但您始终可以通过以下方式检查项目是否存在:

    if params[:item].present?
       …
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 2013-03-05
      • 1970-01-01
      • 2016-12-10
      相关资源
      最近更新 更多