Edit- view duality


#1

Hi, I don’t know if this has been suggested before (btw is there a
searchable history of the mailing list? - I’m new)

My problem is the following. I have a form with many input fields,
sorted out and cleanly formatted using tables.

I would need to offer users a page exactly like it, only not editable.
And in particular, text areas in the ‘edit’ page will develop scrollbars
and truncate the text if it’s too long, but in the ‘view’ page, I’d like
to show everything.

Of course, the ideal solution would be to use a parameter into the page
(or partial or component) to switch editability on or off. For that,
form helpers should take an “editable” parameter, and produce an input
field or just a value if the parameter is false.

Foe example

.

.

<%= text_field "my_class", "compute", {:editable => false} %>

This would show the actual value, not the

I look at the definition of text_field in
ActionView::Helpers::FormHelper

def text_field(object_name, method, options = {})
InstanceTag.new(object_name, method, self,
nil,options.delete(:object)).to_input_field_tag(“text”, options)
end

Since I prefer not to mess with the rails source, I define my new
function in the helper module for my controller

def text_field(object_name, method, options = {})
InstanceTag.new(object_name, method, self,
nil,options.delete(:object)).value
end

(value is a message defined in the InstanceTag class in module
ActionView::Helpers::FormHelper)

However this doesn’t work at all.

Does someone have any other ideas for achieving edit/view duality
without code duplication?

Is there something already made in Rails?

Thanks

Jaime


#2

Silvela, Jaime (Exchange) wrote:

Does someone have any other ideas for achieving edit/view duality
without code duplication?

Certainly not the most elegant solution, but how about:

<% if editable then %> <%= text_field("myclass", "compute") %> <% else %> <%= @myclass.compute %> <% end %>

You could make that one line as well.

As for a search of the mailing list, go here:
http://www.ruby-forum.com/forum/3


#3

On 9-jun-2006, at 23:12, Silvela, Jaime (Exchange) wrote:

develop scrollbars and truncate the text if itâ??s too long, but in
the â??viewâ?? page, Iâ??d like to show everything.

Of course, the ideal solution would be to use a parameter into the
page (or partial or component) to switch editability on or off. For
that, form helpers should take an â??editableâ? parameter, and
produce an input field or just a value if the parameter is false.

Make an abstraction on top of the field helpers that will handle this
for you and handle the requirements.

You can look at the way of batch-creation of “covering” helper
methods in my plugin here:

http://julik.textdriven.com/svn/tools/rails_plugins/simplified_form/
lib/simplified_form.rb

Of particular interest:

field_helper_methods =
ActionView::Helpers::FormHelper.instance_methods.reject{|m| not
m.to_s =~ /field$/ }
field_helper_methods.concat
ActionView::Helpers::DateHelper.instance_methods.reject{|m| not
m.to_s =~ /select/}

field_helper_methods.concat
[:radio_button, :text_area, :check_box, :file_column_field ]

method_code = “”

collection_methods =
ActionView::Helpers::FormOptionsHelper.instance_methods.reject do |m|
(m.to_s =~ /for_select$/) or (not m.to_s =~ /select$/)
end

You then build a helper module on the fly and include it into your
controller. Judging from what you need you only have to replace the
actual field helpers with some generic display routine that will
detect the proper representation of the data based on the helper used.

Julian ‘Julik’ Tarkhanov
please send all personal mail to
me at julik.nl