才华横溢 wiki 案例教学

才华横溢 wiki 案例教学 9

Github Repo: https://github.com/shenzhoudance/wiki Visit this website: https://xiaoweiwiki.herokuapp.com/

cd workspace
rails new wiki
cd wiki
git init
git add .
git commit -m "initial commit"
git remote add origin https://github.com/shenzhoudance/wiki.git
git push -u origin master
rails server

image

git checkout -b Gemfile_gem
https://rubygems.org/
gem 'haml', '~> 5.0', '>= 5.0.4'
gem 'bootstrap-sass', '~> 3.3', '>= 3.3.7'
gem 'simple_form', '~> 3.5', '>= 3.5.1'
gem 'devise', '~> 4.4', '>= 4.4.3'
bundle install
---
git add .
git commit -m "Add Application Gems"
git push origin Gemfile_gem

image image image image

git checkout -b model_article
rails g model Article title:string content:text
rake db:migrate
---
git add .
git commit -m "Add model_article"
git push origin model_article

image

git checkout -b controller_articles
rails g controller Articles
---

image

config/routes.rb
---
Rails.application.routes.draw do
 resources :articles
 root 'articles#index'
end
---
rails server

image

rails server

image

app/controllers/articles_controller.rb
---
def index
end
---
app/views/articles/index.html.haml
---
%h1 欢迎来到才华横溢的世界
---

image

git add .
git commit -m "add crud index"
git push origin controller_articles
---
rails generate simple_form:install --bootstrap
---
app/controllers/articles_controller.rb
---
class ArticlesController < ApplicationController
def index
end

def new
  @article = Article.new
end

def create
  @article = Article.new(artcle.params)
  if @article.save
    redirect_to @article
  else
    render 'new'
  end
end

private

def artcle_params
  params.require(:article).permit(:title, :content)
 end
end
---
app/views/articles/_form.html.haml
---
= simple_form_for @article do |f|
 f.input :title
 f.input :cintent
 f.submit
---
app/views/articles/new.html.haml
---
%h1 New Article

= render 'form'

= link_to 'back', root_path
---
rails server

image

app/views/articles/_form.html.haml
---
= simple_form_for @article do |f|
 = f.input :title
 = f.input :content
 = f.submit
---

image image image

app/controllers/articles_controller.rb
---
class ArticlesController < ApplicationController
before_action :find_article, only:[:show]

def index
end

def show
end

def new
  @article = Article.new
end

def create
  @article = Article.new(article_params)
  if @article.save
    redirect_to @article
  else
    render 'new'
  end
end

private
def find_article
  @article = Article.find(params[:id])
end

def article_params
  params.require(:article).permit(:title, :content)
 end
end
---
app/views/articles/show.html.haml
---
%h1= @article.title
%p= @article.content

.btn-group
    = link_to "Back", root_path, class: "btn btn-default"
    = link_to "Edit", edit_article_path(@article), class: "btn btn-default"
    = link_to "Delete", article_path(@article), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-default"

image

app/controllers/articles_controller.rb
---
def index
  @articles = Article.all.order("created_at DESC")
end
---
app/views/articles/index.html.haml
---
%h1 欢迎来到才华横溢的世界

- @articles.each do |article|
    %h2= link_to article.title, article
    %p
        Published at
        = article.created_at.strftime('%b %d, %Y')

= link_to "New Article", new_article_path
---

image


git checkout -b devise
rails generate devise:install
rails g devise:views
rails g devise User
rake db:migrate
rails server
http://localhost:3000/users/sign_up

image

git checkout -b devise2
rails c
User
User.connection
User.count
User.first
exit
---
git status
git add .
git commit -m "add devise and generate user model"

image image image

git checkout -b relation
rails g migration add_user_id_to_articles user_id:integer:index
---
app/models/article.rb
---
class Article < ApplicationRecord
  belongs_to :user
