Ruby On Rails hello world social network

Ruby On Rails Based Social Network


Create a new rails project:
walid$ rails new MeToo

Models

Add models:
walid$ rails generate model User
walid$ rails generate model Post
walid$ rails generate model Message
walid$ rails generate model Comment

Add migrations:
walid$ rails generate migration User
walid$ rails generate migration Post
walid$ rails generate migration Message
walid$ rails generate migration Comment

Modify ./db/migrate/number_comment.rb as the following:
class Comment < ActiveRecord::Migration
   def up
        create_table :comments do |t|
            t.column :id, :Serial, :required => true
            t.column :content, :string, :required => true
            t.column :like, :Integer, :required => true  
            t.column :created_at, :timedate  
            t.column :updated_at, :timedate  
        end
    end
    
    def down
        drop_table :comments
    end
end

Modify ./db/migrate/number_user.rb as the following:
class User < ActiveRecord::Migration
    def up
        create_table :users do |t|
            t.column :username, :string, :required => true, :key => true
            t.column :email, :string, :required => true  
            t.column :password, :string, :required => true
            t.column :photo, :string
            t.column :created_at, :timedate  
            t.column :updated_at, :timedate
        end
    end

    def down
        drop_table :comments
    end
end

Modify ./db/migrate/number_post.rb as the following:
class Post < ActiveRecord::Migration
     def up
          create_table :posts do |t|
               t.column :title, :string
               t.column :content, :string, :required => true  
               t.column :photo, :string, :required => true 
               t.column :archive_path, :string, :required => true  
               t.column :created_at, :timedate  
               t.column :updated_at, :timedate
          end
     end

     def down
          drop_table :posts
     end
end

Modify ./db/migrate/number_message.rb as the following:
class Message < ActiveRecord::Migration
     def up
          create_table :messages do |t|
               t.column :subject, :string
               t.column :content, :string, :required => true  
               t.column :receiver, :string, :required => true  
               t.column :status, :string, :required => true  
               t.column :created_at, :timedate  
               t.column :updated_at, :timedate
          end
      end

     def down
          drop_table :messages
     end
end

Use Rake to apply the migrations to your database
walid$ rake db:migrate

If all goes well, you will get something like this:

==  Comment: migrating ========================================================
-- create_table(:comments)
   -> 0.0009s
==  Comment: migrated (0.0010s) ===============================================
==  User: migrating ===========================================================
-- create_table(:users)
   -> 0.0009s
==  User: migrated (0.0010s) ==================================================
==  Message: migrating ========================================================
-- create_table(:messages)
   -> 0.0008s
==  Message: migrated (0.0009s) ===============================================
==  Post: migrating ===========================================================
-- create_table(:posts)
   -> 0.0010s
==  Post: migrated (0.0011s) ==================================================



Views & Controllers

User

User controller
walid$ rails generate controller User

Modify ./app/controllers/user_controller.rb as the following:

class UserController < ApplicationController
      def new_user
            newuser=User.new
            newuser.username=params[:username]
            newuser.email=params[:email]
            newuser.password=params[:password]
            newuser.photo=params[:photo]
            newuser.save
            redirect_to :action => :index
      end
end

Modify ./app/views/user/index.html.rb as the following:

<h2>Signup</h2>
       <%= form_tag( { :action => :new_user, }, { :method => :post }) do %>
       Username: <%= text_field_tag "username" %>
       <br>
       Email: <%= text_field_tag "email" %>
       <br>
       Password: <%= password_field_tag "password"%>
       <br>
       Photo: <%= text_field_tag "photo"%>
       <%= submit_tag 'Signup' %>
<% end %>


Add the following to ./config/routes.rb

get "user/index"
post "user/new_user"
resources :user





No comments:

Post a Comment