@@ -0,0 +1,3 @@ |
||
1 |
+# Place all the behaviors and hooks related to the matching controller here. |
|
2 |
+# All this logic will automatically be available in application.js. |
|
3 |
+# You can use CoffeeScript in this file: http://coffeescript.org/ |
@@ -0,0 +1,3 @@ |
||
1 |
+// Place all the styles related to the blog_posts controller here. |
|
2 |
+// They will automatically be included in application.css. |
|
3 |
+// You can use Less here: http://lesscss.org/ |
@@ -0,0 +1,78 @@ |
||
1 |
+class BlogPostsController < ApplicationController |
|
2 |
+ before_action :set_blog_post, only: [:show, :edit, :update, :destroy] |
|
3 |
+ |
|
4 |
+ # GET /blog_posts |
|
5 |
+ # GET /blog_posts.json |
|
6 |
+ def index |
|
7 |
+ @blog_posts = BlogPost.all |
|
8 |
+ end |
|
9 |
+ |
|
10 |
+ def list |
|
11 |
+ @blog_posts = BlogPost.all |
|
12 |
+ end |
|
13 |
+ |
|
14 |
+ # GET /blog_posts/1 |
|
15 |
+ # GET /blog_posts/1.json |
|
16 |
+ def show |
|
17 |
+ end |
|
18 |
+ |
|
19 |
+ # GET /blog_posts/new |
|
20 |
+ def new |
|
21 |
+ @blog_post = BlogPost.new |
|
22 |
+ end |
|
23 |
+ |
|
24 |
+ # GET /blog_posts/1/edit |
|
25 |
+ def edit |
|
26 |
+ end |
|
27 |
+ |
|
28 |
+ # POST /blog_posts |
|
29 |
+ # POST /blog_posts.json |
|
30 |
+ def create |
|
31 |
+ @blog_post = BlogPost.new(blog_post_params) |
|
32 |
+ @blog_post.update(:author => current_user) |
|
33 |
+ respond_to do |format| |
|
34 |
+ if @blog_post.save |
|
35 |
+ format.html { redirect_to @blog_post, notice: 'Blog post was successfully created.' } |
|
36 |
+ format.json { render action: 'show', status: :created, location: @blog_post } |
|
37 |
+ else |
|
38 |
+ format.html { render action: 'new' } |
|
39 |
+ format.json { render json: @blog_post.errors, status: :unprocessable_entity } |
|
40 |
+ end |
|
41 |
+ end |
|
42 |
+ end |
|
43 |
+ |
|
44 |
+ # PATCH/PUT /blog_posts/1 |
|
45 |
+ # PATCH/PUT /blog_posts/1.json |
|
46 |
+ def update |
|
47 |
+ respond_to do |format| |
|
48 |
+ if @blog_post.update(blog_post_params) |
|
49 |
+ format.html { redirect_to @blog_post, notice: 'Blog post was successfully updated.' } |
|
50 |
+ format.json { head :no_content } |
|
51 |
+ else |
|
52 |
+ format.html { render action: 'edit' } |
|
53 |
+ format.json { render json: @blog_post.errors, status: :unprocessable_entity } |
|
54 |
+ end |
|
55 |
+ end |
|
56 |
+ end |
|
57 |
+ |
|
58 |
+ # DELETE /blog_posts/1 |
|
59 |
+ # DELETE /blog_posts/1.json |
|
60 |
+ def destroy |
|
61 |
+ @blog_post.destroy |
|
62 |
+ respond_to do |format| |
|
63 |
+ format.html { redirect_to blog_posts_url } |
|
64 |
+ format.json { head :no_content } |
|
65 |
+ end |
|
66 |
+ end |
|
67 |
+ |
|
68 |
+ private |
|
69 |
+ # Use callbacks to share common setup or constraints between actions. |
|
70 |
+ def set_blog_post |
|
71 |
+ @blog_post = BlogPost.find(params[:id]) |
|
72 |
+ end |
|
73 |
+ |
|
74 |
+ # Never trust parameters from the scary internet, only allow the white list through. |
|
75 |
+ def blog_post_params |
|
76 |
+ params.require(:blog_post).permit(:title, :slug, :content, :published, :author_id) |
|
77 |
+ end |
|
78 |
+end |
@@ -0,0 +1,2 @@ |
||
1 |
+module BlogPostsHelper |
|
2 |
+end |
@@ -0,0 +1,3 @@ |
||
1 |
+class BlogPost < ActiveRecord::Base |
|
2 |
+ belongs_to :author, :class_name => "User" |
|
3 |
+end |
@@ -6,7 +6,9 @@ class User < ActiveRecord::Base |
||
6 | 6 |
|
7 | 7 |
validates :password, presence: true, length: {minimum: 5, maximum: 120}, on: :create |
8 | 8 |
validates :password, length: {minimum: 5, maximum: 120}, on: :update, allow_blank: true |
9 |
- |
|
9 |
+ |
|
10 |
+ has_many :posts |
|
11 |
+ |
|
10 | 12 |
def full_name |
11 | 13 |
name = self.first_name.to_s + ' ' + self.last_name.to_s |
12 | 14 |
return name |
@@ -0,0 +1,15 @@ |
||
1 |
+<%= simple_form_for(@blog_post) do |f| %> |
|
2 |
+ <%= f.error_notification %> |
|
3 |
+ |
|
4 |
+ <div class="form-inputs"> |
|
5 |
+ <%= f.input :title %> |
|
6 |
+ <%= f.input :slug %> |
|
7 |
+ <%= f.input :content %> |
|
8 |
+ <%= f.input :published %> |
|
9 |
+ <%= f.association :author %> |
|
10 |
+ </div> |
|
11 |
+ |
|
12 |
+ <div class="form-actions"> |
|
13 |
+ <%= f.button :submit %> |
|
14 |
+ </div> |
|
15 |
+<% end %> |
@@ -0,0 +1,6 @@ |
||
1 |
+<h1>Editing blog_post</h1> |
|
2 |
+ |
|
3 |
+<%= render 'form' %> |
|
4 |
+ |
|
5 |
+<%= link_to 'Show', @blog_post %> | |
|
6 |
+<%= link_to 'Back', blog_posts_path %> |
@@ -0,0 +1,19 @@ |
||
1 |
+<div class="page-header"> |
|
2 |
+ <h1>Blog</h1> |
|
3 |
+</div> |
|
4 |
+ |
|
5 |
+<ul class="thumbnails"> |
|
6 |
+ <% @blog_posts.each do |post| %> |
|
7 |
+ <li class="span3"> |
|
8 |
+ <div class="thumbnail"> |
|
9 |
+ <img src="http://placehold.it/300x200" alt=""> |
|
10 |
+ <h3><%= post.title %></h3> |
|
11 |
+ <p><%= post.content %></p> |
|
12 |
+ </div> |
|
13 |
+ </li> |
|
14 |
+ <% end %> |
|
15 |
+</ul> |
|
16 |
+ |
|
17 |
+<br> |
|
18 |
+ |
|
19 |
+<%= link_to 'New Blog post', new_blog_post_path %> |
@@ -0,0 +1,4 @@ |
||
1 |
+json.array!(@blog_posts) do |blog_post| |
|
2 |
+ json.extract! blog_post, :id, :title, :slug, :content, :published, :author_id |
|
3 |
+ json.url blog_post_url(blog_post, format: :json) |
|
4 |
+end |
@@ -0,0 +1,37 @@ |
||
1 |
+<div class="page-header"> |
|
2 |
+ <h1>Blog</h1> |
|
3 |
+</div> |
|
4 |
+ |
|
5 |
+<table class="table table-bordered"> |
|
6 |
+ <thead> |
|
7 |
+ <tr> |
|
8 |
+ <th>Title</th> |
|
9 |
+ <th>Slug</th> |
|
10 |
+ <th>Content</th> |
|
11 |
+ <th>Published</th> |
|
12 |
+ <th>Author</th> |
|
13 |
+ <th></th> |
|
14 |
+ <th></th> |
|
15 |
+ <th></th> |
|
16 |
+ </tr> |
|
17 |
+ </thead> |
|
18 |
+ |
|
19 |
+ <tbody> |
|
20 |
+ <% @blog_posts.each do |blog_post| %> |
|
21 |
+ <tr> |
|
22 |
+ <td><%= blog_post.title %></td> |
|
23 |
+ <td><%= blog_post.slug %></td> |
|
24 |
+ <td><%= blog_post.content %></td> |
|
25 |
+ <td><%= blog_post.published %></td> |
|
26 |
+ <td><%= blog_post.author.full_name %></td> |
|
27 |
+ <td><%= link_to 'Show', post_path(blog_post) %></td> |
|
28 |
+ <td><%= link_to 'Edit', edit_blog_post_path(blog_post) %></td> |
|
29 |
+ <td><%= link_to 'Destroy', blog_post, method: :delete, data: { confirm: 'Are you sure?' } %></td> |
|
30 |
+ </tr> |
|
31 |
+ <% end %> |
|
32 |
+ </tbody> |
|
33 |
+</table> |
|
34 |
+ |
|
35 |
+<br> |
|
36 |
+ |
|
37 |
+<%= link_to 'New Blog post', new_blog_post_path %> |
@@ -0,0 +1,5 @@ |
||
1 |
+<h1>New blog_post</h1> |
|
2 |
+ |
|
3 |
+<%= render 'form' %> |
|
4 |
+ |
|
5 |
+<%= link_to 'Back', blog_path %> |
@@ -0,0 +1,29 @@ |
||
1 |
+<p id="notice"><%= notice %></p> |
|
2 |
+ |
|
3 |
+<p> |
|
4 |
+ <strong>Title:</strong> |
|
5 |
+ <%= @blog_post.title %> |
|
6 |
+</p> |
|
7 |
+ |
|
8 |
+<p> |
|
9 |
+ <strong>Slug:</strong> |
|
10 |
+ <%= @blog_post.slug %> |
|
11 |
+</p> |
|
12 |
+ |
|
13 |
+<p> |
|
14 |
+ <strong>Content:</strong> |
|
15 |
+ <%= @blog_post.content %> |
|
16 |
+</p> |
|
17 |
+ |
|
18 |
+<p> |
|
19 |
+ <strong>Published:</strong> |
|
20 |
+ <%= @blog_post.published %> |
|
21 |
+</p> |
|
22 |
+ |
|
23 |
+<p> |
|
24 |
+ <strong>Author:</strong> |
|
25 |
+ <%= @blog_post.author.full_name %> |
|
26 |
+</p> |
|
27 |
+ |
|
28 |
+<%= link_to 'Edit', edit_blog_post_path(@blog_post) %> | |
|
29 |
+<%= link_to 'Back', blog_posts_path %> |
@@ -0,0 +1 @@ |
||
1 |
+json.extract! @blog_post, :id, :title, :slug, :content, :published, :author_id, :created_at, :updated_at |
@@ -1,6 +1,6 @@ |
||
1 | 1 |
<div class="container nav-collapse"> |
2 | 2 |
<ul class="nav"> |
3 |
- <li><%= link_to "Link1", "#" %></li> |
|
3 |
+ <li><%= link_to "Blog", blog_path %></li> |
|
4 | 4 |
<li><%= link_to "Link2", "#" %></li> |
5 | 5 |
<li><%= link_to "Link3", "#" %></li> |
6 | 6 |
</ul> |
@@ -1,5 +1,10 @@ |
||
1 | 1 |
RailsWebsiteTemplate::Application.routes.draw do |
2 | 2 |
|
3 |
+ get "blog" => "blog_posts#index", :as => :blog |
|
4 |
+ get "post/:id" => "blog_posts#show", :as => :post |
|
5 |
+ get "admin/posts/list" => "blog_posts#list", :as => :post_list |
|
6 |
+ resources :blog_posts, path: '/admin/posts' |
|
7 |
+ |
|
3 | 8 |
get "start/index" |
4 | 9 |
devise_for :users, :skip => [:sessions, :passwords, :confirmations, :registrations] |
5 | 10 |
as :user do |
@@ -0,0 +1,14 @@ |
||
1 |
+class CreateBlogPosts < ActiveRecord::Migration |
|
2 |
+ def change |
|
3 |
+ create_table :blog_posts do |t| |
|
4 |
+ t.string :title |
|
5 |
+ t.string :slug |
|
6 |
+ t.text :content |
|
7 |
+ t.boolean :published |
|
8 |
+ t.references :author, index: true |
|
9 |
+ |
|
10 |
+ t.timestamps |
|
11 |
+ end |
|
12 |
+ add_index :blog_posts, :slug, unique: true |
|
13 |
+ end |
|
14 |
+end |
@@ -11,11 +11,24 @@ |
||
11 | 11 |
# |
12 | 12 |
# It's strongly recommended that you check this file into your version control system. |
13 | 13 |
|
14 |
-ActiveRecord::Schema.define(version: 20140916035845) do |
|
14 |
+ActiveRecord::Schema.define(version: 20140916213814) do |
|
15 | 15 |
|
16 | 16 |
# These are extensions that must be enabled in order to support this database |
17 | 17 |
enable_extension "plpgsql" |
18 | 18 |
|
19 |
+ create_table "blog_posts", force: true do |t| |
|
20 |
+ t.string "title" |
|
21 |
+ t.string "slug" |
|
22 |
+ t.text "content" |
|
23 |
+ t.boolean "published" |
|
24 |
+ t.integer "author_id" |
|
25 |
+ t.datetime "created_at" |
|
26 |
+ t.datetime "updated_at" |
|
27 |
+ end |
|
28 |
+ |
|
29 |
+ add_index "blog_posts", ["author_id"], name: "index_blog_posts_on_author_id", using: :btree |
|
30 |
+ add_index "blog_posts", ["slug"], name: "index_blog_posts_on_slug", unique: true, using: :btree |
|
31 |
+ |
|
19 | 32 |
create_table "friendly_id_slugs", force: true do |t| |
20 | 33 |
t.string "slug", null: false |
21 | 34 |
t.integer "sluggable_id", null: false |
@@ -0,0 +1,49 @@ |
||
1 |
+require 'test_helper' |
|
2 |
+ |
|
3 |
+class BlogPostsControllerTest < ActionController::TestCase |
|
4 |
+ setup do |
|
5 |
+ @blog_post = blog_posts(:one) |
|
6 |
+ end |
|
7 |
+ |
|
8 |
+ test "should get index" do |
|
9 |
+ get :index |
|
10 |
+ assert_response :success |
|
11 |
+ assert_not_nil assigns(:blog_posts) |
|
12 |
+ end |
|
13 |
+ |
|
14 |
+ test "should get new" do |
|
15 |
+ get :new |
|
16 |
+ assert_response :success |
|
17 |
+ end |
|
18 |
+ |
|
19 |
+ test "should create blog_post" do |
|
20 |
+ assert_difference('BlogPost.count') do |
|
21 |
+ post :create, blog_post: { author_id: @blog_post.author_id, content: @blog_post.content, published: @blog_post.published, slug: @blog_post.slug, title: @blog_post.title } |
|
22 |
+ end |
|
23 |
+ |
|
24 |
+ assert_redirected_to blog_post_path(assigns(:blog_post)) |
|
25 |
+ end |
|
26 |
+ |
|
27 |
+ test "should show blog_post" do |
|
28 |
+ get :show, id: @blog_post |
|
29 |
+ assert_response :success |
|
30 |
+ end |
|
31 |
+ |
|
32 |
+ test "should get edit" do |
|
33 |
+ get :edit, id: @blog_post |
|
34 |
+ assert_response :success |
|
35 |
+ end |
|
36 |
+ |
|
37 |
+ test "should update blog_post" do |
|
38 |
+ patch :update, id: @blog_post, blog_post: { author_id: @blog_post.author_id, content: @blog_post.content, published: @blog_post.published, slug: @blog_post.slug, title: @blog_post.title } |
|
39 |
+ assert_redirected_to blog_post_path(assigns(:blog_post)) |
|
40 |
+ end |
|
41 |
+ |
|
42 |
+ test "should destroy blog_post" do |
|
43 |
+ assert_difference('BlogPost.count', -1) do |
|
44 |
+ delete :destroy, id: @blog_post |
|
45 |
+ end |
|
46 |
+ |
|
47 |
+ assert_redirected_to blog_posts_path |
|
48 |
+ end |
|
49 |
+end |
@@ -0,0 +1,15 @@ |
||
1 |
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html |
|
2 |
+ |
|
3 |
+one: |
|
4 |
+ title: MyString |
|
5 |
+ slug: MyString |
|
6 |
+ content: MyText |
|
7 |
+ published: false |
|
8 |
+ author_id: |
|
9 |
+ |
|
10 |
+two: |
|
11 |
+ title: MyString |
|
12 |
+ slug: MyString |
|
13 |
+ content: MyText |
|
14 |
+ published: false |
|
15 |
+ author_id: |
@@ -0,0 +1,4 @@ |
||
1 |
+require 'test_helper' |
|
2 |
+ |
|
3 |
+class BlogPostsHelperTest < ActionView::TestCase |
|
4 |
+end |
@@ -0,0 +1,7 @@ |
||
1 |
+require 'test_helper' |
|
2 |
+ |
|
3 |
+class BlogPostTest < ActiveSupport::TestCase |
|
4 |
+ # test "the truth" do |
|
5 |
+ # assert true |
|
6 |
+ # end |
|
7 |
+end |