Simple add friend code
This commit is contained in:
parent
638fb7a8b1
commit
6f7a044e59
12 changed files with 77 additions and 37 deletions
2
Gemfile
2
Gemfile
|
@ -15,3 +15,5 @@ gem 'spring', group: :development
|
|||
|
||||
gem 'omniauth-google-oauth2', '~> 0.2.5'
|
||||
gem 'bootstrap-sass', '~> 3.2.0.2'
|
||||
|
||||
gem 'rb-readline'
|
||||
|
|
|
@ -96,6 +96,7 @@ GEM
|
|||
rake (>= 0.8.7)
|
||||
thor (>= 0.18.1, < 2.0)
|
||||
rake (10.3.2)
|
||||
rb-readline (0.5.1)
|
||||
rdoc (4.1.2)
|
||||
json (~> 1.4)
|
||||
sass (3.2.19)
|
||||
|
@ -139,6 +140,7 @@ DEPENDENCIES
|
|||
jquery-rails
|
||||
omniauth-google-oauth2 (~> 0.2.5)
|
||||
rails (= 4.1.6)
|
||||
rb-readline
|
||||
sass-rails (~> 4.0.3)
|
||||
sdoc (~> 0.4.0)
|
||||
spring
|
||||
|
|
3
app/assets/javascripts/user.js.coffee
Normal file
3
app/assets/javascripts/user.js.coffee
Normal file
|
@ -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://coffeescript.org/
|
3
app/assets/stylesheets/user.css.scss
Normal file
3
app/assets/stylesheets/user.css.scss
Normal file
|
@ -0,0 +1,3 @@
|
|||
// Place all the styles related to the User controller here.
|
||||
// They will automatically be included in application.css.
|
||||
// You can use Sass (SCSS) here: http://sass-lang.com/
|
|
@ -1,54 +1,29 @@
|
|||
class FriendshipsController < ApplicationController
|
||||
before_action :require_login
|
||||
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)
|
||||
@friendship = current_user.friendships.build(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
|
||||
if @friendship.save
|
||||
flash[:notice] = 'Added friend.'
|
||||
redirect_to root_url
|
||||
else
|
||||
flash[:notice] = 'Unable to add friend.'
|
||||
redirect_to root_url
|
||||
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
|
||||
def friendship_params
|
||||
# why not namespaced
|
||||
params.permit(:friend_id)
|
||||
end
|
||||
end
|
||||
|
|
8
app/controllers/users_controller.rb
Normal file
8
app/controllers/users_controller.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
class UsersController < ApplicationController
|
||||
def index
|
||||
@users = User.all
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
end
|
2
app/helpers/user_helper.rb
Normal file
2
app/helpers/user_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
module UserHelper
|
||||
end
|
24
app/views/users/index.html.erb
Normal file
24
app/views/users/index.html.erb
Normal file
|
@ -0,0 +1,24 @@
|
|||
<h2>User List</h2>
|
||||
|
||||
<table class="table table-striped table-hover">
|
||||
<thead>
|
||||
<tr>
|
||||
<td>Name</td>
|
||||
<td>Created</td>
|
||||
<td>Updated</td>
|
||||
<td>Actions</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @users.each do |user| %>
|
||||
<tr>
|
||||
<td><%= link_to user.name, user %></td>
|
||||
<td><%= user.created_at %></td>
|
||||
<td><%= user.updated_at %></td>
|
||||
<td>
|
||||
<%= link_to '+', friendships_path(:friend_id => user), :method => :post %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
2
app/views/users/show.html.erb
Normal file
2
app/views/users/show.html.erb
Normal file
|
@ -0,0 +1,2 @@
|
|||
<h1>User#show</h1>
|
||||
<p>Find me in app/views/user/show.html.erb</p>
|
|
@ -4,8 +4,9 @@ Rails.application.routes.draw do
|
|||
get 'logout', to: 'sessions#destroy', as: 'logout'
|
||||
|
||||
resources :home, only: [ :show ]
|
||||
resources :friendships, only: [ :index, :show, :create, :new, :destroy ]
|
||||
resources :friendships, only: [ :index, :show, :create, :destroy ]
|
||||
resources :sessions, only: [ :create, :destroy ]
|
||||
resources :users, only: [ :index, :show ]
|
||||
|
||||
root to: 'home#show'
|
||||
end
|
||||
|
|
14
test/controllers/user_controller_test.rb
Normal file
14
test/controllers/user_controller_test.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
require 'test_helper'
|
||||
|
||||
class UserControllerTest < ActionController::TestCase
|
||||
test "should get index" do
|
||||
get :index
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get show" do
|
||||
get :show
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
end
|
4
test/helpers/user_helper_test.rb
Normal file
4
test/helpers/user_helper_test.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
require 'test_helper'
|
||||
|
||||
class UserHelperTest < ActionView::TestCase
|
||||
end
|
Loading…
Reference in a new issue