Last active
December 25, 2017 06:44
-
-
Save uriklar/2834a8bb56a0cd998dc55747dcd05017 to your computer and use it in GitHub Desktop.
Create fakebook app with models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From your root folder (C:\Sites ?), write the following commands: | |
rails new fakebook (Creates the fakebook app) | |
cd fakebook (Enters the fakebook folder) | |
rails g model User first_name last_name email password (Creates the User model file + migration to create users table) | |
rake db:migrate (Runs the migration and creates the users table) | |
rails g model Post text user_id:integer (Creates the Post model + migration to create posts table) | |
rake db:migrate (Runs the migration and creates the posts table) | |
rails g controller Users (creates the user's controller) | |
rails g controller Posts (creates the posts controller) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/post.rb | |
class Post < ActiveRecord::Base | |
belongs_to :user | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/routes.rb | |
Rails.application.routes.draw do | |
resources :users do | |
resources :posts | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/user.rb | |
class User < ActiveRecord::Base | |
has_many :posts | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/users_controller.rb | |
class UsersController < ApplicationController | |
# GET /users | |
def index | |
@users = User.all | |
render json: @users | |
end | |
# GET /users/1 | |
def show | |
@user = User.find(params[:id]) | |
render json: @user | |
end | |
# POST /users | |
def create | |
@user = User.new(user_params) | |
if @user.save | |
render json: @user | |
else | |
render json: @user.errors | |
end | |
end | |
# PATCH/PUT /users/1 | |
def update | |
@user = User.find(params[:id]) | |
if @user.update(user_params) | |
render json: @user | |
else | |
render json: @user.errors | |
end | |
end | |
# DELETE /users/1 | |
def destroy | |
@user = User.find(params[:id]) | |
@user.destroy | |
render json: @user | |
end | |
private | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def user_params | |
params.require(:user).permit(:first_name, :last_name, :email, :password) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment