Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RoR set value for textarea

Is there a way to specify a value to the text_area method that will be placed between the generated textarea tags?

This is an example of the code I am using.

<% remote_form_for ... do |f| %>
      <%= f.text_area :message %>
      <%= f.submit 'Update' %>
<% end %>
like image 636
Brian Avatar asked Aug 26 '10 22:08

Brian


3 Answers

<% remote_form_for ... do |f| %>
      <%= f.text_area :message, :value => "my default value" %>
      <%= f.submit 'Update' %>
<% end %>
like image 165
Jed Schneider Avatar answered Oct 19 '22 18:10

Jed Schneider


You should be using the 'placeholder' HTML attribute for showing default texts in HTML fields. Thats the whole purpose of this attribute. The way to use it in ROR is:-

<%= f.text_area :message, :placeholder => "my default value" %>
like image 45
adhundia Avatar answered Oct 19 '22 16:10

adhundia


The FormHelper text_area method takes a second argument to specify the method which returns the body of a textarea.

From the documentation linked above:

  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>
like image 4
Lee Jarvis Avatar answered Oct 19 '22 16:10

Lee Jarvis