overriding form element?

1 view
Skip to first unread message

John Paul

unread,
Jun 1, 2007, 9:51:33 PM6/1/07
to Streamlined
Folks,

I'm trying to do something that seems fairly straightforward -- I'd
like to take an AR field that's a string and display/edit it using a
textarea instead of a input box.

Am I correct in assuming I have to override the _form.rhtml partial to
do that? And when I *do* override the partial, can I pretend like it's
just @mymodel going into the partial and do something along the lines
of

<% form_for :sponsor do |f| %>
<% f.text_area %>
<% end %>

and it will magically work?

Or do I have to use the @streamlined_item and modify the content of
the generic _form partial and hack my way through everything? Do I
also have to override the controller methods so I get @sponsor? I got
lost specifically here

/vendor/streamlined/lib/streamlined/column/base.rb#render_td (which I
found in the _form partial)

send "render_td_#{view.crud_context}", view, item

Was hoping there was some way to override the way it rendered the item
there but lost my way.

As an aside, I also need to modify a couple of forms to upload files
using attachment_fu, so I'm going to need to get comfortable with all
these modifications to form partials. Advice or a quick turtorial
would be greatly appreciated.

Matthew

unread,
Jun 6, 2007, 2:54:45 PM6/6/07
to strea...@googlegroups.com
John Paul, one way to accomplish this quite easily would be to change
the database column for the field you want to show up as a text area
from a :string type to a :text type. Streamlined is calling Rails'
#input method underneath, which will render the field as a text area
if the type of the column in the database is :text.

Otherwise, I would copy the generic _form.rhtml from Streamlined into
your controller's views directory and customize it to suit my needs.
In some cases, I've even defined the form fields I need using HTML...
bypassing Streamlined's declarative syntax.

HTH,
Matthew

John Paul

unread,
Jun 9, 2007, 11:35:43 AM6/9/07
to Streamlined

> Otherwise, I would copy the generic _form.rhtml from Streamlined into
> your controller's views directory and customize it to suit my needs.
> In some cases, I've even defined the form fields I need using HTML...
> bypassing Streamlined's declarative syntax.

A guide for doing that customization (or a rake task to materialize
the default form) would be a great help. I'm finding that I have to do
a few customizations here and there (eg to handle file uploads or
changing the appearance of a string field to textarea) and I'm unsure
what the resulting code needs to look like since the _form.rhtml just
loops through the columns. Any quick thoughts/examples on overriding a
form with your own custom one?

>
> HTH,
> Matthew

Matthew

unread,
Jun 12, 2007, 9:16:51 PM6/12/07
to strea...@googlegroups.com
It's as simple as dropping a partial of the same name into the views
directory for your controller. So for example, to override the form,
you would create the _form.rhtml partial as I outlined in my previous
post. This is what it looks like by default:

<%= error_messages_for model_underscore %>

<!--[form:<%= model_underscore %>]-->

<table class="sl_edit_form">

<% model_ui.edit_columns.each do |column| %>
<% if column.is_displayable_in_context?(self) %>
<tr><td class="sl_edit_label"><label for="<%= model_underscore
%>_<%= column.name %>"><%= column.human_name %></label></td>
<td class="sl_edit_value"><%= column.render_td(self,
@streamlined_item) %></td>
</tr>
<% end %>
<% end %>

</table><!--[eoform:<%= model_underscore %>]-->


I would then take this and either customize it to suit my needs, or
just tack on additional HTML below as needed. So for example, if I
wanted to display a list of other items, I would add this at the
bottom of the template:

<% for item in @items %>
<%= item.name %><br/>
<%= item.address %><br/>
<% end %>


...and so on. I would, of course, have to also update the Streamlined
controller actions that deal with this form. I would do this by
copying one or more of the actions out of
lib/streamlined/controller/crud_methods.rb and modifying it as needed.
I might end up with something like this for the "new" action:


def new
self.crud_context = :new
self.instance = model.new
@items = self.instance.items
render_or_redirect(:success, 'new')
end


Hope this helps. A generator is an interesting idea that may work in
some of these cases, but to be honest we're all pretty much slammed
working on other stuff at the moment and probably won't be able to
write a guide or build a generator for this for quite some time.

If you end up writing something yourself, please submit it as a
ticket! We appreciate *any and all* contributions from the community.

Matthew

Matthew Bass

unread,
Jul 19, 2008, 9:51:32 AM7/19/08
to strea...@googlegroups.com
Sorry, but we haven't made any progress on this. The process of overriding a template is fairly straightforward, and a common practice when we've used Streamlined for projects in the past. If you have any specific questions about the process, feel free to post here and one of us will do our best to answer.

 
Matthew
Reply all
Reply to author
Forward
0 new messages