Nesiehr
Nesiehr

Reputation: 247

Ruby on Rails Form Submission Error and Success Messages

I'm creating a landing page and I have two forms on my root page (trying to create a landing page). Very new to ruby on rails so forgive me because I'm going to explain this terribly.

The landing page controller (landing_controller) looks like this:

class LandingController < ApplicationController
  def index
    @email = Email.new
    @design = Design.new
  end
end

The emails_controller (emails_controller) looks like this:

class EmailsController < ApplicationController
protect_from_forgery with: :exception

def new
    @email = Email.new
end

def create
  @email = Email.new(params[email_params])

  respond_to do |format|
    if @email.save
      format.html  { redirect_to(root_path, :notice => 'Thank You For Subscribing!') }
      format.json  { render json: Email.create(email_params) }
    else
      format.html  { redirect_to(root_path)}
      format.json  { render :json => @email.errors, :status => :unprocessable_entity }
    end
  end
end

    private

    def email_params
        params.require(:email).permit(:username, :email)
    end
end

and the designs controller (designs_controller) looks pretty much the same as emails_controller.

Then I have some validation in the email.rb model:

class Email < ActiveRecord::Base
    validates :username, :presence => true
    validates :email, :presence => true
end

And again the design.rb looks pretty much the same.

The form I have on the landing page (root_path) index looks like this:

    <%= form_for @email, url: emails_path, html: {class: "email-form"} do |f| %>
    <% if @email.errors.any? %>
        <h3>Error</h3>
        <ul>
            <% @email.errors.full_messages.each do |message| %>
            <li><%= message %></li>
            <% end %>
        </ul>
    <% end %>
    <h2>Receive Notifications</h2>
    <%= f.text_field :username, :class => 'email-box', :placeholder => "First Name", :autocomplete => :off %>
    <%= f.text_field :email , :class => 'email-box', :placeholder => "Email", :autocomplete => :off %>
    <%= f.submit "Subscribe", :class => 'email-submit' %>
    <p class="info">- We'll update ​you ​when ​we launch our new website</p>
    <% end %>

When I submit the form breaking the validation I get no errors and if I submit the form following the validation rules I don't know if it creates a new entry in the database. If anyone can help I'd be very appreciative.

Upvotes: 2

Views: 5048

Answers (1)

Athar
Athar

Reputation: 3268

you need to render the landing controller index action rather than redirecting to it. because on redirection, it does @email = Email.new and all the errors are gone for email. try this as create action in your emails controller

def create
  @email = Email.new(email_params)

  respond_to do |format|
    if @email.save
      format.html  { redirect_to root_path, notice: 'Thank You For Subscribing!' }
      format.json  { render json: Email.create(email_params) }
    else
      @design = Design.new
      format.html  { render "landing/index" }
      format.json  { render :json => @email.errors, :status => :unprocessable_entity }
    end
  end
end

for success or errors messages, put this in your application.html.erb

<% if flash[:error].present? %>
  <p class='flash-error'><%= flash[:error] %></p>
<% end %>
<% if flash[:notice].present? %>
  <p class='flash-notice'><%= flash[:notice] %></p>
<% end %>

Upvotes: 2

Related Questions