From b487ffc5da4edbddfd3361d048580ee18c9bbfab Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Wed, 15 Oct 2014 13:32:35 -0400 Subject: [PATCH] Setup controller for friendships --- app/controllers/friendships_controller.rb | 53 +++++++++++++++++++ app/views/friendships/_form.html.erb | 17 ++++++ app/views/friendships/index.html.erb | 23 ++++++++ app/views/friendships/new.html.erb | 5 ++ app/views/friendships/show.html.erb | 4 ++ config/routes.rb | 3 +- .../friendships_controller_test.rb | 49 +++++++++++++++++ 7 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 app/controllers/friendships_controller.rb create mode 100644 app/views/friendships/_form.html.erb create mode 100644 app/views/friendships/index.html.erb create mode 100644 app/views/friendships/new.html.erb create mode 100644 app/views/friendships/show.html.erb create mode 100644 test/controllers/friendships_controller_test.rb diff --git a/app/controllers/friendships_controller.rb b/app/controllers/friendships_controller.rb new file mode 100644 index 0000000..5d6a280 --- /dev/null +++ b/app/controllers/friendships_controller.rb @@ -0,0 +1,53 @@ +class FriendshipsController < ApplicationController + before_action :set_friendship, only: [:show, :destroy] + + def index + @friendships = Friendship.all + end + + def show + end + + def new + @friendship = Friendship.new + end + + def create + @friendship = Friendship.new(friendship_params) + + respond_to do |format| + if @friendship.save + format.html { + redirect_to @friendship, + notice: 'Friendship was successfully created.' + } + format.json { render :show, status: :created, location: @friendship } + else + format.html { render :new } + format.json { + render json: @friendship.errors, status: :unprocessable_entity + } + end + end + end + + def destroy + @friendship.destroy + respond_to do |format| + format.html { + redirect_to friendships_url, + notice: 'Friendship was successfully destroyed.' + } + format.json { head :no_content } + end + end + + private + def set_friendship + @friendship = Friendship.find(params[:id]) + end + + def friendship_params + params[:friendship] + end +end diff --git a/app/views/friendships/_form.html.erb b/app/views/friendships/_form.html.erb new file mode 100644 index 0000000..c792b4b --- /dev/null +++ b/app/views/friendships/_form.html.erb @@ -0,0 +1,17 @@ +<%= form_for(@friendship) do |f| %> + <% if @friendship.errors.any? %> +
+

<%= pluralize(@friendship.errors.count, "error") %> prohibited this friendship from being saved:

+ + +
+ <% end %> + +
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/friendships/index.html.erb b/app/views/friendships/index.html.erb new file mode 100644 index 0000000..6139261 --- /dev/null +++ b/app/views/friendships/index.html.erb @@ -0,0 +1,23 @@ +

Listing friendships

+ + + + + + + + + + <% @friendships.each do |friendship| %> + + + + + + <% end %> + +
<%= link_to 'Show', friendship %><%= link_to 'Edit', edit_friendship_path(friendship) %><%= link_to 'Destroy', friendship, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Friendship', new_friendship_path %> diff --git a/app/views/friendships/new.html.erb b/app/views/friendships/new.html.erb new file mode 100644 index 0000000..523229c --- /dev/null +++ b/app/views/friendships/new.html.erb @@ -0,0 +1,5 @@ +

New friendship

+ +<%= render 'form' %> + +<%= link_to 'Back', friendships_path %> diff --git a/app/views/friendships/show.html.erb b/app/views/friendships/show.html.erb new file mode 100644 index 0000000..8a72d24 --- /dev/null +++ b/app/views/friendships/show.html.erb @@ -0,0 +1,4 @@ +

<%= notice %>

+ +<%= link_to 'Edit', edit_friendship_path(@friendship) %> | +<%= link_to 'Back', friendships_path %> diff --git a/config/routes.rb b/config/routes.rb index dd160c6..7e57d2b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,8 +3,9 @@ Rails.application.routes.draw do get 'auth/failure', to: redirect('/') get 'logout', to: 'sessions#destroy', as: 'logout' - resources :sessions, only: [ :create, :destroy ] resources :home, only: [ :show ] + resources :friendships, only: [ :index, :show, :create, :new, :destroy ] + resources :sessions, only: [ :create, :destroy ] root to: 'home#show' end diff --git a/test/controllers/friendships_controller_test.rb b/test/controllers/friendships_controller_test.rb new file mode 100644 index 0000000..abc0dc9 --- /dev/null +++ b/test/controllers/friendships_controller_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class FriendshipsControllerTest < ActionController::TestCase + setup do + @friendship = friendships(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:friendships) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create friendship" do + assert_difference('Friendship.count') do + post :create, friendship: { } + end + + assert_redirected_to friendship_path(assigns(:friendship)) + end + + test "should show friendship" do + get :show, id: @friendship + assert_response :success + end + + test "should get edit" do + get :edit, id: @friendship + assert_response :success + end + + test "should update friendship" do + patch :update, id: @friendship, friendship: { } + assert_redirected_to friendship_path(assigns(:friendship)) + end + + test "should destroy friendship" do + assert_difference('Friendship.count', -1) do + delete :destroy, id: @friendship + end + + assert_redirected_to friendships_path + end +end