【问题标题】:simple form association as hidden field简单的表单关联作为隐藏字段
【发布时间】:2012-10-29 23:08:45
【问题描述】:

我正在尝试以简单的形式将关联值作为隐藏字段传递

和你做时一样的功能......

= simple form for @document do |f|
 = f.association :clients

...但是会生成隐藏字段insted

我正在尝试这样做

  = f.association :clients, as: hidden 

但这显然行不通

唯一对我有用的是

%input{ name: 'document[client_ids][]', value: '1'}
%input{ name: 'document[client_ids][]', value: '2'}
%input{ name: 'document[client_ids][]', value: '3'}

【问题讨论】:

  • 你试过= f.association :clients, type: 'hidden'吗?

标签: ruby-on-rails ruby-on-rails-3 simple-form


【解决方案1】:

也许

=f.hidden_field :client_id, :value => "some value"

但我认为你最好解释一下大局,然后我们可以为你推荐一个合适的 rails 答案。在大多数情况下,使用隐藏字段表明应该以更好的方式完成某些事情。

【讨论】:

    【解决方案2】:

    一个数组在提交时以简单的形式从关联发送。 以下将允许使用隐藏字段:

      <%= f.hidden_field('client_ids][', value: "some value") %>
    

    (注意:反括号很重要)

    【讨论】:

      【解决方案3】:

      我觉得

      <%= f.association :clients, input_html: { hidden: true } %>
      

      应该可以。

      【讨论】:

      • 不,这不起作用有两个原因,简单的形式 f.association 是第一个参数关联字段符号除外,(例如:has_many 的:clients :clients),即使我生成这些字段,它们仍然是选择只有 hidden=hidden 参数的 html 标记
      • hidden="hidden" 有什么问题? whatwg.org/specs/web-apps/current-work/multipage/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 2011-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多