开发者

How to set a default value for a text input in ActiveScaffold?

开发者 https://www.devze.com 2023-02-16 13:28 出处:网络
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)

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开发者_如何学Go, 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'.


Try maybe :value => "Something"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号