rails new feisbuk
Create a bunch of files! Yo!
create
create README.rdoc
create Rakefile
create config.ru
create .gitignore
create Gemfile
create app
create app/assets/images/rails.png
create app/assets/javascripts/application.js
create app/assets/stylesheets/application.css
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create app/views/layouts/application.html.erb
create app/mailers/.gitkeep
create app/models/.gitkeep
create config
create config/routes.rb
create config/application.rb
create config/environment.rb
create config/environments
create config/environments/development.rb
create config/environments/production.rb
create config/environments/test.rb
create config/initializers
create config/initializers/backtrace_silencers.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/secret_token.rb
create config/initializers/session_store.rb
create config/initializers/wrap_parameters.rb
create config/locales
create config/locales/en.yml
create config/boot.rb
create config/database.yml
create db
create db/seeds.rb
create doc
create doc/README_FOR_APP
create lib
create lib/tasks
create lib/tasks/.gitkeep
create lib/assets
create lib/assets/.gitkeep
create log
create log/.gitkeep
create public
create public/404.html
create public/422.html
create public/500.html
create public/favicon.ico
create public/index.html
create public/robots.txt
create script
create script/rails
create test/fixtures
create test/fixtures/.gitkeep
create test/functional
create test/functional/.gitkeep
create test/integration
create test/integration/.gitkeep
create test/unit
create test/unit/.gitkeep
create test/performance/browsing_test.rb
create test/test_helper.rb
create tmp/cache
create tmp/cache/assets
create vendor/assets/javascripts
create vendor/assets/javascripts/.gitkeep
create vendor/assets/stylesheets
create vendor/assets/stylesheets/.gitkeep
create vendor/plugins
create vendor/plugins/.gitkeep
run bundle install
cd feisbuk/
view all files and directories with tree command on unix! enjoy! :D [ tree . ] View, Models, Controllers, Routes and Gemfile
Maybe you need some Js library like JavaScript runtime [ExecJS::RuntimeUnavailable] Edit Gemfile and uncomment gem 'therubyracer', :platforms => :ruby
sublime . & [ sublime and command info ]
gem 'gravtastic' gem 'bcrypt-ruby', '~> 3.0.0'
bundle install
rails server
On Firefox, http://localhost:3000. You will see the index page. Find it on [ public folder ] and remove it.
rm public/index.html
--> SWITCH TO SLIDES [ USERS ] <--
rails g scaffold User username:string email:string password_digest:string
Generate these lines:
invoke active_record create db/migrate/20130219165217_create_users.rb create app/models/user.rb invoke test_unit create test/unit/user_test.rb create test/fixtures/users.yml invoke resource_route route resources :users invoke scaffold_controller create app/controllers/users_controller.rb invoke erb create app/views/users create app/views/users/index.html.erb create app/views/users/edit.html.erb create app/views/users/show.html.erb create app/views/users/new.html.erb create app/views/users/_form.html.erb 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 coffee create app/assets/javascripts/users.js.coffee invoke scss create app/assets/stylesheets/users.css.scss invoke scss create app/assets/stylesheets/scaffolds.css.scss
What is active_record ? [ Switch to slides ]
rake db:migrate
Watch MVC files..
root :to => 'users#index'
Add some bootstrap style or gangnam style of course! :) You can do it adding the 'twitter-bootstrap-rails' gem in Gemfile or copy and paste bootstrap's files in twit/app/assets subdirectories! It's better! :D
gem "less-rails" gem "twitter-bootstrap-rails"
bundle install
rails generate bootstrap:install less rails g bootstrap:layout application fixed -> rails g bootstrap:layout [LAYOUT_NAME] [*fixed or fluid] rails g bootstrap:themed Users
pull-right on nav
*= require bootstrap_and_overrides body { padding-top: 50px; }
@linkColor: #ff0000; [ remove // ] it works! :) @navbarBackgroundHighlight: #0000ff; @navbarBackground: #0000ff; @navbarText: #ffffff; @navbarLinkColor: #ffffff;
Show a bit of Rails console.. [ Switch to terminal ] Add User.
As you can see, email and username could be empty..
add some validates to user model:
include Gravtastic
before_validation :prep_email
validates :username, :email, :password_digest, :presence => true validates :username, :uniqueness => true, :length => { minimum: 3, maximum: 18 } validates :email, :uniqueness => true, :format => { with: /^[\w.+-]+@([\w]+.)+\w+$/ }
has_secure_password has_gravatar :secure => true, :filetype => :png, :size => 120
private
def prep_email
self.email = self.email.strip.downcase if self.email
end
Change _form.html.erb in views/users folder from
to
attr_accessible :password, :password_confirmation [ for MassAssignmentSecurity ]
Add errors popups under form initialization
<% if @user.errors.any? %>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Try now to update user/new page and insert uncorrect values: Urg! We must immediately add the style for errors! unwatchable! O.o
rm app/assets/stylesheets/scaffold.css.scss
class="alert alert-error"
Try to create a new profile with this e-mail address: [email protected] add <%= image_tag user.gravatar_url %> on views aaaaaand Uhuh! Gravtastic works like a charm! :D
--> SWITCH TO SLIDES [ POSTS ] <--
Now add the posts feature!
rails g scaffold Post body:text
Generate these lines:
invoke active_record create db/migrate/20130219211339_create_posts.rb create app/models/post.rb invoke test_unit create test/unit/post_test.rb create test/fixtures/posts.yml invoke resource_route route resources :posts invoke scaffold_controller create app/controllers/posts_controller.rb invoke erb create app/views/posts create app/views/posts/index.html.erb create app/views/posts/edit.html.erb create app/views/posts/show.html.erb create app/views/posts/new.html.erb create app/views/posts/_form.html.erb invoke test_unit create test/functional/posts_controller_test.rb invoke helper create app/helpers/posts_helper.rb invoke test_unit create test/unit/helpers/posts_helper_test.rb invoke assets invoke coffee create app/assets/javascripts/posts.js.coffee invoke scss create app/assets/stylesheets/posts.css.scss invoke scss create app/assets/stylesheets/scaffolds.css.scss
rake db:migrate
add some validations in Post model like this validates :body, :presence => true, :length => { minimum: 1, maximum: 140 }
Create some posts.
Important: add all the neccessary to make the relationship between users and posts..
--> SWITCH TO SLIDES [ USER'S POSTS ] <--
has_many :posts in user model belongs_to :user in post model
add related attr_accessible: :posts in users :user_id, :created_at in posts
Add the user_id attribute to the model with rails generate migration command: rails generate migration add_user_id_to_posts user_id:integer rake db:migrate
--> SWITCH TO SLIDES [ NESTED ROUTES ] <--
http://localhost:3000/users/1/posts
Define a new private method..
private def get_user @user = User.find(params[:user_id]) end
Add before_filter: get_user on top of the controller file.
Remember to change ALL Post with @user.posts for get all user's posts obviously!
In create method change first line with this: @post = @user.posts.build(params[:post]) redirect_to user_posts_path(@user)
Change all links refer to http://localhost:3000/posts
Ok, add style to pages...
© Company 2012
<%- model_class = User -%>
- <%= link_to "Show written posts", user_posts_path(@user), :class => "btn-link" %>
- <%= link_to "Settings", edit_user_path(@user) %>
- Logout
<%= button_to 'Edit', edit_user_path(@user), method: :get, :class => "btn btn-primary" %> <%= button_to 'Back', users_path, method: :get, :class => "btn btn-primary"%> <%= button_to 'Write post', new_user_post_path(@user), method: :get, :class => "btn btn-primary"%>
section {
background-color: #CED1F8; color: #444; display: block; outline: medium none; margin: auto; width: 940px; margin-top: 40px; box-shadow: 0px 0px 3px #222; position: relative;
#userInfoGlob { text-shadow: 0px 1px 1px #000; position: absolute; top: 10px; left: 240px; }
#userInfoMail { position: absolute; top: 55px; left: 240px; }
}
section:first-child { margin-top: 140px; padding-left: 30px; padding-right: 30px; }
#userImageDiv { width: 178px; height: 178px; position: absolute; top: -100px; left: 30px; background: #FFF; border: 1px solid #222; padding: 5px 4px; box-shadow: 0px 0px 2px #222; }
#userImageDiv>img { vertical-align: middle; text-align: center; width: 176px; height: 176px; border: 1px solid #222; text-decoration: none; text-transform: none; }
#userConfig { position: absolute; top: 20px; right: 20px; }
<%= form_for([@user,@post]) do |f| %> <% if @post.errors.any? %>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.text_area :body %>
<%= f.datetime_select :created_at %>
.singlePost { padding: 10px; background-color: #e4e6fb; border: 1px solid #a2aaf0; border-radius: 6px; }
.postBody { word-wrap:break-word; width: 99%; height: 100px; }
.datetimeHidden { display: none; }
<% if notice%>
<%= notice %>
<% end %> <% if flash[:error] %><%= flash[:error] %>
<% end %><% @user.posts.each do |post| %>
<% end %>Id | Content | |||
---|---|---|---|---|
<%= post.id %> | <%= raw truncate( post.body, :length => 250, :omission => (link_to ' ..Continued', user_post_path(post.user_id, post), :class => "btn-link" ) ) %> | <%= link_to raw(''), user_post_path(post.user_id, post), :class => "btn btn-success",:title => "Show" %> | <%= link_to raw(''), edit_user_post_path(post.user_id, post), :class => "btn btn-primary",:title => "Edit" %> | <%= link_to raw(''), user_post_path(post.user_id, post), :class => "btn btn-danger",:title => "Delete", method: :delete, data: { confirm: 'Are you sure?' } %> |