diff --git a/app/assets/javascripts/chatrooms.js b/app/assets/javascripts/chatrooms.js new file mode 100644 index 0000000..f4d6d72 --- /dev/null +++ b/app/assets/javascripts/chatrooms.js @@ -0,0 +1,8 @@ +$(document).on('turbolinks:load', function() { + $('#new_message').on('keypress', function(e) { + if(e && e.keyCode == 13){ + e.preventDefault(); + $(this).submit(); + } + }); +}); diff --git a/app/assets/javascripts/chatrooms.coffee b/app/assets/javascripts/messages.coffee similarity index 100% rename from app/assets/javascripts/chatrooms.coffee rename to app/assets/javascripts/messages.coffee diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 48a3160..6bc6172 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -1,3 +1,13 @@ @import "bootstrap-sprockets"; @import "bootstrap"; @import 'devise_flash_messages'; + +.container-fluid.content-container { + padding-top: 70px; +} + +.sidebar-fixed-position { + position: fixed; + top: 70; + left: 0; +} diff --git a/app/assets/stylesheets/messages.scss b/app/assets/stylesheets/messages.scss new file mode 100644 index 0000000..492f0fa --- /dev/null +++ b/app/assets/stylesheets/messages.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Messages controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/chatrooms_controller.rb b/app/controllers/chatrooms_controller.rb index db90d24..2923811 100644 --- a/app/controllers/chatrooms_controller.rb +++ b/app/controllers/chatrooms_controller.rb @@ -10,6 +10,7 @@ def index # GET /chatrooms/1 # GET /chatrooms/1.json def show + @messages = @chatroom.messages.order(created_at: :desc).limit(100).reverse end # GET /chatrooms/new diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb new file mode 100644 index 0000000..1487e7d --- /dev/null +++ b/app/controllers/messages_controller.rb @@ -0,0 +1,21 @@ +class MessagesController < ApplicationController + before_action :authenticate_user! + before_action :set_chatroom + + def create + message = @chatroom.messages.new(message_params) + message.user = current_user + message.save + redirect_to @chatroom + end + + private + + def set_chatroom + @chatroom = Chatroom.find(params[:chatroom_id]) + end + + def message_params + params.require(:message).permit(:body) + end +end diff --git a/app/helpers/messages_helper.rb b/app/helpers/messages_helper.rb new file mode 100644 index 0000000..f1bca9f --- /dev/null +++ b/app/helpers/messages_helper.rb @@ -0,0 +1,2 @@ +module MessagesHelper +end diff --git a/app/views/chatrooms/show.html.erb b/app/views/chatrooms/show.html.erb deleted file mode 100644 index a077952..0000000 --- a/app/views/chatrooms/show.html.erb +++ /dev/null @@ -1,7 +0,0 @@ -

- Name: - <%= @chatroom.name %> -

- -<%= link_to 'Edit', edit_chatroom_path(@chatroom) %> | -<%= link_to 'Back', chatrooms_path %> diff --git a/app/views/chatrooms/show.html.slim b/app/views/chatrooms/show.html.slim new file mode 100644 index 0000000..328b86a --- /dev/null +++ b/app/views/chatrooms/show.html.slim @@ -0,0 +1,14 @@ +p + strong Chatroom name: + = @chatroom.name + + +div data-behavior='messages' + - @messages.each do |message| + div + strong = message.user.username + |: + = message.body + += form_for [@chatroom, Message.new] do |f| + = f.text_area :body, rows: 1, autofocus: true, class: 'form-control' diff --git a/app/views/layouts/_header_navigation.html.slim b/app/views/layouts/_header_navigation.html.slim index dd737cc..6e48b07 100644 --- a/app/views/layouts/_header_navigation.html.slim +++ b/app/views/layouts/_header_navigation.html.slim @@ -1,4 +1,4 @@ -nav.navbar.navbar-default.navbar-static-top +nav.navbar.navbar-default.navbar-fixed-top .container-fluid .navbar-header button.navbar-toggle.collapsed type='button' data-toggle='collapse' data-target='#navbar' aria-expanded='false' aria-controls='navbar' diff --git a/app/views/layouts/application.html.slim b/app/views/layouts/application.html.slim index 10713c0..f6d2412 100644 --- a/app/views/layouts/application.html.slim +++ b/app/views/layouts/application.html.slim @@ -15,18 +15,19 @@ html lang="en" = render "layouts/header_navigation" - .container-fluid + .container-fluid.content-container = render 'layouts/flash_messages' .row - .col-sm-2 + .col-sm-3.sidebar-fixed-position - if user_signed_in? h5 Chatrooms - ul + ul.nav.nav-pills.nav-stacked - current_user.chatrooms.each do |chatroom| - li = link_to chatroom.name, '#' - .col-sm-10 + li = link_to chatroom.name, chatroom + + .col-sm-9.col-sm-offset-3 = yield hr diff --git a/config/routes.rb b/config/routes.rb index cb3c589..3eeb89d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ resources :chatrooms do resource :chatroom_users + resources :messages end root to: 'chatrooms#index'