Tuesday, 16 August 2011

RAILS



STARTING:


$ rails new app_name
$ cd app_name/
$ bundle install
$ rails server
$ mate Gemfile



GIT:
$ git config --global user.name "Your Name"
$ git config --global user.email youremail@example.com
git config --global alias.co checkout
git config --global core.editor "mate -w"
git init
git add .
git commit -m "Initial commit"
git log
git remote add origin git@github.com:msroot/Karot.git
git push -u origin master
git checkout -b filling-in-layout #B is branch

HEROKU:
[sudo] gem install heroku
sudo gem install taps
heroku keys:add
heroku create
git push heroku master
[sudo] gem install taps
heroku open

rename:
git remote rm heroku
heroku rename karot
git remote -v

GIT AND HEROKU UPDATE COMMANDS:
git add .
git commit -a -m "Done with the demo app"
git push

# heroku create (if u dont create all ready. it will overwite previus)
git push heroku master
heroku rake db:migrate
heroku db:push



RAILS:

----------------------------------------------------------
1. start the server

rails s
----------------------------------------------------------
2. generate user and posts and migrade db

rails generate scaffold User name:string email:string
bundle exec rake db:migrate

rails generate scaffold Micropost content:string user_id:integer
bundle exec rake db:migrate

----------------------------------------------------------
3. A user has_many microposts:

#app/models/user.rb
class User < ActiveRecord::Base has_many :microposts end #app/models/micropost.rb class Micropost < ActiveRecord::Base belongs_to :user validates :content, :length => { :maximum => 140 }
end

----------------------------------------------------------

4. Static pages

rails generate controller Pages home contact



5. add new function to users

rails generate controller Users new
#it know and it added it to users :-\
rails generate migration add_password_to_users encrypted_password:string
bundle exec rake db:migrate


rails generate migration add_salt_to_users salt:string





\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

TIPS:
-----------------------------------------------------------------------------

Rake is Ruby make
when u intalling or updating the system gem: --no-ri --no-rdoc
To see all the Rake tasks available, run: $ bundle exec rake -T
Update DB: bundle exec rake db:migrate
Json format controler:format.html { render :json => @users }


Terminal Development env: rails console #load the development enviroment from terminal
Command: User.first








bundle exec rake db:migrate
bundle exec rake db:rollback


rails new sample_app -T
-T generate TESTS

-----------------------------------------------------------------------------
RAILS CONSOLE:
rails console --sandbox


#rails console
first_user = User.first
first_user.microposts
#control + D to exit


user = User.new(:name => "Michael Hartl", :email => "mhartl@example.com")
user.name
user.save
User.find(1)
User.find_by_email("mhartl@example.com")
User.all
user.update_attributes(:name => "The Dude", :email => "dude@abides.org")
>> user.email
=> "mhartl@example.net"
>> user.email = "foo@bar.com"
=> "foo@bar.com"
>> user.reload.email
=> "mhartl@example.net"
$ rails console --sandbox
>> user = User.new(:name => "", :email => "mhartl@example.com")
>> user.save
=> false
>> user.valid?
=> false
>> user.errors.full_messages
=> ["Name can't be blank"]


rails console
Loading development environment (Rails 3.0.9)
>> Rails.env
=> "development"
>> Rails.env.development?
=> true
>> Rails.env.test?
=> false
#<%= debug(params) if Rails.env.development? %>
rails server --environment production
bundle exec rake db:migrate RAILS_ENV=production

$ heroku console


msRoot:Karot ioannis$
msRoot:Karot ioannis$ heroku console
Ruby console for karot.heroku.com
>> Rails.env
=> "production"





PROGRAMMING:
-----------------------------------------------------------------------------
http://ruby.railstutorial.org/chapters/rails-flavored-ruby

user = { :name => "Michael Hartl", :email => "michael@example.com" }
{:name=>"Michael Hartl", :email=>"michael@example.com"}
user[:name]
"Michael Hartl"

:media => 'screen'
media="screen"



HELPERS:
-----------------------------------------------------------------------------
helps the functionality
application helper its visible to all pages

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

LOGS:
http://pure-robot-759.heroku.com deployed to Heroku

Rename:
URL: http://karot.heroku.com/
Git Repo :git@heroku.com:karot.git

#msRoot:Karot ioannis$ heroku create
#Creating pure-robot-759.... done, stack is bamboo-mri-1.9.2
#http://pure-robot-759.heroku.com/ | git@heroku.com:pure-robot-759.git
#Git remote heroku added
#msRoot:Karot ioannis$


msRoot:Karot ioannis$ heroku rename karot
http://karot.heroku.com/ | git@heroku.com:karot.git
Git remote heroku updated
msRoot:Karot ioannis$








msRoot:Karot ioannis$ rails console
Loading development environment (Rails 3.0.9)
>> User.first
=> #




>> first_user = User.first
=> #
>> first_user.microposts
=> [#, #, #]
>>









>> user = User.new(:name => "Michael Hartl", :email => "mhartl@example.com")
=> #


>> user.save



foo = User.create(:name => "Foo", :email => "foo@bar.com")
=> #
>> foo.destroy
=> #
>>








No comments:

Post a Comment