user2003009
user2003009

Reputation: 37

Custom Validation error messages not appearing in view

I have some custom validations in my model for when the user submits a URL using simple_form, but I can't seem to get the error message associated with each custom validation to show in the view (yet the validations seem to work)?

The only error I see is the one defined in the create method. Any guidance would be appreciated....

Model

class Product < ActiveRecord::Base

validates :url, presence: true
validate :check_source, :must_contain_product_id

def check_source         

    valid_urls = ["foo", "bar"]

    errors.add(:url, "Must be from foo or bar") unless valid_urls.any? {|mes| self.url.include? mes}
end

def must_contain_product_id         

    errors.add(:url, "Must be product page") unless self.url.include? "productID"

end                                                            

end

Controller

def create

    @product = Product.new


    if @product.save
        flash[:success] = "Product added to your list"
        redirect_to root_path

    else
        flash[:message] = "Sorry we can't add this product"
        redirect_to root_path
    end

end

View (using Simple_form)

# Various messaging I've tried
  <% if flash[:success].present? %>
      <div data-alert class="alert-box success">
        <%= flash[:success] %>
      </div>
  <% end %>

  <% if flash[:error].present? %>
      <div data-alert class="alert-box">
        <%= flash[:error] %>
      </div>
  <% end %>

  <% if flash[:message].present? %>
      <div data-alert class="alert-box">
        <%= flash[:message] %>
      </div>
  <% end %>

  <% if @product.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@product.errors.count, "error") %> prohibited this post from being saved:</h2>

        <ul>
        <% @product.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
        </ul>
      </div>
<% end %>


# The actual form...

<%= simple_form_for Product.new do |form| %>

<%= form.input :url, maxlength: false %>

<%= form.button :submit, "Get Product", data: { disable_with: "Retrieving product ..." } %>

<% end %>

Upvotes: 0

Views: 885

Answers (5)

Jenorish
Jenorish

Reputation: 1714

See your code you are redirecting to root_path if you redirecting means values and variable will not come to your partial.

so you have to change your code like,

def create

  @product = Product.new
  if @product.save
    flash[:success] = "Product added to your list"
    redirect_to root_path

  else
    flash[:message] = "Sorry we can't add this product"
    render :action =>  "new"
  end

end

after that in model Instead of

errors.add(:url, "Must be from foo or bar") 

Please try like this:

errors[:base] << ("Must be from foo or bar")

Upvotes: 0

sunil
sunil

Reputation: 1040

errors.add(:base, "message")

This is the correct syntax to add message in custom validations.

Upvotes: 0

Saggex
Saggex

Reputation: 3500

I think you neet to redirect to products, or to edit. Otherwise you are unable to see the error, because you are not on the page where it should be displayed. Try

def create

   @product = Product.new


   if @product.save
       flash[:success] = "Product added to your list"
       redirect_to root_path

   else
       flash[:message] = "Sorry we can't add this product"
       render 'edit'
   end

end

Upvotes: 0

nhjk
nhjk

Reputation: 7830

You're redirecting to the root_path in the create action regardless of whether or not the save is successful. Your view for the Product form contains divs for the flashes, but unless the view for the root_path renders flash divs as well you won't see them.

Upvotes: 0

Nimir
Nimir

Reputation: 5839

I had the same issue solved using:

errors.add(:base, "message here")

However this won't be assigned to a specific form input (e.g url) which was not necessary in my case

Upvotes: 0

Related Questions