开发者

Rails: Fill in form on invalid data

I have a rails form, and the label and input-box for each field are under a div class="field". When the user enters invalid data, rails changes the class to div class="field_with_errors" for me. My question is, when this happens, how do I populate the input-box with some prede开发者_运维技巧fined text?

I realize this is possible with javascript, but I'm clueless on how to implement it.


You could do this on the server side by using errors[:attribute]. You can find more info on the ruby on rails guides.


If you specify a default for your columns when creating the table, these will be used to pre-populate the forms.

Here's an example of a migration which specified defaults, that are then used by the form:

class CreateDevices < ActiveRecord::Migration
  def self.up
    create_table :devices do |t|
      t.string :name
      t.string :msgs_checked, :default => true
      t.string :app_version, :default => 'Not Known'
      t.boolean :active, :default => true

      t.timestamps
  end
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