Ruby On Rails Application Developing

Application Generation

Creating the Application Structure

rails -d <database:oracle, mysql, sqlite ...> <name of project>

Generating the Scaffolds

ruby script/generate scaffold <table name in singular> <name of the controller>

example

ruby script/generate scaffold genre genre
ruby script/generate scaffold tale tale

Customizing the Model

++++Relationship Mapping

# has_many
class Genre < ActiveRecord::Base
has_many:tales
end
 
# belongs_to
class Tale < ActiveRecord::Base
  belongs_to :genre
end

++++Data Validation

# single check
class Genre < ActiveRecord::Base
  validates_presence_of :genre_name
  has_many :tales
end
 
# multipule check
class Tale < ActiveRecord::Base
  validates_presence_of :title, :body_text, :source
  belongs_to:genre
end

Customizing the Controller

#'New Tale' form
#tales_controller.rb
 
# 
def new
@tale = Tale.new
end
 
#|
#V
 
def new
  @genres=Genre.find_allgenres=all
  @tale = Tale.new
  @tale.status= “new"
end
 
# The create method is called after the data has been submitted.
def create
  @tale=Tale.new(params[:tale])
  @tale.genre_id=params[:genre])
  if@tale.saveif @save
    flash[:notice]='Talewassuccessfullycreated.'
    redirect_to:action=>'list'redirect_to :action => 'list'
  else
    render:action=>'new'render :action => 'new'
  end
end
  • params: It is an hash like structure containing the values of the form being submitted.
  • redirect_to: This method redirects the user to the action or the view specified.
  • render: It is used to output some data to the screen.
  • flash: It is a temporary hash stored in the session.
def edit
  @tale=Tale.find(params[:id])@
  @tale.status="updated"
end

An instance variable created in an action method is always accessible in the corresponding view.

Refining the View

Refining the New Tale Template

genres:action methods defined in genre_controller.rb
layouts:standard.rhtml, if you want to provide a different layout for genre, then you will have to create a genre.rhtml
tales

# new.rhtml
<h1>New tale</h1>
<% form_tag :action => 'create' do %>
<%= render :partial => 'form' %>
<%= submit_tag “Create" %>
<% end %>
<%= link_to 'Back', :action => 'list' %>

form_tag:It creates an HTML form tag. The :action parameter is (part of) the url hash that is used in the action attribute to post the info to. In reality, the form_tag takes a hash as parameter.
render:It renders the fragment of page on the browser.The fragment can be extracted from another template using the hash named :partial.
submit:It creates a submit button. The name to be displayed is given as the parameter.
link_to:It creates a link to a given action or a page. The parameters are the name to be displayed and the action or page to be called.

The convention that RoR follows in naming a template containing a fragment is prefixed with underscore (_)

# _form.rhtml before modify
<%= error_messages_for 'tale' %>
<!--[form:tale]-->
<p><label for="tale_title">Title</label><br/>
<%= text_field 'tale', 'title' %></p>
<p><label for="tale_body_text">Body text</label><br/>
<%= text_area 'tale', 'body_text' %></p>
<p><label for="tale_submission_date">Submission date</label><br/>
<%= date_select 'tale', 'submission_date' %></p>
<p><label for="tale_source">Source</label><br/>
<%= text_field 'tale', 'source' %></p>
<p><label for="tale_status">Status</label><br/>
<%=@tale.status></p>
<!--[eoform:tale]-->
 
# after modify
<%= error_messages_for 'tale' %>
<!--[form:tale]-->
<p><label for="tale_title">Title</label><br/>
<%= text_field 'tale', 'title' %></p>
<p><label for="tale_body_text">Body text</label><br/>
<%= text_area 'tale', 'body_text' %></p>
<p><label for="tale_submission_date">Submission date</label><br/>
<%= date_select 'tale', 'submission_date' %></p>
<p><label for="tale_source">Source</label><br/>
<%= text_field 'tale', 'source' %></p>
 
<p><label for="tale_genre_name">Genre<br/>
<select name="genre">
<% @genres.each do |genre| %>
<option value="<%= genre.id %>">
<%= genre.genre_name %>
</option>
<% end %>
</select>
</p>
<!--[eoform:tale]-->

Refining the Edit Tale Template

# edit.rhtml
<h1>Editing tale</h1>
<% form_tag :action => 'update', :id => @tale do %>
<%= render :partial => 'form' %>
<%= submit_tag 'Edit' %>
<% end %>
<%= link_to 'Show', :action => 'show', :id => @tale %> |
<%= link_to 'Back', :action => 'list' %>
 
# after modify
 
<h1>Editing tale</h1>
<% form_tag :action => 'update', :id => @tale do %>
<%= render :partial => 'form_edit' %>
<%= submit_tag 'Edit' %>
<% end %>
<%= link_to 'Show', :action => 'show', :id => @tale %> |
<%= link_to 'Back', :action => 'list' %>

As it is also calling _form.rhtml, we will have to change the fragment being called. The reason is that _form.rhtml contains code to create a select box with type of genres in it which we don't need while editing. So let's create a new template
for showing the edit form. The name will be _form_edit. It will contain the following code:

# 
<%= error_messages_for 'tale' %>
<!--[form:tale]-->
<p><label for="tale_title">Title</label><br/>
<%= text_field 'tale', 'title' %></p>
<p><label for="tale_body_text">Body text</label><br/>
<%= text_area 'tale', 'body_text' %></p>
<p><label for="tale_submission_date">Submission date</label><br/>
<%= date_select 'tale', 'submission_date' %></p>
<p><label for="tale_source">Source</label><br/>
<%= text_field 'tale', 'source' %></p>
<p><label for="tale_status">Status</label><br/>
<%= @tale.status %></p>
<p><label for="genre_name">Genre</label><br/>
<%=@tale.genre.genre_name %></p>
<!--[eoform:tale]-->