SUPPORT THE SITE WITH A CLICK

Subscribe Rss:

SUPPORT THE SITE WITH A CLICK

Friday, May 9, 2008

Use a partial form with form_for in ruby on rails

I’m sure the cool kids know this already but it gave me a problem so I thought I’d post it.
When using “render :partial => ‘form’”, where ‘form’ is being rendered for a ‘new’ and edit ‘view’, you need to pass the submit action.

Basically if u are using scaffold in rails,then it will generate new,index,edit,show .
Just notice the code inside new and edit will be same.Inorder make ur code looks more standard.Then we should go for render:partial
At first my new.html.erb looks like this

New article


<%= error_messages_for :article %>
<% form_for(@article) do |f| %>
<b>Site</b>
<%=collection_select 'article',
'site_id',
Site.find(:all), :id, :name ,{:prompt => 'Select the website'} %>
<b>Menu</b>
<%=select 'article',
'menu_id',
Menu.find(:all,
:conditions => 'parent_id is not null',
:order => 'sequence_number').collect{|c| [c.name, c.id]},
{:prompt => 'Select Menu'}%>


<b>Document type</b>
<%= collection_select 'article',
'document_type_id',
DocumentType.find(:all), :id, :name ,{:prompt => 'Select Doctype'} %>
<b>Title</b>
<%= f.text_field :title %>
<b>Sub title</b>
<%= f.text_field :sub_title %>
<%= f.submit "Create" %>
<% end %>
<%= link_to 'Back', articles_path %>


My edit.html.erb will look like this

Editing article


<%= error_messages_for :article %>
<% form_for(@article) do |f| %>
<b>Site</b>
<%=collection_select 'article',
'site_id',
Site.find(:all), :id, :name ,{:prompt => 'Select the website'} %>
<b>Menu</b>
<%=select 'article',
'menu_id',
Menu.find(:all,
:conditions => 'parent_id is not null',
:order => 'sequence_number').collect{|c| [c.name, c.id]},
{:prompt => 'Select Menu'}%>


<b>Document type</b>
<%= collection_select 'article',
'document_type_id',
DocumentType.find(:all), :id, :name ,{:prompt => 'Select Doctype'} %>
<b>Title</b>
<%= f.text_field :title %>
<b>Sub title</b>
<%= f.text_field :sub_title %>
<%= f.submit "update" %>
<% end %>
<%= link_to 'Show', @article %> |
<%= link_to 'Back', articles_path %>



Since the code in new and edit seems to be same.I have started using partials.By creating a file _form.html.erb.

My _form.html.erb will contains the common code for both edit & new.Even though the both new & edit form looks similar,but one is going for create action & other for update.so in my _form.html.erb i have used a common variable button_name.Now my _form.html.erb will look this

<% form_for(@article) do |f| %>
<b>Site</b>
<%=collection_select 'article',
'site_id',
Site.find(:all), :id, :name ,{:prompt => 'Select the website'} %>
<b>Menu</b>
<%=select 'article',
'menu_id',
Menu.find(:all,
:conditions => 'parent_id is not null',
:order => 'sequence_number').collect{|c| [c.name, c.id]},
{:prompt => 'Select Menu'}%>


<b>Document type</b>
<%= collection_select 'article',
'document_type_id',
DocumentType.find(:all), :id, :name ,{:prompt => 'Select Doctype'} %>
<b>Title</b>
<%= f.text_field :title %>
<b>Sub title</b>
<%= f.text_field :sub_title %>
<%= f.submit button_name %>
<% end %>


finally my new.html.erb will be look like

New article


<%= error_messages_for :article %>
<%=render :partial=> 'form',:locals=>{:button_name=>"Create"}%>
<%= link_to 'Back', articles_path %>


here i am passing the value of the button as Create and for edit.html.erb i will be passing it as Update
finally my edit.html.erb will look like

Editing article


<%= error_messages_for :article %>
<%=render :partial=> 'form',:locals => {:button_name=>"Update" }%>
<%= link_to 'Show', @article %> |
<%= link_to 'Back', articles_path %>


Note:This has been tested under rails 2.0.2