Rails 4简单形式验证

时间:2014-09-29 18:02:17

标签: ruby-on-rails simple-form

由于某些原因,我的验证无法正常工作。我可以提交表格,如果它是空白的,它将由控制器处理。模型在进入控制器之前是不是要验证它?

我的控制器是:

class SubscribersController < ApplicationController
  before_action :set_subscriber, only: [:show, :edit, :update]

  # GET /subscribers
  # GET /subscribers.json
  def index
    @subscribers = Subscriber.all
  end

  # GET /subscribers/1
  # GET /subscribers/1.json
  def show
    # This is Wrong, but You Get The Idea
    @subscriber = Subscriber.find_by(params[:id])
  end

  # GET /subscribers/new
  def new
    @subscriber = Subscriber.new
  end

  # GET /subscribers/1/edit
  def edit
  end

  # POST /subscribers
  # POST /subscribers.json
    def create
      @subscriber = Subscriber.new(subscriber_params)
      respond_to do |format|
        if @subscriber.save
          format.html { redirect_to :back, notice: 'Thanks For Joining Our Mailing List!' }
          format.json { render :back, status: :created, location: @subscriber }
        else
          format.html { redirect_to :back, notice: 'Error, Invalid Entry, or you are on the List Already!'}
          format.json { redirect_to :back, @subscriber.errors, status: :unprocessable_entity }
        end
      end
    end

    # PATCH/PUT /subscribers/1
    # PATCH/PUT /subscribers/1.json
    def update
      respond_to do |format|
        if @subscriber.update(subscriber_params)
          format.html { redirect_to :back, notice: 'Subscriber was successfully updated.' }
          format.json { redirect_to :back, status: :ok }
        else
          format.html { redirect_to :back }
          format.json { render json: @subscriber.errors, status: :unprocessable_entity }
        end
      end
    end

    # DELETE /subscribers/1
    # DELETE /subscribers/1.json
    def destroy
      if @subscriber = Subscriber.find_by(params[:email])
      respond_to do |format|
          if @subscriber.destroy
          format.html { redirect_to :back, notice: 'Subscriber was successfully destroyed.' }
          format.json { head :no_content }
          else
            format.html { redirect_to :back, notice: 'Something Wrong' }
            format.json { render json: @subscriber.errors, status: :unprocessable_entity }
          end
      end
      else
        redirect_to :back
        flash[:error] = "Couldn't Find That Email Database!"
      end
    end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_subscriber
      @subscriber = Subscriber.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def subscriber_params
      params.require(:subscriber).permit(:name, :email)
    end
end

我的模特是:

class Subscriber < ActiveRecord::Base
    before_validation :downcase_email

    validates_uniqueness_of :email
    validates_format_of :email,:with => Devise::email_regexp
    validates_presence_of :name, :message => "Name can't be blank"
    validates_presence_of :email, :message => "Need an Email!!", on: :destroy


    private
    def downcase_email
      self.email = self.email.downcase if self.email.present?
    end
end

最后我的表格:

<div class="five wide column subscriber">
  <div class="ui inverted form segment">
    <%= simple_form_for(@subscriber) do |f| %>
    <%= f.error_notification %>
    <h3 class="ui header">Join Our Mailing List</h3>
    <div class="two fields">
      <div class="field">
        <%= f.input :name, placeholder: "Enter Name", required: true, error_html: { id: 'password_error'}%>
      </div>
      <div class="field">
        <%= f.input :email, placeholder: "Enter Email", type: "email", required: true%>
      </div>
    </div>
      <%= f.submit "Join", class: "ui blue submit button" %>
    <% end %>
  </div>
</div>

如果我需要提供更多信息,请告诉我。我希望这很容易解决,但我不确定我做错了什么。

0 个答案:

没有答案