Ruby on Rails validation errors

I am super newb ruby ​​/ rails, so you will need to talk to me.

I created a small application that takes an email address and saves it, I set up validation on models (a unique and valid email address), and they both work.

I use the code below to try to save the email if it already exists, or its invalid format that needs to be stopped, and set an error message

def create
    interest = KnownInterest.new( :email => params[:email] )
    if(interest.valid? and interest.save)
        flash[:notice] = "Thanks for showing interest, We'll be in touch with updates."
    else
        flash[:notice] = interest.errors.messages
    end     
    redirect_to action: "index"
end

it spits out ["Email not valid"], how can I make it be a string (not what I think is an array, correct me if I am wrong)

Thank you so much

+5
source share
4 answers

, interest.errors.messages.first. , - interest.errors.messages.join(", ") .

ActiveRecord .
:

http://guides.rubyonrails.org/active_record_validations_callbacks.html

:

  • 7:
  • 8:
+9

.messages . .

, :

- for error in flash[:notice] do
  = error

, html.erb:

<%- for error in flash[:notice] do %>
    <%= error %>
<%- end %>
+4

:

<html>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">
      <% flash.each do |key, value| %>
        <div class="alert alert-<%= key %>"><%= value %></div>
      <% end %>
      <%= yield %>
      <%= render 'layouts/footer' %>
      <%= debug(params) if Rails.env.development? %>
    </div>

  </body>
</html>
0

interest.errors.messages.join(<any concatenating character>) , .

- , ",", ":" , .

-1

All Articles