0

Hello guys I searched everywhere on how to output form errors but no luck. I tried almost all the code I found on the internet but still didn't work.

Here are some of my files:

view/books/new.html.erb

<div class="container">
  <h2>Create a new book</h2>
  <%= form_with model: @book, url: create_new_book_path do |f| %>
    <% if @book.errors.any? %>
      <ul>
        <% @book.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    <% end %>

    <div class="form-group row">
      <%= label_tag(:title, "Title:", class: "col-sm-2 col-form-label") %>
      <div class="col-sm-10">
        <%= f.text_field :title, class: "form-control" %>
      </div>
    </div>
    <div class="form-group row">
      <%= label_tag(:price, "Price:", class: "col-sm-2 col-form-label") %>
      <div class="col-sm-10">
        <%= f.text_field :price, class: "form-control" %>
      </div>
    </div>
    <div class="form-group row">
      <%= label_tag(:subject_id, "Subject:", class: "col-sm-2 col-form-label") %>
      <div class="col-sm-10">
        <%= f.collection_select :subject_id, @subjects, :id, :name, class: "form-control" %>
      </div>
    </div>
    <div class="form-group row">
      <%= label_tag(:description, "Book description:", class: "col-sm-2 col-form-label") %>
      <div class="col-sm-10">
        <%= f.text_area :description, class: "form-control" %>
      </div>
    </div>
    <%= submit_tag "Create book", class: "btn btn-success" %>

    <%= link_to "Cancel", books_path, class: "btn btn-danger" %>
  <% end %>
  <br/>
</div>

books_controller.rb

class BooksController < ApplicationController
  def index
    @books = Book.all
  end

  def show
    @book = Book.find(params[:id])
  end

  def new
    @book = Book.new
    @subjects = Subject.all
  end

  def create
    @book = Book.new(book_params)
  end

  def book_params
    params.require(:book).permit(:title, :price, :subject_id, :description)
  end

  def edit
    @book = Book.find(params[:id])
    @subjects = Subject.all
  end

  def update
    @book = Book.find(params[:id])
    if @book.update_attributes(book_params)
      redirect_to action: "index"
    else
      @subjects = Subject.all
      render action: "edit"
    end
  end

  def destroy
    Book.find(params[:id]).destroy
    redirect_to action: "index"
  end
end

routes.rb

  get 'books/new', to: 'books#new', as: 'new_book'
  post 'books/create', to: 'books#create', as: 'create_new_book'

view/layouts/application.html.erb

<main role="main">
  <%= yield %>
</main>

I don't really know what I'm missing to get the errors, grateful for your answers!!

4
  • in which action it fails ? Commented Sep 8, 2020 at 22:57
  • @roshiend it doesnt really show I guess? but when I checked thhe browser console its a get error response as far as i can remember Commented Sep 8, 2020 at 23:05
  • where do you want the errors? like in a bar above the form? Commented Sep 8, 2020 at 23:09
  • @behraaang Would love to have the errors below or above their corresponding textfield Commented Sep 8, 2020 at 23:10

1 Answer 1

1

try putting them in a flash like this and render it in your view

  def create
    @book = Book.new
    if @book.update(book_params)
      flash[:notice] = 'success'
      redirect_to action: 'index'
    else
      flash[:alert] = @book.errors.full_messages.join(', ')

      redirect_to action: 'index'
    end
  end

and in your view render those flashes like

<% if flash[:notice]%>
  <span class='notice'><%= flash[:notice] %></span>
<% end %>
<% if flash[:alert]%>
  <span class='alert'><%= flash[:alert] %></span>
<% end %>
Sign up to request clarification or add additional context in comments.

6 Comments

It finally works!! What would I need to do to be able to separate each error?
you could work with the @contact.errors.full_messages to do what you want also please accept the answer if you got yours :)
How should I render it that way in my view since they are now supposedly in two different flashes?
that was just to see the errors as a whole you can use something called simple_form which gives you the errors on each label, or you could implement it yourself, take a look at the answer on this stackoverflow.com/questions/36946608/…
and the third answer on this stackoverflow.com/questions/7878662/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.