【问题标题】:How do I get the value of a form.input in active admin (ruby on rails)如何在活动管理员中获取 form.input 的值(ruby on rails)
【发布时间】:2019-12-13 21:30:46
【问题描述】:

我正在使用管理框架“活动管理员”来构建 RubyOnRails 应用程序。我的表单中有一个如下所示的下拉菜单:

  form do |f|    

    f.inputs do
      f.input :art, as: :select, :include_blank => "Bitte wählen!", label: 'art', :collection => ["Fachobjekt", "Gruppe", "Externes Dokument"]
    end
    f.actions    
  end

现在在我的控制器中,我想检查选定的值。我试过了:


controller do
  after_save :update_object

  def update_object(guid)
      if params[:art].values == 'Fachobjekt'
        # do stuff
      end
  end
end

我在下拉列表中选择了“Fachobjekt”,但我得到了NoMethodeError "undefined method 'values' for nil:NilClass",因此params[:art] 为空。

我的问题是:获取 f.input-field 的选定值的正确语法是什么?我很感激任何提示!

[:root_tables] 是模型名称。我把它放在 [:art] 之前,就像 params[:root_tables][:art] 但同样的错误。

【问题讨论】:

  • 型号是什么?
  • [:root_tables] 是模型名称。我把它放在 [:art] 之前,就像 params[:root_tables][:art] 但同样的错误。
  • 嗯。它应该返回正确的参数。你使用允许的参数吗?因此,如果您清理参数,您将能够以 allowed_pa​​rams[:root_tables] 的形式访问它们
  • 所以,也许可以使用binding.pry 或类似方法进行一些基本调试。把它放在方法的最上面,看看你收到了什么参数。或者甚至对它们进行简单的打印(实际上 rails 在每个请求上都打印参数)发布这些参数,然后可能更容易找到原因

标签: ruby-on-rails activeadmin


【解决方案1】:

放一个

raise params.inspect

就在之前

if params[:art]

线。您应该能够确定发布的信息在参数中的位置。

【讨论】:

    【解决方案2】:

    感谢您的所有提示!特别是使用 binding.pry 进行调试的提示帮助了我。我现在使用 f.select 而不是 f.input:

      form do |f|    
    
        f.inputs do
          f.select :art, ["Fachobjekt", "Gruppe", "Externes Dokument"], :prompt => 'Bitte wählen! '
        end
        f.actions    
      end
    

    对于选择参数[:root_table][:art] 正在工作:

    controller do
      after_save :update_object
    
      def update_object(guid)
          if params[:root_table][:art] == 'Fachobjekt'
            # do stuff
          end
      end
    end
    

    当我使用 f.input 时,在执行 binding.pry 或 raise params.inspect 时 [:art] 甚至都没有出现。 使用 f.select 可以。 但是,至少它现在可以工作了,所以谢谢!

    【讨论】:

      猜你喜欢
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多