Posts Tagged “Ruby”

Ruby on Rails: JavaScript

Posted on May 5th, 2015 by webninjataylor

Sample CoffeeScript AJAX request… //This code fetches data from “/test”, and then appends the result to the div with an id of results… $.ajax(url: “/test”).done (html) -> $(“#results”).append html Unobtrusive JavaScript is separating the script from the HTML markup Built-in helper methods form_for and form_tag … helps in writing forms // Remote submits the form via […]

Tags:
Posted in Web ShotsComments Off on Ruby on Rails: JavaScript

Ruby on Rails: Associations

Posted on May 5th, 2015 by webninjataylor

Associations between models make common operations simpler and easier class Customer < ActiveRecord::Base has_many :orders, dependent: :destroy end class Order < ActiveRecord::Base belongs_to :customer end By declaring that one model belongs_to another, you instruct Rails to maintain Primary Key-Foreign Key information between instances of the two models, and you also get a number of utility […]

Tags:
Posted in Web ShotsComments Off on Ruby on Rails: Associations

Ruby on Rails: Validations

Posted on May 4th, 2015 by webninjataylor

Validations are used to ensure that only valid data is saved into your database Model-level validations are the best way They are database agnostic, cannot be bypassed by end users, and are convenient to test and maintain If any validations fail, the object will be marked as invalid and Active Record will not perform the […]

Tags:
Posted in Web ShotsComments Off on Ruby on Rails: Validations

Ruby on Rails: Layouts and Rendering

Posted on May 4th, 2015 by webninjataylor

By default, controllers in Rails automatically render views with names that correspond to valid routes The rule is that if you do not explicitly render something at the end of a controller action, Rails will automatically look for the action_name.html.erb template in the controller’s view path and render it If you want to render the […]

Tags:
Posted in Web ShotsComments Off on Ruby on Rails: Layouts and Rendering

Ruby on Rails: Routing

Posted on May 1st, 2015 by webninjataylor

Routing connects URLs to code and generates paths from code // routes.rb … e.g. directs a call of /patients/17 to the show action (method) in the patients controller with a parameter of {id: ’17’} … the ‘as’ option names the route, creating the patient_path and patient_url helper methods get ‘/patients/:id’, to: ‘patients#show’, as: ‘patient’ // […]

Tags:
Posted in Web ShotsComments Off on Ruby on Rails: Routing

Ruby on Rails: Suggested Project Flow

Posted on April 26th, 2015 by webninjataylor

Sketch Use Cases, Page Flow, & Basic Data Identify basic use cases, sketch page flow on paper (or simple drawing app), and identify basic data (e.g. product which will have a name, description, image, and price) Ambiguity is ok at this stage since Ruby on Rails is great for agile, iterative development Pick a part of […]

Tags:
Posted in Web ShotsComments Off on Ruby on Rails: Suggested Project Flow

Ruby on Rails: Relationships, Nested Resources, and AJAX in Rails

Posted on April 22nd, 2015 by webninjataylor

Most common relationship types for models One-to-one One-to-many (parent/child relationships) The parent has many children The children belong to a parent Many-to-many A book has many authors An author has many books Create model in the middle so info can be reused A doctor has many patients through appointments Use the resource generator to generate […]

Tags:
Posted in Web ShotsComments Off on Ruby on Rails: Relationships, Nested Resources, and AJAX in Rails

Ruby on Rails: Validation, Queries, Layouts, and Styling in Rails

Posted on April 15th, 2015 by webninjataylor

You can also validate any object, not just ActiveRecord objects Declare rules using validation helpers (e.g. validates :title, presence: true … which validates that the title is there) Lifecycle methods – save, update, and create “Bang” method variants raise exception if object is not valid (e.g. save!, create!, update!) include ActiveModel::Validations Validations populate the errors object […]

Tags:
Posted in Web ShotsComments Off on Ruby on Rails: Validation, Queries, Layouts, and Styling in Rails

Ruby on Rails: Testing, Configuration, and Forms in Rails

Posted on April 8th, 2015 by webninjataylor

There’s a default testing framework in Ruby 1.9+ Borrows from Test::Unit and RSpec Choose one or the other to write all your tests in; don’t use both … be consistent Rails provides testing support out of the box Test runner (via Rake) Testing models against the database using fixtures Functional testing controllers (e.g. that the create […]

Tags:
Posted in Web ShotsComments Off on Ruby on Rails: Testing, Configuration, and Forms in Rails

Ruby on Rails: Introduction to Ruby on Rails

Posted on April 8th, 2015 by webninjataylor

rails -v to check version Rails takes MVC further: when you develop in Rails, you start with a working application, there’s a place for each piece of code, and all the pieces of your application interact in a standard way Once you create an application with a specific version of Rails, it will continue to use […]

Tags:
Posted in Web ShotsComments Off on Ruby on Rails: Introduction to Ruby on Rails

Ruby on Rails: Introduction to Ruby

Posted on April 8th, 2015 by webninjataylor

ruby -v to check version Everything in Ruby is an object You don’t declare variable types, but all objects have a type No compiler, so unit testing is crucial No auto type conversion … “3” + 4 results in a TypeError Created in Japan in 1995 by Yukihiro Matsumoto “Mats” Inspired by Pearl and Smalltalk […]

Tags:
Posted in Web ShotsComments Off on Ruby on Rails: Introduction to Ruby