Using Formtastic, in read-only mode, for a controller’s Show action

Formtastic is a powerful form library for Ruby on Rails. The documentation and tutorials I’ve read describe using it for the New and Edit actions, for an ActiveRecord, CRUD controller. However, … wouldn’t it be useful to re-use the same Formtastic partials the controller’s Show action?

After some trial and error, I succeeded in using adding a flag to disable/enable the inputs depending on the Action. That disableFlag is this then passed as the ‘disabled’ parameter, in the :input_html parameters. For the sake of this example, I have removed some of the extraneous code.

Here is an excerpt of the show.html.erb file. Notice the disableFlag being passed into the partial, in the locals array.

   Viewing Widget
   'form', :locals=>{:widget=>@widget, :disableFlag=>true} %>
    'edit', :id => @widget.id %>
   

Here is an excerpt of the edit.html.erb file. Notice the disableFlag, again.

   Editing Widget
    'form', :object => @widget, :locals => {:button_text =>"Update", :disableFlag=>false} %>
    |
   

Below… is part of the main partial, _form.html.erb

  
   {:multipart => true}) do |f| %>
  

											
	   <%= f hop over to this website.input :employee_name_first, :input_html => { :disabled=>disableFlag,  :size => 25 } %>
	    { :disabled=>disableFlag,  :size => 25 } %> 
	    { :disabled=>disableFlag,  :size => 25 } %>
	    { :disabled=>disableFlag,  :size => 25 } %>
	    { :disabled=>disableFlag,  :size => 25 } %>	
			

	
	   
	      disableFlag, :f => builder %>
	   	
	

And… here is a sub-form, _widget_workflow_fields.html.erb which receives the disableFlag.



	Workflow
	{:disabled=>disableFlag }, :as=>:select %>	

See the attached screenshots for an example for the main and sub forms, in new/edit versus show mode.

Comments |0|

Legend *) Required fields are marked
**) You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
In category: Uncategorized
Tagged with: ,