iBrasten

My methods of calculating time are far superior to yours, in every way.

 

This is the blog of Brasten Sager, a freelance software developer, Mariners fan, guitarist, haphazard philosopher.

readonly_forms plugin

April 17, 2006 @ 07:53 AM

I recently found myself solving the same Rails problem for the third time and decided to extract the solution to a plugin. Hopefully this helps someone else.

It’s like InPlaceEditor for a form…

Several of my projects recently have run into the same problem where a the data on one of my views needs to be editable sometimes, and read_only others. The two key requirements were: read-only means displaying the value only, not a read-only text box; and the form had to look largely identical in both states.

To solve this problem, you can either build two identical forms (one with text\_fields, the other without), or you can build one field with if/elses (or something along the lines of: <%= @editable ? text\_field(...) : value %>.

One form for both states…

Using the readonly\_forms plugin, text\_field and select tags now accept :read\_only as an option. If it’s false or nil, the tag operates normally. If it’s “true”, the input box is dropped and the value is displayed instead (in the case of selects, it will resolve the selected IDs with their display values… So you’ll see whatever you would have in the drop-down.)

Screenshots

ead\_only => true

Read Only Form

ead\_only => false (or nil) Read Only Form - Editable

View Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<div class="left pretty_form">
        <label for="customer[company]">Company:</label> <%= text_field('customer', 'company', :read_only => !editable, :size => 20, :class => 'my_new_plugin') %> <br />
        <label for="customer[name]">Name:</label> <%= text_field('customer', 'name', :read_only => !editable, :size => 20) %><br/>
        <label for="customer[title]">Title:</label> <%= text_field('customer', 'title', :read_only => !editable, :size => 20) %><br/>

        <label for="customer[address]">Address:</label> <%= text_field('customer', 'address', :read_only => !editable) %><br />
        <label for="customer[city]">City:</label> <%= text_field('customer', 'city', :read_only => !editable, :size => 15) %><br/>
        <label for="customer[state]">State:</label> <%= select('customer', 'state', Lead::STATES, :read_only => !editable) %><br/>
</div>

<div class="right pretty_form">
        <label for="customer[zip]">Zip:</label> <%= text_field('customer', 'zip', :read_only => !editable, :size => 10) %><br/>

<label for="customer[phone]">Phone:</label> <%= text_field('customer', 'home_phone', :read_only => !editable, :size => 15) %><br />
        <label for="customer[email]">Email:</label> <%= text_field('customer', 'email', :read_only => !editable, :size => 20) %><br />

        <label for="customer[source]">Source:</label> <%= select('customer', 'source', Lead::SOURCES, :read_only => !editable) %><br />
        <label for="customer[referred_by]">Referred By:</label> <%= text_field('customer', 'referred_by', :read_only => !editable, :size => 20) %><br />
        <label for="customer[status_code_id]">Status:</label> <%= select('customer', 'status_code_id', StatusCode.find(:all), :read_only => !editable) %><br />        
</div>

Download

This version works for text\_field and select tags. Other tags will be added later. I will also be adding support for form\_for as a later date.

0 Responses to “readonly_forms plugin”