RoR为textarea设置值

时间:2010-08-26 22:34:25

标签: ruby-on-rails

有没有办法为text_area方法指定一个值,该方法将放置在生成的textarea标记之间?

这是我正在使用的代码示例。

<% remote_form_for ... do |f| %>
      <%= f.text_area :message %>
      <%= f.submit 'Update' %>
<% end %>

3 个答案:

答案 0 :(得分:23)

<% remote_form_for ... do |f| %>
      <%= f.text_area :message, :value => "my default value" %>
      <%= f.submit 'Update' %>
<% end %>

答案 1 :(得分:5)

您应该使用“占位符”HTML属性在HTML字段中显示默认文本。这就是这个属性的全部目的。在ROR中使用它的方法是: -

<%= f.text_area :message, :placeholder => "my default value" %>

答案 2 :(得分:4)

FormHelper text_area方法接受第二个参数来指定返回textarea主体的方法。

从上面链接的文档:

  text_area(:post, :body, :cols => 20, :rows => 40)
  # => <textarea cols="20" rows="40" id="post_body" name="post[body]">
  #      #{@post.body}
  #    </textarea>

  text_area(:comment, :text, :size => "20x30")
  # => <textarea cols="20" rows="30" id="comment_text" name="comment[text]">
  #      #{@comment.text}
  #    </textarea>

  text_area(:application, :notes, :cols => 40, :rows => 15, :class => 'app_input')
  # => <textarea cols="40" rows="15" id="application_notes" name="application[notes]" class="app_input">
  #      #{@application.notes}
  #    </textarea>

  text_area(:entry, :body, :size => "20x20", :disabled => 'disabled')
  # => <textarea cols="20" rows="20" id="entry_body" name="entry[body]" disabled="disabled">
  #      #{@entry.body}
  #    </textarea>