end
---
app/models/user.rb
---
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :articles
end
---
rake db:migrate

image image

app/controllers/articles_controller.rb
---
def new
  @article = current_user.articles.build
end

def create
  @article = current_user.articles.build(article_params)
  if @article.save
    redirect_to @article
  else
    render 'new'
  end
end
rails c
2.3.1 :001 > @article = Articles.last
2.3.1 :002 > @article = Article.last
2.3.1 :003 > @article = Article.first
2.3.1 :004 > @article.user_id = 1
2.3.1 :005 > @article.save
2.3.1 :006 > @article = Article.find(2)
2.3.1 :007 > @article.user_id = 1
2.3.1 :008 > @article.save
2.3.1 :009 > exit
---
git status
git add .
git commit -m "add association between user and article"
git push origin relation

image image image

app/controllers/articles_controller.rb
---
before_action :find_article, only:[:show]
before_action :authenticate_user!, except: [:index, :show]
---
app/views/articles/index.html.haml
---
- if user_signed_in?
    = link_to "New Article", new_article_path
---
rails server
http://localhost:3000/
---
git status
git add .
git commit -m "authenticate_user"
git push origin relation

image image image

git checkout -b category
rails g model Category name:string
rake db:migrate
rails g migration add_category_id_to_articles category_id:integer
rake db:migrate

image image

app/models/article.rb
---
class Article < ApplicationRecord
  belongs_to :user
  belongs_to :category
end
app/models/category.rb
---
class Category < ApplicationRecord
  has_many :articles
end
---
rails c
Category
Category.connection
Category
Category.create(name: "Art")
Category.create(name: "Technology")
Category.create(name: "Politics")
Category.count
@articles = Article.last
---
Article.all
Article.count
@article = Article.first
@article.category_id = 1
@article
@article = Article.find(2)
@article.category_id = 3
@article = Article.find(3)
@article.category_id = 1
@article
---

image image image

app/views/layouts/application.html.haml
---
!!!
%html
%head
    %title Wiki
    = stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true
    = javascript_include_tag 'application', 'data-turbolinks-track' => true
    = csrf_meta_tags
%body
    %nav.navbar.navbar-default.navbar-fixed-top
        .container
            = link_to "Wiki", root_path, class: "navbar-brand"
            %ul.nav.navbar-nav.navbar-right
                - if user_signed_in?
                    %li= link_to "New Article", new_article_path
    %p.notice= notice
    %p.alert= alert

    .container
        .row
            .col-md-8
                = yield
            .col-md-4
                %ul.list-group
                    %li= link_to "All Articles", root_path, class: "list-group-item"
                    - Category.all.each do |category|
                        %li= link_to category.name, articles_path(category: category.name), class: "list-group-item"
---
app/assets/stylesheets/application.scss
---
@import "bootstrap-sprockets";
@import "bootstrap";

ul {
 list-style: none;
}
---
git status
git add .
git commit -m "add Category to Article"
git push origin category

image image

https://getbootstrap.com/docs/3.3/css/
https://getbootstrap.com/docs/3.3/components/
---
app/controllers/articles_controller.rb
---
before_action :find_article, only: [:show, :edit, :update, :destroy]

def edit
end

def update
  if @article.update(article_params)
    redirect_to @article
  else
    render 'edit'
  end
end

def destroy
  @article.destroy
  redirect_to root_path
end
---
git status
git add .
git commit -m "eud"
git push origin EUD

image

git heroku -b heroku
---
Gemfile
gem 'sqlite3', '1.3.13'

group :production do
  gem 'pg', '0.20.0'
end
---
bundle install
rails server
http://localhost:3000/
---
git add .
git commit -a -m "Update Gemfile for Heroku"
heroku create xiaoweiwiki
git push heroku heroku:master
rake assets:clean
eroku run rake db:migrate
heroku open
https://xiaoweiwiki.herokuapp.com/

image

image image image image

image

results matching ""

    No results matching ""