so have form (typical articles , comments example) comments @ bottom of page. if validation fails display validation errors.
thats comments controller code:
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
here form:
= simple_form_for(comment, url: article_comments_path(article)) |f| - if flash[:comment_errors] .alert.alert-danger strong= pluralize(flash[:comment_errors].count, 'error') + ' prohibited article being saved:' - flash[:comment_errors].each |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'
for forms i'm using simple-form. i'm using decent exposure , slim
i want my page scroll down form after validation fails (so user don't have scroll manually). there simple way achieve that?
afaik can't pass anchor render (that solve problem). ideas?
so solved javascript placed in comment form:
javascript: if (document.getelementbyid("comment_errors")) { location.hash = '#new_comment'; }
when element id 'comment_errors' (my validation errors div) exists jumps it.
Comments
Post a Comment