Setup controller for friendships
This commit is contained in:
parent
1752dffb95
commit
b487ffc5da
7 changed files with 153 additions and 1 deletions
53
app/controllers/friendships_controller.rb
Normal file
53
app/controllers/friendships_controller.rb
Normal file
|
@ -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
|
17
app/views/friendships/_form.html.erb
Normal file
17
app/views/friendships/_form.html.erb
Normal file
|
@ -0,0 +1,17 @@
|
|||
<%= form_for(@friendship) do |f| %>
|
||||
<% if @friendship.errors.any? %>
|
||||
<div id="error_explanation">
|
||||
<h2><%= pluralize(@friendship.errors.count, "error") %> prohibited this friendship from being saved:</h2>
|
||||
|
||||
<ul>
|
||||
<% @friendship.errors.full_messages.each do |message| %>
|
||||
<li><%= message %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="actions">
|
||||
<%= f.submit %>
|
||||
</div>
|
||||
<% end %>
|
23
app/views/friendships/index.html.erb
Normal file
23
app/views/friendships/index.html.erb
Normal file
|
@ -0,0 +1,23 @@
|
|||
<h1>Listing friendships</h1>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th colspan="3"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<% @friendships.each do |friendship| %>
|
||||
<tr>
|
||||
<td><%= link_to 'Show', friendship %></td>
|
||||
<td><%= link_to 'Edit', edit_friendship_path(friendship) %></td>
|
||||
<td><%= link_to 'Destroy', friendship, method: :delete, data: { confirm: 'Are you sure?' } %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<br>
|
||||
|
||||
<%= link_to 'New Friendship', new_friendship_path %>
|
5
app/views/friendships/new.html.erb
Normal file
5
app/views/friendships/new.html.erb
Normal file
|
@ -0,0 +1,5 @@
|
|||
<h1>New friendship</h1>
|
||||
|
||||
<%= render 'form' %>
|
||||
|
||||
<%= link_to 'Back', friendships_path %>
|
4
app/views/friendships/show.html.erb
Normal file
4
app/views/friendships/show.html.erb
Normal file
|
@ -0,0 +1,4 @@
|
|||
<p id="notice"><%= notice %></p>
|
||||
|
||||
<%= link_to 'Edit', edit_friendship_path(@friendship) %> |
|
||||
<%= link_to 'Back', friendships_path %>
|
|
@ -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
|
||||
|
|
49
test/controllers/friendships_controller_test.rb
Normal file
49
test/controllers/friendships_controller_test.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue