What is the best way to center my error message in Simple_form for Ruby on Rails?

Currently, my form is displaying an error message (Name is a required field) in this format:

However, I want to position the 'can't be blank' error next to the 'Name', like so:

To achieve this layout, I made modifications to the default bootstrap Simple_form classes using the following code snippet:

<%= f.input :name, :required => true, :label_html => { :class => 'edit_form_titles' }, :error_html => { :class => 'cant_be_blank'} %>

The corresponding CSS for these classes looks like this:

.edit_form_titles{
  display: block;
  margin-bottom: 0px;
  color: #333333;
}

.cant_be_blank{
  font-size:12px;
}

If anyone has suggestions on how I can align the error message next to the name as mentioned above, I would greatly appreciate it. Thank you!

Answer №1

I successfully displayed my errors above the input box.

By adding a class to my errors in the code below, I was able to format their positioning. However, there was always an empty div or something under the input box that affected the alignment of other input boxes below it.

<%= f.input :name, :required => true, :label_html => { :class => 'edit_form_titles' }, :error_html => { :class => 'cant_be_blank'} %>

In my initializers/simple_form.rb file, the original code looked like this:

  config.wrappers :bootstrap, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
    b.use :html5
    b.use :placeholder
    b.use :label
    b.wrapper :tag => 'div', :class => 'controls' do |input|
      input.use :input
      input.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
      input.use :hint,  :wrap_with => { :tag => 'p', :class => 'help-block' }
    end
  end

I made the following changes:

 config.wrappers :bootstrap, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
    b.use :html5
    b.use :placeholder
    b.wrapper :tag => 'div', :class => 'label-error' do |input|
      b.use :label
      b.use :error, :wrap_with => { :tag => 'span', :class => 'help-block' }
    </div>
    b.wrapper :tag => 'div', :class => 'controls' do |ba|
      ba.use :input
      ba.use :hint,  :wrap_with => { :tag => 'p', :class => 'help-block' }
    </end>
  end

This adjustment removed the blank space under the input box, allowing me to properly align the text from my cant_be_blank class right next to the label text.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Determine the Placement of Bootstrap Popover by Utilizing SVG and Mouse Coordinates

Is there a way to dynamically reposition the popover that currently appears to the right of the Circle SVG? I would like to either base its position on the user's mouse click within the CircleSVG or move it to the exact center of the SVG, including bo ...

Table does not have appropriate rowspan and colspan attributes

I've created a table using bootstrap, but it's not displaying as intended. I used row-span and col-span to format the table, and while most rows are correct, some are not appearing on the page. To better illustrate the issue, I have included an ...

The alignment and sizing issues with stacking icons using fontawesome are still unresolved

I have been exploring the use of stacked icons with fontawesome, following a helpful blog post by Dave Gandy. Although stacking is working well, I am facing an issue aligning a stacked icon with a non-stacked icon (which is fa-5x). It's challenging t ...

Attempting to troubleshoot the issues causing my React application to malfunction

Having some trouble with my Spotify project. Every time I search for an artist, I receive an error message saying "illegal redirect_uri." I am also facing a list of coding issues that I am struggling to resolve. Any advice or suggestions would be greatly a ...

Switch between two button designs in a React JS application

Is it possible to have the style of a button change when clicked? Specifically, I would like the option 1 button to be selected and have its background color changed by default. If I click on the option 2 button, I want only that button to change while lea ...

The most straightforward approach to modifying model data in Django through an AJAX-based form

In my Django model, I have defined fields with associated choice options. Here is an example: class Product(models.Model): CONDITION_CHOICES = ( ("GOOD", "Good"), ("BAD", "Bad"), ("UNKNOWN", "Unknown"), ) name = models ...

Unable to select the initial element

Having trouble targeting the first child in this code snippet. I've tried various methods but nothing seems to be working. Any suggestions on how to resolve this? <div id="main"> <div class="page"> <p>random 1</p> </div ...

Why are the UI components (`Card Number`, `MM/YY`, `CVC`) not being displayed in the React app when using Card

Having an issue where the CardElement Ui component is not visible in the image provided. It should appear above the Order Total: $0 next to the payment method. https://i.sstatic.net/NCK5z.png I've attempted various debugging methods without success. ...

Unable to eliminate the default styling of Material UI table using an external CSS file

Currently, I am incorporating a Material Ui table into my project. My goal is to eliminate the border and adjust the padding of the table. Upon investigation, I came across a default className for material ui table known as MuiTableCell-root-40. Below is t ...

The puzzling appearance of the mysterious character ">s" in HTML code

Could someone explain the strange ">s" appearing above the table in this fiddle? It doesn't seem to be part of the HTML code. <table>...</table> http://jsfiddle.net/d2j3x5xh/ ...

Need help fixing my issue with the try-catch functionality in my calculator program

Can someone assist me with my Vue.js calculator issue? I am facing a problem where the output gets added to the expression after using try and catch. How can I ensure that both ERR and the output are displayed in the {{output}} section? Any help would be a ...

Encountering a jQuery error while trying to utilize the $window.load

I have a code snippet that is functioning well when wrapped within a document ready event: jQuery(document).ready(function($) { $('tr[data-name="background_colour"] input.wp-color-picker').each(function() { //this section works fin ...

Images on a webpage can cause the height of a div element to remain

I've encountered a problem with displaying 5 images horizontally on the screen. For some reason, I'm unable to set the height of the Div "block-before-description". In FireBug, the height appears as 0 while the 20px margin is visible. Despite try ...

Designing CSS elements that flow from top to bottom, left to right, and extend to the right when overflowing

I'm attempting to create a layout where divs are displayed from top to bottom, but once they reach the bottom of the browser window, any additional divs will overflow to the right. You can take a look at the desired layout here: https://i.stack.imgur. ...

How can I place a Pure CSS Menu within a Div Container?

I've been experimenting with the Pure CSS menu found on this site: http://csswizardry.com/2011/02/creating-a-pure-css-dropdown-menu/ It's working well, but I'm facing an issue where I want to place this menu inside another div container. H ...

Adjust the color of the border surrounding the icon within a button

Is there a way to customize the color border of an SVG icon within a button to match the text of the button? My code snippet can be found on JSFiddle, along with my usage of Bootstrap: JSFiddle @import 'https://maxcdn.bootstrapcdn.com/bootstrap/4 ...

Transforming the background color of the body upon hovering over a hyperlink

How can I create a hover effect in which the body background color changes when a user hovers over a link within a div? Below is the CSS code snippet: body { background-color: black; } a { color: white; text-decoration: none; } a:hover { color ...

What might be preventing me from achieving a full-length page using height 100% or height 100vh?

I am currently working on a web application that has a similar layout to Slack. I am facing an issue where the page doesn't take up the full width and height as intended. The problem seems to be that it sometimes only occupies half of the screen while ...

Ways to place a png image on top of a video background

How can I overlay a PNG over my background video without it appearing behind? I attempted to use z-index, but the video disappears when changing the position from relative to absolute or fixed. Here is the HTML and CSS code: <div id="page2"> &l ...

What is the reason that the css backdrop-filter: blur() is functioning properly everywhere except on the active bootstrap-4 list-group-item?

I have a gallery with an album-card component inside, and within that is a carousel. I noticed that when I add a list-group and set one of the items as active, it remains unblurred. Can anyone help me understand why? Here is the HTML for the gallery com ...