如何在ActiveScaffold中设置文本输入的默认值?

发布于 2024-10-21 20:32:36 字数 1618 浏览 1 评论 0原文

如何使用 ActiveScaffold 1.2RC1 为文本输入设置默认值?

对于更高版本,它看起来像这样(来自 http:// /activescaffold.com/2010/7/21/changes-in-naming-schema-for-overrides)应该可以工作:

module PlayersHelper
  def player_name_form_column(record, options)
    text_field :record, :name, options.merge(:value => record.name || 'new player')
  end
end

但它出现在1.2RC1中,列形式覆盖方法将输入名称作为第二个参数。我尝试过这个:

module PlayersHelper
  def player_name_form_column(record, _)
    text_field :record, :name, {:value => record.name || 'new player'}
  end
end

但没有效果。

更新

我的第二次尝试确实有效。事实上,这两种方法都有效:

    text_field :record, :name, {:value => record.name || 'new player'}
    text_field :record, :name, :value => (record.name || 'new player')

有趣的是,ActiveScaffold 实际上会将数据库中列的默认值传播到输入表单!我的玩家表如下所示:

mysql> show create table players\G
*************************** 1. row ***************************
       Table: players
Create Table: CREATE TABLE `players` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(64) DEFAULT 'Manny Ramirez',
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=119 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

因此record.name实际上设置为“Manny Ramirez”,这意味着我从未看到过默认值。因此,这里正确的做法似乎是修改数据库中的默认值,因为盲目设置该值会破坏编辑(即,如果玩家的名字是“David Ortiz”,单击“编辑”将弹出一个具有所有 David 属性的玩家,但名称设置为“曼尼·拉米雷斯”。

How does one set a default value for a text input using ActiveScaffold 1.2RC1?

For later versions, it looks like this (from http://activescaffold.com/2010/7/21/changes-in-naming-schema-for-overrides) should work:

module PlayersHelper
  def player_name_form_column(record, options)
    text_field :record, :name, options.merge(:value => record.name || 'new player')
  end
end

But it appears in 1.2RC1, the column form override method takes the input name as the second argument. I tried this:

module PlayersHelper
  def player_name_form_column(record, _)
    text_field :record, :name, {:value => record.name || 'new player'}
  end
end

But it had no effect.

Update

My second try actually did work. In fact, both of these work:

    text_field :record, :name, {:value => record.name || 'new player'}
    text_field :record, :name, :value => (record.name || 'new player')

The interesting thing is that ActiveScaffold will actually propagate the default value for a column in the database to the input form! My players table looks like this:

mysql> show create table players\G
*************************** 1. row ***************************
       Table: players
Create Table: CREATE TABLE `players` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(64) DEFAULT 'Manny Ramirez',
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=119 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

So record.name was actually set to 'Manny Ramirez', meaning I never saw my default. So the correct thing to do here seems to be to modify the default in the database, since blindly setting the value will break edits (i.e. if the player's name is 'David Ortiz', clicking Edit would pop up a player with all David's attributes, but with the name set to 'Manny Ramirez'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人骨 2024-10-28 20:32:36

尝试一下 :value => “某事”

Try maybe :value => "Something"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文