Using the textarea helper in Rails forms

后端 未结 2 1218
予麋鹿
予麋鹿 2021-02-19 00:33

Why does this code show an error in text area?

<%= form_for(:ad, :url => {:action => \'create\'}) do |f| %>
  <%= f.text_field(:name) %>
  <         


        
相关标签:
2条回答
  • 2021-02-19 00:56

    The f variable that you are creating in the first line is a reference to your FormBuilder. By default it references ActionView::Helpers::FormBuilder or you can create your own.

    The FormBuilder helper for textareas is called text_area. FormBuilder helpers are smarter than regular HTML helpers. Rails models can be nested logically, and your forms can be written to reflect this; one of the primary things FormBuilder helpers do is keep track of how each particular field relates to your data model.

    When you call f.text_area, since f is associated with a form named :ad and the field is named :text it will generate a field named ad[text]. This is a parameter convention that will be automatically parsed into a Hash on the server: { :ad => { :text => "value" } } instead of a flat list of parameters. This is a huge convenience because if you have a Model named Ad, you can simply call Ad.create(params[:ad]) and all the fields will be filled in correctly.

    text_area_tag is the generic helper that isn't connected to a form automatically. You can still make it do the same things as FormBuilder#text_area, but you have to do it manually. This can be useful in situations that a FormBuilder helper isn't intended to cover.

    0 讨论(0)
  • 2021-02-19 01:07

    The FormHelper method is text_area, not text_area_tag.

    Use either of the following:

    <%= f.text_area(:text, size: '50x10') %>
    

    or:

    <%= text_area_tag(:ad, :text, size: '50x10') %>
    
    0 讨论(0)
提交回复
热议问题