使用 Ruby on Rails 15分钟开发一个博客系统

download 使用 Ruby on Rails 15分钟开发一个博客系统

of 8

description

Ruby on Rails 的入门示例。

Transcript of 使用 Ruby on Rails 15分钟开发一个博客系统

Ruby On Rails [email protected] on Rails Ubuntu 14.04/Ruby 1.9.3/Rails 4.2

1IDEAptana Studio 3

2 Article /home/zhangmz/ProgramFiles/Aptana_Studio_3/workspace/blog

rails generate scaffold Article title:string text:textrake db:migrate

3

rails s

4 Article

5blog/app/views/articles/index.html.erb

Listing Articles

- ago

blog/app/views/articles//show.html.erb

Title:

Text:

| | :delete, data: { confirm: "Are you sure?" } %>

6IDViewmodel/controller

rails generate model Comment article_id:integer text:textrake db:migrate

rails generate controller Comments create destroy

7blog/app/views/articles//show.html.erb//

Title:

Text:

Comments

ago :delete, data: { confirm: "Are you sure?" } %>

'20x5' %>

| | :delete, data: { confirm: "Are you sure?" } %>

blog/config/routes.rb resources :articles do resources :comments end

blog/app/models/article.rbclass Article < ActiveRecord::Base has_many :comments, dependent: :destroyend

blog/app/models/comment.rbclass Comment < ActiveRecord::Base belongs_to :article validates :text, presence: trueend

blog/app/controllers/comments_controller.rbclass CommentsController < ApplicationController def create @article = Article.find(params[:article_id]) @comment = @article.comments.new(comment_params) @comment.save redirect_to @article end

def destroy @article = Article.find(params[:article_id]) @comment = @article.comments.find(params[:id]) @comment.destroy redirect_to @article end

private

def comment_params params.require(:comment).permit(:post_id , :text) endend

1sqlitesqlite3 -line db/development.sqlite3.quit

2BootstrapBootstrapsudo gem install bootstrap-sassGemfileBootstrapgem 'bootstrap-sass'blog/assets/javascripts/application.js//= require bootstrapblog/config/application.rbclass Application < Rails::Applicationconfig.assets.paths