2016-03-26 4 views
1

Итак, у меня есть форма (типичные статьи и комментарии) для комментариев внизу страницы. Если проверка не завершена, отображаются ошибки проверки.Rails 4: прокрутить окно браузера, чтобы сформировать после неудачной отправки (ошибки проверки)

Thats мой комментарии код контроллера:

class CommentsController < ApplicationController 
    before_action :authenticate_admin!, only: [:destroy] 
    expose(:article) 
    expose(:comment, attributes: :comment_params) 
    expose(:reply) { Reply.new } 

    def create 
    comment.article = article 
    if verify_recaptcha(model: comment, message: t('captcha_verification_error')) && comment.save 
     flash[:comment_notice] = t('comment_created_successfully') 
     redirect_to article_path(article) + '#comments' 
    else 
     flash[:comment_errors] = comment.errors.full_messages 
     render 'articles/show' 
    end 
    end 

    def destroy 
    comment.destroy 
    redirect_to article_path(article) 
    end 

    private 

    def comment_params 
    params.require(:comment).permit(:author, :content) 
    end 
end 

Вот форма:

= simple_form_for(comment, url: article_comments_path(article)) do |f| 
    - if flash[:comment_errors] 
    .alert.alert-danger 
     strong= pluralize(flash[:comment_errors].count, 'error') + ' prohibited this article from being saved:' 
     - flash[:comment_errors].each do |msg| 
      ul 
      li= msg 
    fieldset class='form-group' 
    = f.label t('author') 
    = f.text_field :author, class: 'form-control', placeholder: t('who_are_you') 
    fieldset class='form-group' 
    = f.label t('content') 
    = f.text_area :content, class: 'form-control', rows: 6, placeholder: t('what_do_you_want_to_say') 
    fieldset class='form-group' 
    = recaptcha_tags 
    fieldset class='form-group' 
    = f.submit t('create_comment'), class: 'btn btn-primary' 

Для форм я использую simple-form. Кроме того, я использую decent exposure и slim

Я хочу моя страница для прокрутки вниз, чтобы сформировать после проверки не удается (так что пользователь не должен прокручивать вручную). Есть ли простой способ достичь этого?

AFAIK Я не могу передать якорь для рендеринга (это решит проблему). Есть идеи?

ответ

0

Так что я решил его с этим JavaScript, помещенной в форму комментария:

javascript: 
    if (document.getElementById("comment_errors")) { 
    location.hash = '#new_comment'; 
    } 

Когда элемент с идентификатором «comment_errors» (Мои ошибки проверки дел) существует, то оно переходит к нему.