Skip to content
This repository
Browse code

add flash

  • Loading branch information...
commit be30afcdf593bb7812fea5b6f2505ef023695bd6 1 parent 0e867d6
Jan Bernacki releu authored
24 app/assets/javascripts/cache.js.coffee
... ... @@ -1,4 +1,20 @@
  1 +class @Flash
  2 + constructor: ->
  3 + @div = $('div#flash')
  4 + update: (type, message) ->
  5 + $div = @div
  6 + $div
  7 + .removeClass()
  8 + .addClass(type)
  9 + .html(message)
  10 + .fadeIn =>
  11 + setTimeout @hide, 3000
  12 + hide: =>
  13 + @div.fadeOut('slow')
  14 +
1 15 $ ->
  16 + window.flash = new Flash
  17 +
2 18 # Posts
3 19 $posts = $('article.post')
4 20 $posts.each (i, html) ->
@@ -96,4 +112,10 @@ $ ->
96 112 done: ->
97 113 $comment.remove()
98 114 # if last comment show that there is no comments
99   -
  115 +
  116 + $new_comment = $('div.new-comment-form form:first')
  117 + $new_comment.on 'ajax:success', (e, data) ->
  118 + console.log(e)
  119 + console.log(data)
  120 + $new_comment.on 'ajax:error', (e, xhr) ->
  121 + window.flash.update 'error', xhr.responseText
24 app/assets/stylesheets/styles.less
@@ -634,4 +634,26 @@ a.secondary {
634 634 width: 42px;
635 635 height: 42px;
636 636 }
637   -}
  637 +}
  638 +
  639 +div#flash {
  640 + position: fixed;
  641 + z-index: 100;
  642 + font-weight: bold;
  643 + top: 0;
  644 + left: 0;
  645 + width: 100%;
  646 + text-align: center;
  647 + color: white;
  648 + line-height: 30px;
  649 + display: none;
  650 +
  651 + &.error {
  652 + background: #FF7E79;
  653 + border-bottom: 2px solid #FF7E79 * 1.5;
  654 + }
  655 + &.notice {
  656 + background: #00ce00;
  657 + border-bottom: 2px solid #00ce00 * 1.5;
  658 + }
  659 +}
8 app/controllers/application_controller.rb
@@ -2,9 +2,11 @@ class ApplicationController < ActionController::Base
2 2 enable_authorization
3 3 protect_from_forgery
4 4
5   - rescue_from Exception, with: :notify_batman
6   - rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
7   - rescue_from CanCan::Unauthorized, with: :handle_unauthorized
  5 + if Rails.env.production?
  6 + rescue_from Exception, with: :notify_batman
  7 + rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
  8 + rescue_from CanCan::Unauthorized, with: :handle_unauthorized
  9 + end
8 10 helper_method :user_signed_in?, :current_user, :sidebar_tags
9 11
10 12 prepend_before_filter :token_authentication
22 app/controllers/posts/comments_controller.rb
@@ -4,17 +4,14 @@ class Posts::CommentsController < ApplicationController
4 4
5 5 def create
6 6 @post = find_post(params[:post_id])
7   - @new_comment = @post.comments.build(params[:comment]) do |comment|
  7 + @comment = @post.comments.build(params[:comment]) do |comment|
8 8 comment.user = current_user
9 9 end
10   -
11   - if @new_comment.save
12   - @comment = @post.comments.build do |comment|
13   - comment.user = current_user
14   - end
15   - render json: { form: render_form(:build), comment: render_comment }
  10 + if @comment.save
  11 + render json: {}, status: :created
16 12 else
17   - render_json_error("We are sorry, but comment couldn't be saved.")
  13 + errors = @comment.errors.to_a.to_sentence
  14 + render text: errors, status: :unprocessable_entity
18 15 end
19 16 end
20 17
@@ -48,13 +45,4 @@ def destroy
48 45 def find_post(id)
49 46 Post.find_by_param id
50 47 end
51   -
52   - def render_form(action)
53   - @action = action
54   - render_to_string(inline: "<%= render partial: 'form', locals: { comment: @comment, action: @action } %>")
55   - end
56   -
57   - def render_comment
58   - render_to_string(inline: "<%= render partial: 'comment', locals: { comment: @new_comment } %>")
59   - end
60 48 end
1  app/views/layouts/new_application.html.slim
@@ -54,3 +54,4 @@ html
54 54 li
55 55 a class="secondary" href="#" Map
56 56 div class="clear"
  57 + div id="flash"
8 app/views/posts/_comments.html.slim
@@ -45,10 +45,10 @@ section class="comments"
45 45 = link_to current_user, title: current_user.username do
46 46 = avatar_image current_user, 84
47 47 div class="new-comment-form"
48   - = form_for [@post, @comment] do |f|
  48 + = form_for @comment, url: post_comments_path(@post, format: :js), remote: true do |f|
49 49 fieldset
50   - = f.text_area :body, rows: 3
  50 + = f.text_area :content, rows: 3
51 51 div class="form-actions"
52   - = f.submit 'Preview', class: 'button'
53   - = f.submit 'Comment', class: 'button'
  52 + = f.submit 'Preview', class: 'button', data: { action: api_markdown_path }
  53 + = f.submit 'Comment', class: 'button', data: { action: url_for([@post, :comments]) }
54 54 div class="clear"

0 comments on commit be30afc

Please sign in to comment.
Something went wrong with that request. Please try again.