create new rails app: rails new edu -d mysql
git git init
git add . git commit -m ‘first commit’ git remote add origin
create git repo at github git push -u origin master
Create Databases rake db:create:all may need to edit database.yml file for user and password
Start server rails server
Gemfile gemfile - ensure it has jquery, sass, and maybe haml, comment coffee gem, added nokogiri, don’t need pg bundle install
Resource Model rails g scaffold Resource title_manual:text title_scraped:text raw_url:string clean_url:string description:text keywords_manual:text keywords_scraped:text user_id:integer .model name will be singular, table name will be plural
invoke active_record create db/migrate/20121028173855_create_resources.rb create app/models/resource.rb invoke test_unit create test/unit/resource_test.rb create test/fixtures/resources.yml invoke resource_route
route resources :resources
invoke scaffold_controller create app/controllers/resources_controller.rb invoke erb create app/views/resources create app/views/resources/index.html.erb create app/views/resources/edit.html.erb create app/views/resources/show.html.erb create app/views/resources/new.html.erb create app/views/resources/_form.html.erb invoke test_unit create test/functional/resources_controller_test.rb invoke helper create app/helpers/resources_helper.rb invoke test_unit create test/unit/helpers/resources_helper_test.rb invoke assets invoke js create app/assets/javascripts/resources.js invoke scss create app/assets/stylesheets/resources.css.scss invoke scss create app/assets/stylesheets/scaffolds.css.scss
Routes for root .added to routes: root :to => ‘resources#index’ .renamed public/index.html to public/indexOLD.html
-
make user model, including authentication and cookies
.railscast 250 .rails g controller users new .rails g model user email:string password_hash:string password_salt:string .rake db:migrate .rails g controller sessions new
-
update authentication with revised 250
-
railscasts 270 on additional authentication stuff
-
railscasts 274 on additional authentication stuff, remember me, reset password
.rails g migration add_auth_token_to_users auth_token:string .rails g controller password_resets new .rails g migration add_password_reset_to_users password_reset_token:string password_reset_sent_at:datetime .rails g mailer user_mailer password_reset
-
rake task to add auth tokens, railscasts.com/episodes/66-custom-rake-tasks
-
put it on heroku
.bundle exec rake assets:precompile .heroku create .git push heroku master .heroku rename <name> .heroku run rake db:migrate .heroku db:push
-
add collection model
.rails g scaffold Collection user_id:integer, title:string, description:text, keywords:text
-
add collectionize model for join table through
.rails g model collectionize collection_id:integer resource_id:integer order:integer notes:text
-
change to mysql
.database.yml change .migrate .heroku db:pull
Migrate database rake db:migrate
– create_table(:resources)
-> 0.0112s
Rename columns from ‘scraped’ to ‘sourced’, add ‘description_sourced’ rails g migration ChangeResource
invoke active_record create db/migrate/20121028180957_change_resource.rb
.update migration file .add new fields to attr_accessible line in resource.rb model .made mistake first time, then rake db:rollback so I can add :raw_html and remove :clean_url column, then migrate again rake db:migrate
– add_column(:resources, :description_from_source, :text)
-> 0.0166s
– add_column(:resources, :raw_html, :text)
-> 0.0133s
– remove_column(:resources, :clean_url)
-> 0.0185s
– rename_column(:resources, :title_manual, :title_from_user)
-> 0.0441s
– rename_column(:resources, :title_scraped, :title_from_source)
-> 0.0295s
– rename_column(:resources, :description, :description_from_user)
-> 0.0186s
– rename_column(:resources, :keywords_manual, :keywords_from_user)
-> 0.0211s
– rename_column(:resources, :keywords_scraped, :keywords_from_source)
-> 0.0172s
Add authentication railscasts.com/episodes/250-authentication-from-scratch-revised rails g resource user email password_digest .a resource generator is different from scaffold generator in that it doesn’t create all the controller actions
invoke active_record create db/migrate/20121028222628_create_users.rb create app/models/user.rb invoke test_unit create test/unit/user_test.rb create test/fixtures/users.yml invoke controller create app/controllers/users_controller.rb invoke erb create app/views/users invoke test_unit create test/functional/users_controller_test.rb invoke helper create app/helpers/users_helper.rb invoke test_unit create test/unit/helpers/users_helper_test.rb invoke assets invoke js create app/assets/javascripts/users.js invoke scss create app/assets/stylesheets/users.css.scss invoke resource_route
route resources :users
edit model user.rb uncomment bcrypt-ruby gem in gemfile edit users_controller.rb rails g controller sessions new
create app/controllers/sessions_controller.rb
route get "sessions/new"
invoke erb create app/views/sessions create app/views/sessions/new.html.erb invoke test_unit create test/functional/sessions_controller_test.rb invoke helper create app/helpers/sessions_helper.rb invoke test_unit create test/unit/helpers/sessions_helper_test.rb invoke assets invoke js create app/assets/javascripts/sessions.js invoke scss create app/assets/stylesheets/sessions.css.scss
edit routes to add <resources :sessions> and get rid of the get request
cookies and remember me railscasts.com/episodes/274-remember-me-reset-password rails g migration add_auth_token_to_users auth_token:string
invoke active_record create db/migrate/20121029123127_add_auth_token_to_users.rb
rake db:migrate edit some files
reset password railscasts.com/episodes/274-remember-me-reset-password rails g controller password_resets new
create app/controllers/password_resets_controller.rb
route get "password_resets/new"
invoke erb create app/views/password_resets create app/views/password_resets/new.html.erb invoke test_unit create test/functional/password_resets_controller_test.rb invoke helper create app/helpers/password_resets_helper.rb invoke test_unit create test/unit/helpers/password_resets_helper_test.rb invoke assets invoke js create app/assets/javascripts/password_resets.js invoke scss create app/assets/stylesheets/password_resets.css.scss
edit model user.rb edit password_resets_controller.rb rails g migration add_password_reset_to_users password_reset_token:string password_reset_sent_at:datetime
invoke active_record create db/migrate/20121029132244_add_password_reset_to_users.rb
rake db:migrate rails g mailer user_mailer password_reset
create app/mailers/user_mailer.rb invoke erb create app/views/user_mailer create app/views/user_mailer/password_reset.text.erb invoke test_unit create test/functional/user_mailer_test.rb
restart the server finish editing other files
heroku add gem ‘pg’ to gemfile add to /etc/my.cnf this [mysqld] max_allowed_packet = 32M heroku create
Creating warm-forest-3395… done, stack is cedar warm-forest-3395.herokuapp.com/ | git@heroku.com:warm-forest-3395.git Git remote heroku added
git push heroku master .heroku receiving push… heroku rename bedu heroku run rake db:migrate heroku db:push
Add Review Model rails g scaffold Review user_id:integer title:text body:text score:integer resource_id:integer
invoke active_record create db/migrate/20121229032421_create_reviews.rb create app/models/review.rb invoke test_unit create test/unit/review_test.rb create test/fixtures/reviews.yml invoke resource_route route resources :reviews invoke scaffold_controller create app/controllers/reviews_controller.rb invoke erb create app/views/reviews create app/views/reviews/index.html.erb create app/views/reviews/edit.html.erb create app/views/reviews/show.html.erb create app/views/reviews/new.html.erb create app/views/reviews/_form.html.erb invoke test_unit create test/functional/reviews_controller_test.rb invoke helper create app/helpers/reviews_helper.rb invoke test_unit create test/unit/helpers/reviews_helper_test.rb invoke assets invoke js create app/assets/javascripts/reviews.js invoke scss create app/assets/stylesheets/reviews.css.scss invoke scss
identical app/assets/stylesheets/scaffolds.css.scss
rake db:migrate
– create_table(:reviews)
-> 0.2907s
add to associations: to resource model - has_many :reviews to reviews model - belongs_to :resource git commit this
# search works, regular and AJAX railscasts.com/episodes/343-full-text-search-in-postgresql railscasts.com/episodes/240-search-sort-paginate-with-ajax railscasts.com/episodes/111-advanced-search-form-revised
# sorted resources#index with this => @resources = Resource.order(“id desc”)
# update heroku heroku run rake db:migrate heroku db:pull
# add provider rails g migration add_provider_to_resource provider:text
invoke active_record create db/migrate/20130120223732_add_provider_to_resource.rb
edit file to add down for rollback rake db:migrate
– add_column(:resources, :provider, :text)
-> 0.2048s
add provider field to resource form
# rake task to add providers railscasts.com/episodes/66-custom-rake-tasks add lib/tasks/add_providers.rake heroku run rake addproviderstorailscasts
# auto focus on search at home
-
edit application.js file
github.com/jaredcohe/edu/commit/0c6e979184d953f117221b9cc8a86e4180671e97
# search button and text field bigger, remove header image github.com/jaredcohe/edu/commit/30378c66ca36b9c08ebb4dd1fe7570b327c0d47f
# add clickable to click on each resource to get to the resource page github.com/jaredcohe/edu/commit/c4248a070bc145412feb025a60e85b39983f446a
# add reviews to resource show page github.com/jaredcohe/edu/commit/2115f29c74994e2dbd6c8919a0bca0a039d376ca
# add resource form for button on homepage github.com/jaredcohe/edu/commit/43fd5da774228f5bf72a22d2c3adadcbd25af0f1
# clickable callback so it still works after ajax search github.com/jaredcohe/edu/commit/75560f5cc3f2b9cde45db5f35a3a1c89737ede6c
# add to resource boxes a link to go to the external resource with target blank, add number of reviews, add average score
# curl for logging out Started DELETE “/logout” for 127.0.0.1 at 2013-02-10 10:59:23 -0500 Processing by SessionsController#destroy as HTML
Parameters: {"authenticity_token"=>"s/WMnHVLmKlz/+PQa2X5Yv9KfU7LvsRsRcx69LECAqA="}
xxx
# style the resources edit page .admin v reg .reg cant edit scraped data .can edit only if youre the creator or an admin
# style the reviews .add explanation to scores …useless, met expectations, Gift from Above
# sort homepage by score and number of reviews, whether it has providers and description, then by age
# add todo and done user_resource rails g scaffold UserResource user_id:integer, resource_id:integer, status:integer status (todo, done, inprogress)
# viewings table, if user has visited, gray out the tutorial # put a mark on the ones that are syllabi # menu on mouseover card to add to-do # get rid of search button? # add admin signoff on each resource, integer for stage # search should find substring in URL, and provider # add other URLs table to avoid dupes …resource_id, url # add live validation for url in create resource and other places # add link to email that makes the user confirm - “change a field in the database to show the person is confirmed” - end of railscasts 274, that code can be used for this # BML
fix tagging railscasts.com/episodes/382-tagging
# improve search .order should be better .search of “r” and “R” should return R programming and not everything with an R # add ability to drag and drop collections
heroku custom domain heroku addons:add custom_domains heroku domains:add <name such as www.disclaim.in> heroku domains:add <name such as disclaim.in> go to host and add to A names under DNS management: 75.101.163.44 75.101.145.87 174.129.212.2 devcenter.heroku.com/articles/custom-domains to destroy database on internet, go to directory and: <heroku db:push> to push: git push heroku before pushing to heroku run: bundle exec rake assets:precompile
#fix coursera scraping issue .wont scrape coursera .class.coursera.org/compilers-2012-002/class/index #class.coursera.org/compilers-2012-002/auth/welcome?type=logout&visiting=%2Fcompilers-2012-002%2Fclass%2Findex
#User can create collections by putting in a URL or searching Resources railscasts.com/episodes/196-nested-model-form-revised
Collection Model rails g scaffold Collection user_id:integer title:text description:text keywords:text
has_many :collectionizes, :dependent => :destroy has_many :resources, :through => :collectionizes, :foreign_key => :resource_id
accepts_nested_attributes_for :resources, :reject_if => lambda { |a| a.blank? } accepts_nested_attributes_for :collectionizes
Resource Model has_many :collectionizes has_many :collections, :through => :collectionizes, :foreign_key => :collection_id accepts_nested_attributes_for :collectionizes accepts_nested_attributes_for :collections
Collectionize Model resource_id:integer, collection_id:integer, description:text, order:integer belongs_to :collection belongs_to :resource accepts_nested_attributes_for :resource accepts_nested_attributes_for :collection
#add comments to collections, rather than reviews?
Errors, Problems # to fix heroku not seeing css scss files, type this in terminal: RAILS_ENV=production bundle exec rake assets:precompile
Rails is a web-application framework that includes everything needed to create database-backed web applications according to the Model-View-Control pattern.
This pattern splits the view (also called the presentation) into “dumb” templates that are primarily responsible for inserting pre-built data in between HTML tags. The model contains the “smart” domain objects (such as Account, Product, Person, Post) that holds all the business logic and knows how to persist themselves to a database. The controller handles the incoming requests (such as Save New Account, Update Product, Show Post) by manipulating the model and directing data to the view.
In Rails, the model is handled by what’s called an object-relational mapping layer entitled Active Record. This layer allows you to present the data from database rows as objects and embellish these data objects with business logic methods. You can read more about Active Record in files/vendor/rails/activerecord/README.html.
The controller and view are handled by the Action Pack, which handles both layers by its two parts: Action View and Action Controller. These two layers are bundled in a single package due to their heavy interdependence. This is unlike the relationship between the Active Record and Action Pack that is much more separate. Each of these packages can be used independently outside of Rails. You can read more about Action Pack in files/vendor/rails/actionpack/README.html.
-
At the command prompt, create a new Rails application:
<tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
-
Change directory to
myappand start the web server:<tt>cd myapp; rails server</tt> (run with --help for options)
-
Go to localhost:3000/ and you’ll see:
"Welcome aboard: You're riding Ruby on Rails!" -
Follow the guidelines to start developing your application. You can find
the following resources handy:
-
The Getting Started Guide: guides.rubyonrails.org/getting_started.html
-
Ruby on Rails Tutorial Book: www.railstutorial.org/
Sometimes your application goes wrong. Fortunately there are a lot of tools that will help you debug it and get it back on the rails.
First area to check is the application log files. Have “tail -f” commands running on the server.log and development.log. Rails will automatically display debugging and runtime information to these files. Debugging info will also be shown in the browser on requests from 127.0.0.1.
You can also log your own messages directly into the log file from your code using the Ruby logger class from inside your controllers. Example:
class WeblogController < ActionController::Base def destroy @weblog = Weblog.find(params[:id]) @weblog.destroy logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!") end end
The result will be a message in your log file along the lines of:
Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
More information on how to use the logger is at www.ruby-doc.org/core/
Also, Ruby documentation can be found at www.ruby-lang.org/. There are several books available online as well:
-
Programming Ruby: www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
-
Learn to Program: pine.fm/LearnToProgram/ (a beginners guide)
These two books will bring you up to speed on the Ruby language and also on programming in general.
Debugger support is available through the debugger command when you start your Mongrel or WEBrick server with –debugger. This means that you can break out of execution at any point in the code, investigate and change the model, and then, resume execution! You need to install ruby-debug to run the server in debugging mode. With gems, use sudo gem install ruby-debug. Example:
class WeblogController < ActionController::Base def index @posts = Post.all debugger end end
So the controller will accept the action, run the first line, then present you with a IRB prompt in the server window. Here you can do things like:
>> @posts.inspect
=> "[#<Post:0x14a6be8
@attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
#<Post:0x14a6620
@attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
>> @posts.first.title = "hello from a debugger"
=> "hello from a debugger"
…and even better, you can examine how your runtime objects actually work:
>> f = @posts.first
=> #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
>> f.
Display all 152 possibilities? (y or n)
Finally, when you’re ready to resume execution, you can enter “cont”.
The console is a Ruby shell, which allows you to interact with your application’s domain model. Here you’ll have all parts of the application configured, just like it is when the application is running. You can inspect domain models, change values, and save to the database. Starting the script without arguments will launch it in the development environment.
To start the console, run rails console from the application directory.
Options:
-
Passing the
-s, --sandboxargument will rollback any modifications made to the database. -
Passing an environment name as an argument will load the corresponding environment. Example:
rails console production.
To reload your controllers and models after launching the console run reload!
More information about irb can be found at: http://www.rubycentral.org/pickaxe/irb.html
You can go to the command line of your database directly through rails dbconsole. You would be connected to the database with the credentials defined in database.yml. Starting the script without arguments will connect you to the development database. Passing an argument will connect you to a different database, like rails dbconsole production. Currently works for MySQL, PostgreSQL and SQLite 3.
The default directory structure of a generated Ruby on Rails application:
|-- app
| |-- assets
| |-- images
| |-- javascripts
| `-- stylesheets
| |-- controllers
| |-- helpers
| |-- mailers
| |-- models
| `-- views
| `-- layouts
|-- config
| |-- environments
| |-- initializers
| `-- locales
|-- db
|-- doc
|-- lib
| `-- tasks
|-- log
|-- public
|-- script
|-- test
| |-- fixtures
| |-- functional
| |-- integration
| |-- performance
| `-- unit
|-- tmp
| |-- cache
| |-- pids
| |-- sessions
| `-- sockets
`-- vendor
|-- assets
`-- stylesheets
`-- plugins
app
Holds all the code that's specific to this particular application.
app/assets
Contains subdirectories for images, stylesheets, and JavaScript files.
app/controllers
Holds controllers that should be named like weblogs_controller.rb for automated URL mapping. All controllers should descend from ApplicationController which itself descends from ActionController::Base.
app/models
Holds models that should be named like post.rb. Models descend from ActiveRecord::Base by default.
app/views
Holds the template files for the view that should be named like weblogs/index.html.erb for the WeblogsController#index action. All views use eRuby syntax by default.
app/views/layouts
Holds the template files for layouts to be used with views. This models the common header/footer method of wrapping views. In your views, define a layout using the <tt>layout :default</tt> and create a file named default.html.erb. Inside default.html.erb, call <% yield %> to render the view using this layout.
app/helpers
Holds view helpers that should be named like weblogs_helper.rb. These are generated for you automatically when using generators for controllers. Helpers can be used to wrap functionality for your views into methods.
config
Configuration files for the Rails environment, the routing map, the database, and other dependencies.
db
Contains the database schema in schema.rb. db/migrate contains all the sequence of Migrations for your schema.
doc
This directory is where your application documentation will be stored when generated using <tt>rake doc:app</tt>
lib
Application specific libraries. Basically, any kind of custom code that doesn't belong under controllers, models, or helpers. This directory is in the load path.
public
The directory available for the web server. Also contains the dispatchers and the default HTML files. This should be set as the DOCUMENT_ROOT of your web server.
script
Helper scripts for automation and generation.
test
Unit and functional tests along with fixtures. When using the rails generate command, template test files will be generated for you and placed in this directory.
vendor
External libraries that the application depends on. Also includes the plugins subdirectory. If the app has frozen rails, those gems also go here, under vendor/rails/. This directory is in the load path.