Skip to content

Commit

Permalink
Very simple controller setup
Browse files Browse the repository at this point in the history
  • Loading branch information
thagomizer committed Nov 12, 2014
1 parent 1d664b8 commit f664a48
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 1 deletion.
23 changes: 23 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class PostsController < ApplicationController
# GET /posts
# GET /posts.json
def index
@posts = Post.published

respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end

# GET /posts/1
# GET /posts/1.json
def show
@post = Post.published.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render json: @post }
end
end
end
13 changes: 13 additions & 0 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1>Seattle.rb Blog</h1>

<% @posts.each do |post| %>
<h2><%= link_to post.title, post %></h2>
<h5><%= post.member.name %></h5>
<p/>
<p>
<%= post.body.truncate(255) %>
</p>
<hr/>
<% end %>

<br />
8 changes: 8 additions & 0 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h2><%= @post.title %></h2>
<h5><%= @post.member.name %></h5>
<p/>
<p>
<%= @post.body %>
</p>

<%= link_to 'Back', posts_path %>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

resources :helps

resources :posts, :only => [:index, :show]

mount RailsAdmin::Engine => '/adminsrb', :as => 'rails_admin'

devise_for :admins
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/members_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_create
:email => "[email protected]",
:ruby_gems_id => "qrush"}
end

assert_redirected_to members_path
get :index
assert_response :success
Expand Down
35 changes: 35 additions & 0 deletions test/controllers/posts_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "minitest_helper"

class PostsControllerTest < ActionController::TestCase

def setup
Member.create!(:name => "Big Bird")
Post.all.each { |p| p.update_attribute(:member, Member.first) }
@post = posts(:one)
end

def tear_down
Member.destroy_all
end

def test_index
get :index
assert_response :success
refute_nil assigns(:posts)

Post.all.each do |p|
assert_match p.title, @response.body
assert_match p.body[0..10], @response.body
assert_match p.member.name, @response.body
end
end

def test_show
get :show, id: @post
assert_response :success
assert_match @post.title, @response.body
assert_match @post.body, @response.body
assert_match @post.member.name, @response.body
end

end
12 changes: 12 additions & 0 deletions test/fixtures/posts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Read about fixtures at
# http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html

one:
title: MyString
body: MyText
published: true

two:
title: MyString
body: MyText
published: false

0 comments on commit f664a48

Please sign in to comment.