【问题标题】:Generate correct name with fields_for in Rails在 Rails 中使用 fields_for 生成正确的名称
【发布时间】:2013-05-25 08:12:01
【问题描述】:
= f.field_for @user.account || @user.build_account do |account|
  = account.text_field :name

正在生成

<input name="user[account][name]">

我想要:

<input name="user[account_attributes][name]">

我做错了什么?

【问题讨论】:

    标签: ruby-on-rails actionview fields-for


    【解决方案1】:

    我需要的是:

    = f.fields_for :account, @user.account || @user.build_account do |account|
    

    【讨论】:

      【解决方案2】:

      路过,顺便提一下object_name方法

      account.object_name 应该返回 "user[account_attributes]"

      因此,如果您在括号 "[field_name]" 之间连接您的字段名称,您可以轻松地为您的输入字段构建正确的名称

      【讨论】:

        猜你喜欢
        • 2012-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多