Skip to content

Commit

Permalink
posts created
Browse files Browse the repository at this point in the history
  • Loading branch information
MGoyheneche committed Apr 7, 2013
1 parent e587792 commit 59b8f6f
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/posts.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/posts.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the posts controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
83 changes: 83 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class PostsController < ApplicationController
# GET /posts
# GET /posts.json
def index
@posts = Post.all

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.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render json: @post }
end
end

# GET /posts/new
# GET /posts/new.json
def new
@post = Post.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @post }
end
end

# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
end

# POST /posts
# POST /posts.json
def create
@post = Post.new(params[:post])

respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

# PUT /posts/1
# PUT /posts/1.json
def update
@post = Post.find(params[:id])

respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post = Post.find(params[:id])
@post.destroy

respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/posts_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PostsHelper
end
3 changes: 3 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Post < ActiveRecord::Base
attr_accessible :content, :user_id
end
25 changes: 25 additions & 0 deletions app/views/posts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %>
</div>
<div class="field">
<%= f.label :user_id %><br />
<%= f.number_field :user_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/posts/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing post</h1>

<%= render 'form' %>

<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>
25 changes: 25 additions & 0 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h1>Listing posts</h1>

<table>
<tr>
<th>Content</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @posts.each do |post| %>
<tr>
<td><%= post.content %></td>
<td><%= post.user_id %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New Post', new_post_path %>
5 changes: 5 additions & 0 deletions app/views/posts/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New post</h1>

<%= render 'form' %>

<%= link_to 'Back', posts_path %>
15 changes: 15 additions & 0 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<p id="notice"><%= notice %></p>

<p>
<b>Content:</b>
<%= @post.content %>
</p>

<p>
<b>User:</b>
<%= @post.user_id %>
</p>


<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
10 changes: 10 additions & 0 deletions db/migrate/20130407144832_create_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :content
t.integer :user_id

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

one:
content: MyString
user_id: 1

two:
content: MyString
user_id: 1
49 changes: 49 additions & 0 deletions test/functional/posts_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'test_helper'

class PostsControllerTest < ActionController::TestCase
setup do
@post = posts(:one)
end

test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:posts)
end

test "should get new" do
get :new
assert_response :success
end

test "should create post" do
assert_difference('Post.count') do
post :create, post: { content: @post.content, user_id: @post.user_id }
end

assert_redirected_to post_path(assigns(:post))
end

test "should show post" do
get :show, id: @post
assert_response :success
end

test "should get edit" do
get :edit, id: @post
assert_response :success
end

test "should update post" do
put :update, id: @post, post: { content: @post.content, user_id: @post.user_id }
assert_redirected_to post_path(assigns(:post))
end

test "should destroy post" do
assert_difference('Post.count', -1) do
delete :destroy, id: @post
end

assert_redirected_to posts_path
end
end
4 changes: 4 additions & 0 deletions test/unit/helpers/posts_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class PostsHelperTest < ActionView::TestCase
end
7 changes: 7 additions & 0 deletions test/unit/post_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class PostTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 59b8f6f

Please sign in to comment.