开发者

Rails: Fill in form on invalid data

开发者 https://www.devze.com 2023-03-26 09:36 出处:网络
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. M

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

精彩评论

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