Forum: Ruby on Rails edit- view duality

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2924f7b331d630802046a39f7b523414?d=identicon&s=25 Silvela, Jaime (Exchange) (Guest)
on 2006-06-09 23:16
(Received via mailing list)
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



<table>

.

.

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



</table>



This would show the actual value, not the <input>



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
1b6ed15a6126e1d5ef4d7d15550f30bb?d=identicon&s=25 François Montel (zerohalo)
on 2006-06-10 00:06
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:

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

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
A2b2f4ee23989dc68529baef9cbddcd6?d=identicon&s=25 Julian 'Julik' Tarkhanov (Guest)
on 2006-06-10 01:04
(Received via mailing list)
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_plugin...
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
This topic is locked and can not be replied to.