Form question

11 views
Skip to first unread message

rihad

unread,
Apr 30, 2013, 3:48:50 AM4/30/13
to Ruby on Rails: Talk
Hi, just started reading http://guides.rubyonrails.org/getting_started.html
and this code example:
<%= form_for(@post) do |f| %>
...
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>

In case we need to show a field in several templates, we need to
duplicate its type (text_field etc). The type belongs to the field, so
why not remove this repetition by making a self-contained "field" like
f.name, or f.label, or f.content, that would encompass rendering logic
and would spit out proper form snippet: <%= f.name %> Then if someone
wanted to override how a certain field is rendered, they could spell
everything out as above.

Frederick Cheung

unread,
Apr 30, 2013, 6:49:38 AM4/30/13
to rubyonra...@googlegroups.com
On Tuesday, April 30, 2013 8:48:50 AM UTC+1, rihad wrote:
> Hi, just started reading http://guides.rubyonrails.org/getting_started.html
>
>
>
>
> In case we need to show a field in several templates, we need to
>
> duplicate its type (text_field etc). The type belongs to the field, so
>
> why not remove this repetition by making a self-contained "field" like
>
> f.name, or f.label, or f.content, that would encompass rendering logic
>
> and would spit out proper form snippet: <%= f.name %> Then if someone
>
> wanted to override how a certain field is rendered, they could spell
>
> everything out as above.

You certainly can do that - I think either the guide or the api docs for form_for show how to subclass FormBuilder to achieve this. This is something that is going to be quite specific to each app so doesn't belong in rails itself

Fred

Reply all
Reply to author
Forward
0 new messages