0
Ruby On Rails and More
Previously <ul><li>Saw the MVC methodology </li></ul><ul><li>Used scaffold to create a simple application </li></ul><ul><l...
Rails Active Records <ul><li>This is the Object Relational Mapping layer </li></ul><ul><li>Allows mapping between the appl...
Rails friendly MySQL <ul><li>The naming convention carries into the database design </li></ul><ul><li>Each entity in our d...
Active Record Files <ul><li>We have looked at these before </li></ul><ul><li>These belong to the  model  part of the MVC <...
Multiple Tables/Models <ul><li>So far we have looked at applications with a single table/model </li></ul><ul><li>What if w...
Model Associations <ul><li>one-to-one </li></ul><ul><ul><li>A car has one steering wheel </li></ul></ul><ul><li>one-to-man...
Declaring your associations <ul><li>Add the declarations to your  model  files </li></ul><ul><li>So  car.rb  should look l...
Declaring your associations <ul><li>Likewise the  manufacturer.rb  file should look like </li></ul>class Manufacturer < Ac...
Validating Data <ul><li>You can place your validations inside your models as well </li></ul><ul><li>This is used to ensure...
Validation Methods <ul><li>validates_presence_of: </li></ul><ul><ul><li>Has the field been filled in </li></ul></ul><ul><l...
More Validation Methods <ul><li>validates_uniqueness_of: </li></ul><ul><ul><li>Makes sure an entry is unique </li></ul></u...
Even More Validation Methods <ul><li>validates_exclusion_of: </li></ul><ul><ul><li>Is not within some range </li></ul></ul...
Migration <ul><li>Allows for easy database control </li></ul><ul><li>Updates to the database are easy </li></ul><ul><li>Mu...
Migration methods <ul><li>create_table(name, options) </li></ul><ul><li>drop_table(name) </li></ul><ul><li>rename_table(ol...
Migration data types <ul><li>string -  is for small data types such as a title. </li></ul><ul><li>text -  is for longer pi...
Database column options <ul><li>Valid column options are: </li></ul><ul><li>limit  ( :limit => “50” ) </li></ul><ul><li>de...
Generating migrations <ul><li>Migrations can be created using the generate script </li></ul><ul><li>script/generate migrat...
The migration file class Cars < ActiveRecord::Migration def self.up create_table :cars do |t| t.column :model, :string, :l...
Using the migration file <ul><li>As mentioned before we use the  rake  utility to migrate the database </li></ul><ul><li>T...
Using the controllers <ul><li>We have seen that controllers are the ‘go-between’ of the application </li></ul><ul><li>They...
Retrieving data <ul><li>@cars = Car.find(:all) </li></ul><ul><ul><li>Retrieve all the entries in the cars table </li></ul>...
Creating objects <ul><li>‘ new’ is used to create a new object </li></ul><ul><li>@car = Car.new(params[:car]) </li></ul><u...
Saving data <ul><li>If we have an object that has been populated saving is easy!! </li></ul><ul><li>We can send this data ...
Where to send the user <ul><li>We can send the user to different places dependant on some output </li></ul><ul><li>E.g. if...
Removing data <ul><li>As you have seen database manipulation is quite straight forward </li></ul><ul><li>Deleting an entry...
How the view code looks <% if @cars.blank? %> <p> There are not any cars in the database yet </p> <% else %> <p> The cars ...
link_to method <ul><li>In the last slide we used the  link_to  method </li></ul><ul><li>Dynamicaly make links that depend ...
link_to examples link_to &quot;Visit Other Site&quot;, &quot;http://www.rubyonrails.org/&quot;, :confirm => &quot;Are you ...
Making it look nice <ul><li>We can define layouts for our applications </li></ul><ul><li>Then apply that layout to any pag...
Yield <ul><li>You must place a ruby command in the layout file so Rails knows where to add data </li></ul><ul><li><%= yiel...
Ajax <ul><li>Asyncronous JavaScript and XML </li></ul><ul><li>Ajax has had a massive impact on application design </li></u...
Web applications <ul><li>Previously web applications were very limited </li></ul><ul><li>They were not very interactive </...
Data Exchange <ul><li>Ajax allows for data to be sent and received without the whole page updating </li></ul><ul><li>This ...
How does it work? <ul><li>It uses Javascript to make the function calls that get/send data </li></ul><ul><li>Data is retri...
Some Ajax example sites <ul><li>http://maps.google.com/ </li></ul><ul><li>http://www.google.com/webhp?complete=1&hl=en </l...
Ajax and Rails <ul><li>Luckily Rails has incorporated Ajax so that we can make interactive pages </li></ul><ul><li>We can ...
Updating with Ajax <%= form_remote_tag( :url => {:action => 'create'},  :update => “manufacturer_list”,  :position => :bot...
What does this do? <ul><li>It will submit to ‘create’ th string given by the form </li></ul><ul><li>It will update the man...
Some More Ajax Examples <ul><li>http://www.writely.com/ </li></ul><ul><li>http://instantdomainsearch.com/ </li></ul><ul><l...
More Rails <ul><li>RoR also allows you to quickly generate other tools for your web application </li></ul><ul><li>Uploadin...
Upcoming SlideShare
Loading in...5
×

Download It

685

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
685
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Download It"

  1. 1. Ruby On Rails and More
  2. 2. Previously <ul><li>Saw the MVC methodology </li></ul><ul><li>Used scaffold to create a simple application </li></ul><ul><li>CreateReadUpdateDelete (CRUD) </li></ul><ul><li>All working on a single MySQL table </li></ul>
  3. 3. Rails Active Records <ul><li>This is the Object Relational Mapping layer </li></ul><ul><li>Allows mapping between the application and the database </li></ul><ul><li>Relies on the naming convention </li></ul>
  4. 4. Rails friendly MySQL <ul><li>The naming convention carries into the database design </li></ul><ul><li>Each entity in our domain has a table in the database. </li></ul><ul><li>Naming convention gives car (entity) maps to cars (table) </li></ul><ul><li>Each table has a unique number field which is called id </li></ul><ul><li>If the entity car belongs_to manufacturer then there will be a manufacturer_id in the cars table </li></ul>
  5. 5. Active Record Files <ul><li>We have looked at these before </li></ul><ul><li>These belong to the model part of the MVC </li></ul><ul><li>We created these using the generator script </li></ul><ul><li>script/generate model Car </li></ul>
  6. 6. Multiple Tables/Models <ul><li>So far we have looked at applications with a single table/model </li></ul><ul><li>What if we have many tables/models </li></ul><ul><li>How do we incorporate relationships between these tables/models? </li></ul>
  7. 7. Model Associations <ul><li>one-to-one </li></ul><ul><ul><li>A car has one steering wheel </li></ul></ul><ul><li>one-to-many </li></ul><ul><ul><li>A manufacturer has many cars </li></ul></ul><ul><li>many-to-many </li></ul><ul><ul><li>A manufacturer has many points of sale </li></ul></ul><ul><ul><li>Each point of sale has many manufactures </li></ul></ul>
  8. 8. Declaring your associations <ul><li>Add the declarations to your model files </li></ul><ul><li>So car.rb should look like </li></ul>class Car < ActiveRecord::Base belongs_to :manufacturer end Inherits
  9. 9. Declaring your associations <ul><li>Likewise the manufacturer.rb file should look like </li></ul>class Manufacturer < ActiveRecord::Base has_many :cars end Note the plural
  10. 10. Validating Data <ul><li>You can place your validations inside your models as well </li></ul><ul><li>This is used to ensure the correct data is entered into the database </li></ul><ul><li>Users can make mistakes! </li></ul>
  11. 11. Validation Methods <ul><li>validates_presence_of: </li></ul><ul><ul><li>Has the field been filled in </li></ul></ul><ul><li>validates_length_of: </li></ul><ul><ul><li>How many characters are there </li></ul></ul><ul><li>validates_acceptance_of: </li></ul><ul><ul><li>You can set what the user should enter I.e. a confirming they have read and ticked something </li></ul></ul><ul><li>validates_confirmation_of: </li></ul><ul><ul><li>For passwords </li></ul></ul>
  12. 12. More Validation Methods <ul><li>validates_uniqueness_of: </li></ul><ul><ul><li>Makes sure an entry is unique </li></ul></ul><ul><li>validates_format_of: </li></ul><ul><ul><li>For things like email etc </li></ul></ul><ul><li>validates_numericality_of: </li></ul><ul><ul><li>Is a field numberic </li></ul></ul><ul><li>validates_inclusion_of: </li></ul><ul><ul><li>Can check for inclusion in a range </li></ul></ul>
  13. 13. Even More Validation Methods <ul><li>validates_exclusion_of: </li></ul><ul><ul><li>Is not within some range </li></ul></ul><ul><li>To output an error message on failure to validate </li></ul><ul><ul><li>:message => ‘This failed to validate’ </li></ul></ul>
  14. 14. Migration <ul><li>Allows for easy database control </li></ul><ul><li>Updates to the database are easy </li></ul><ul><li>Multiple people/machines can update from a migration file </li></ul><ul><li>Migration allows you to perform many operations on your database </li></ul>
  15. 15. Migration methods <ul><li>create_table(name, options) </li></ul><ul><li>drop_table(name) </li></ul><ul><li>rename_table(old_name, new_name) </li></ul><ul><li>add_column(table_name, column_name, type, options) </li></ul><ul><li>rename_column(table_name, column_name, new_column_name) </li></ul><ul><li>change_column(table_name, column_name, type, options) </li></ul><ul><li>remove_column(table_name, column_name) </li></ul><ul><li>add_index(table_name, column_name, index_type) </li></ul><ul><li>remove_index(table_name, column_name) </li></ul><ul><li>Source (tutorialspoint.com) </li></ul>
  16. 16. Migration data types <ul><li>string - is for small data types such as a title. </li></ul><ul><li>text - is for longer pieces of textual data, such as the description. </li></ul><ul><li>integer - is for whole numbers. </li></ul><ul><li>float - is for decimals.
 </li></ul><ul><li>datetime and timestamp - store the date and time into a column. </li></ul><ul><li>date and time - store either the date only or time only. </li></ul><ul><li>binary - is for storing data such as images, audio, or movies. </li></ul><ul><li>boolean - is for storing true or false values. </li></ul><ul><li>Source (tutorialspoint.com) </li></ul>
  17. 17. Database column options <ul><li>Valid column options are: </li></ul><ul><li>limit ( :limit => “50” ) </li></ul><ul><li>default (:default => “b l ah”) </li></ul><ul><li>null (:null => false implies NOT NULL) </li></ul><ul><li>Source (tutorialspoint.com) </li></ul>
  18. 18. Generating migrations <ul><li>Migrations can be created using the generate script </li></ul><ul><li>script/generate migration cars </li></ul><ul><li>This creates the migration for the table cars which is the plural of the entity car </li></ul>
  19. 19. The migration file class Cars < ActiveRecord::Migration def self.up create_table :cars do |t| t.column :model, :string, :limit => 32, :null => false t.column :price, :float t.column :colour, :string t.column :created_at, :timestamp end end def self.down drop_table :cars end end Migrate the database forward Rollback
  20. 20. Using the migration file <ul><li>As mentioned before we use the rake utility to migrate the database </li></ul><ul><li>This acts as a Version Control System </li></ul><ul><li>This tracks all the changes we make to the database using rake </li></ul><ul><li>rake db:migrate </li></ul>
  21. 21. Using the controllers <ul><li>We have seen that controllers are the ‘go-between’ of the application </li></ul><ul><li>They link the front end to the database </li></ul><ul><li>Sending and receiving data from the database </li></ul>
  22. 22. Retrieving data <ul><li>@cars = Car.find(:all) </li></ul><ul><ul><li>Retrieve all the entries in the cars table </li></ul></ul><ul><li>@car = Car.find(params[:id]) </li></ul><ul><ul><li>Retrieve the entry in the database with the params id. Id is usually dependant on the user clicking on a db entry in the front end. Params lets you pass information </li></ul></ul>
  23. 23. Creating objects <ul><li>‘ new’ is used to create a new object </li></ul><ul><li>@car = Car.new(params[:car]) </li></ul><ul><li>Again params is used to pass data which we have probably received from the user Interface </li></ul>
  24. 24. Saving data <ul><li>If we have an object that has been populated saving is easy!! </li></ul><ul><li>We can send this data to the database… </li></ul><ul><li>@car.save </li></ul>
  25. 25. Where to send the user <ul><li>We can send the user to different places dependant on some output </li></ul><ul><li>E.g. if some code fails send them to a else send them to b </li></ul><ul><li>redirect_to :action => ’show’ </li></ul><ul><ul><li>Takes them back to the show method and renders that template (view file) </li></ul></ul><ul><li>render :action => ‘list’ </li></ul><ul><ul><li>Render the ‘list’ template (view file) </li></ul></ul>
  26. 26. Removing data <ul><li>As you have seen database manipulation is quite straight forward </li></ul><ul><li>Deleting an entry is no different </li></ul><ul><li>Car.find(params[:id]).destroy </li></ul>
  27. 27. How the view code looks <% if @cars.blank? %> <p> There are not any cars in the database yet </p> <% else %> <p> The cars we have found in the database are… </p> <ul id=”cars&quot;> <% @cars.each do |c| %> <li> <%= link_to c.model, {:action => 'show', :id => c.id} -%> </li> <% end %> </ul> <% end %> <p> <%= link_to &quot;Add new Car&quot;, {:action => 'new' }%> </p> Any entries? For each car found create a hyper link and put it as a list item
  28. 28. link_to method <ul><li>In the last slide we used the link_to method </li></ul><ul><li>Dynamicaly make links that depend on the routing system (defind in routes.rb ) </li></ul><ul><li>link_to (name, options = {}, html_options = nil) </li></ul>
  29. 29. link_to examples link_to &quot;Visit Other Site&quot;, &quot;http://www.rubyonrails.org/&quot;, :confirm => &quot;Are you sure?&quot; # => <a href=&quot;http://www.rubyonrails.org/&quot; onclick=&quot;return confirm('Are you sure?');&quot;>Visit Other Site</a> link_to &quot;Help&quot;, { :action => &quot;help&quot; }, :popup => true # => <a href=&quot;/testing/help/&quot; onclick=&quot;window.open(this.href);return false;&quot;>Help</a> link_to &quot;View Image&quot;, { :action => &quot;view&quot; }, :popup => ['new_window_name', 'height=300,width=600'] # => <a href=&quot;/testing/view/&quot; onclick=&quot;window.open(this.href,'new_window_name','height=300,width=600');return false;&quot;>View Image</a> Source rubyonrails.com
  30. 30. Making it look nice <ul><li>We can define layouts for our applications </li></ul><ul><li>Then apply that layout to any page </li></ul><ul><li>They live in app/views/layouts </li></ul><ul><li>Similar to dreamweaver et al </li></ul><ul><li>Create your layout_file.rhtml </li></ul><ul><li>Add your HTML </li></ul><ul><li>Add a style sheet </li></ul>
  31. 31. Yield <ul><li>You must place a ruby command in the layout file so Rails knows where to add data </li></ul><ul><li><%= yield -%> </li></ul><ul><li>In the controller file you wish to apply this layout to…. Add </li></ul><ul><li>layout ’layout_file’ </li></ul>
  32. 32. Ajax <ul><li>Asyncronous JavaScript and XML </li></ul><ul><li>Ajax has had a massive impact on application design </li></ul><ul><li>Previously the web was a poor place for many applications </li></ul><ul><li>Ajax has changed much of this, especially when linked in with RoR etc </li></ul>
  33. 33. Web applications <ul><li>Previously web applications were very limited </li></ul><ul><li>They were not very interactive </li></ul><ul><li>Sending and receiving data was slow and clunky </li></ul><ul><li>Showing changing data was difficult </li></ul>
  34. 34. Data Exchange <ul><li>Ajax allows for data to be sent and received without the whole page updating </li></ul><ul><li>This makes for a more fluid experience </li></ul><ul><li>Increases interactivity </li></ul><ul><li>Increases speed </li></ul><ul><li>Less data is sent/received </li></ul><ul><li>Makes the page more usable </li></ul>
  35. 35. How does it work? <ul><li>It uses Javascript to make the function calls that get/send data </li></ul><ul><li>Data is retrieved from the server using the XMLHttpRequest technology </li></ul><ul><li>This is what makes it possible to exchange data with the server without the need for full page reloads </li></ul>
  36. 36. Some Ajax example sites <ul><li>http://maps.google.com/ </li></ul><ul><li>http://www.google.com/webhp?complete=1&hl=en </li></ul><ul><li>http://maps.yahoo.com/ </li></ul><ul><li>http://www. tutorialspoint . com/cgi-bin/ajaxCGI . cgi ?num1=1&num2=1&result=0& semajax=Standard </li></ul>
  37. 37. Ajax and Rails <ul><li>Luckily Rails has incorporated Ajax so that we can make interactive pages </li></ul><ul><li>We can include the necessary files in our layout page to save doing this over and over </li></ul><ul><li><%= javascript_include_tag :defaults %> </li></ul>
  38. 38. Updating with Ajax <%= form_remote_tag( :url => {:action => 'create'}, :update => “manufacturer_list”, :position => :bottom, :html => {:id => ‘manufacturer_form’} )%> Create a form When we press the button Whats updated? Top or bottom of the list?
  39. 39. What does this do? <ul><li>It will submit to ‘create’ th string given by the form </li></ul><ul><li>It will update the manufacturers list which has been given an id tag </li></ul><ul><li>We make sure the new item goes at the bottom of the list </li></ul>
  40. 40. Some More Ajax Examples <ul><li>http://www.writely.com/ </li></ul><ul><li>http://instantdomainsearch.com/ </li></ul><ul><li>http://www.netflix.com/Top100 </li></ul>
  41. 41. More Rails <ul><li>RoR also allows you to quickly generate other tools for your web application </li></ul><ul><li>Uploading and downloading of files can also be achieved int RoR </li></ul><ul><li>Email tools, so you can be contacted from a web form easily </li></ul><ul><li>Use http://api.rubyonrails.org/ for a reference site </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×