ROR: Creating Views ( Action View )

 
The Rails View directory C:\ruby\organics\app\views\hydrocarbon 
is automatically created when we had created the controller 
hydrocarbon with the generate script command.

We need to create the view files for the established 
methods in the controller; namely hydrocarbon_controller.rb. 
These files have to be written and saved with RHTML extention, 
in  the View directory :
C:\ruby\organics\app\views\hydrocarbon.

a. For list method: list.rhtml 

<ul id="groups"> <% Group.find(:all).each do |c| %> <li><%= link_to c.functional, :action => "display_sgroups", :id => c.id %></li> <% end %> </ul> <% if @hydrocarbons.blank? %> <p>There are not hydrocarbons available ..</p> <% else %> <p>Here are the available hydrocarbons:</p> <ul id="hydrocarbons"> <% @hydrocarbons.each do |c| %> <li> <%= link_to c.compound, :action => 'display', :id => c.id %> <b> <%= link_to 'edit', {:action => "edit", :id => c.id} %> </b> </li> <li> <%= link_to c.compound, :action => 'display', :id => c.id%> <b> <%= link_to 'edit', :action => 'edit', :id => c.id %> </b> <b> <%= link_to "remove", {:action => 'remove', :id => c.id}, :confirm => "Are you sure you want to remove this item?" %> </b></li> <% end %> </ul> <% end %> <p> <%= link_to "Add new Hydrocarbon", :action => 'new' %> </p>
b. For new method: new.rhtml

<h1>Add new hydrocarbon</h1> <%= form_tag :action=>'create' %> <p><label for="hydrocarbon_compound">Name</label>: <%= text_field 'hydrocarbon', 'compound' %></p> <p><label for="hydrocarbon_nbCarbons">nbCarbons</label>: <%= text_field 'hydrocarbon', 'nbCarbons' %></p> <p><label for="hydrocarbon_formula">Formula</label>: <%= text_field 'hydrocarbon', 'formula' %></p> <p><label for="hydrocarbon_image">image</label>: <%= file_field 'hydrocarbon', 'image' %></p> <p><label for="hydrocarbon_group">Group</label>: <%= collection_select(:hydrocarbon,:group_id,@groups,:id, :functional) %></p> <p><label for="hydrocarbon_description">Description </label><br/> <%= text_area 'hydrocarbon', 'description' %></p> <%= submit_tag "create" %> <%= form_tag %> <%= link_to 'Back', {:action => 'list'} %>

c. For display method: display.rhtml <h3><%=@hydrocarbons.compound %></h3> <p><strong>Name: </strong> <%= @hydrocarbon.compound %><br /> <p><strong>nbCarbons: </strong> $<%= @hydrocarbon.nbCarbons %><br /> <p><strong>Formula: </strong> $<%= @hydrocarbon.formula %><br /> <p><strong>Image: </strong> $<%= @hydrocarbon.image %><br /> <strong>Group :</strong> <%= @hydrocarbon.group.functional %><br /> <strong>Created Date:</strong> <%= @hydrocarbon.created_at %><br /> </p> <p><%= @hydrocarbon.description %></p> <hr /> <%= link_to 'Back', {:action => 'list'} %>
d. For edit method: edit.rhtml

<h3>Edit Book Detail</h3> <%= form_tag :action => 'update', :id => @hydrocarbon %> <p><label for="hydrocarbon_compound">Name</label>: <%= text_field 'hydrocarbon', 'compound' %></p> <p><label for="hydrocarbon_nbCarbons">nbCarbons</label>: <%= text_field 'hydrocarbon', 'nbCarbons' %></p> <p><label for="hydrocarbon_formula">Formula</label>: <%= text_field 'hydrocarbon', 'formula' %></p> <p><label for="hydrocarbon_image">image</label>: <%= file_field 'hydrocarbon', 'image' %></p> <p><label for="hydrocarbon_group">Group</label>: <%= collection_select(:hydrocarbon, :group_id, @groups, :id, :functional) %></p> <p><label for="hydrocarbon_description">Description </label><br/> <%= text_area 'hydrocarbon', 'description' %></p> <%= submit_tag "Save changes" %> <%= form_tag %> <%= link_to 'Back', {:action => 'list' } %>

e. Modify list.rhtml and add the following edit link

<li>
<%= link_to c.compound, :action => 'display', :id => c.id %>
<b> <%= link_to 'edit', {:action => "edit", :id => c.id} %></b>
</li>

f. For remove method: remove.rhtml

<li> <%= link_to c.compound, :action => 'display', :id => c.id%> <b> <%= link_to 'edit', :action => 'edit', :id => c.id %></b> <b> <%= link_to "remove", {:action => 'remove', :id => c.id}, :confirm => "Are you sure you want to remove this item?" %> </b></li>
Modify list.rhtml again and add a remove link g.Creating view file for display_groups method
g.1. First define the method in the file: C:\ruby\organics\app\controllers\hydrocarbon_controller.rb as follows:

def display_groups
@group = Group.find(params[:id])
end
and g.2. Create the related view file:

<h3><%= @group.functional -%></h3> <ul> <% @group.hydrocarbons.each do |c| %> <li><%= link_to c.compound, :action => "display", :id => c.id %></li> <% end %> </ul>
then c.Modify display.rhtml as follows:

<strong>Group: </strong>
<%= link_to @hydrocarbon.group.functional,
:action => "display_groups", :id => @hydrocarbon.group.id %>
h.Modify list.rhtml as follows:

<ul id="groups">
<% Group.find(:all).each do |c| %>
<li><%= link_to c.functional, :action => "display_groups",
:id => c.id %></li>
<% end %>
</ul>


Run the server, always with the commad prompt:
C:\ruby\organics> ruby script/server
Browse:
http://localhost:3000/hydrocarbon/list