简体   繁体   中英

Custom validate doesn't appear to be adding errors

I am trying to stop a user voting for themselves using validation.

Submitting a self vote renders the basic flash "Vote not accepted." but doesn't add the validation error "You cannot vote for your self".

How can I add the error to the flash?

In my model I have

class Imagevote < ActiveRecord::Base
    validate :cannot_vote_for_self


    def cannot_vote_for_self
        if voter_id == voted_id
        errors.add(:base, "You cannot vote for your self")
        end
    end
end

In my controller I have

class ImagevotesController < ApplicationController

    def create
        @imagevote = Imagevote.new(imagevote_params)
        @collection = Collection.find(params[:imagevote][:collection_id])
        @imagevote.voted_id = @collection.user_id
        if @imagevote.save
            flash[:success] = "Vote accepted."
            redirect_to @imagevote
        else
            flash[:success] = "Vote not accepted."
            redirect_to :back
        end
    end

    def update
        @imagevote = Imagevote.find(params[:id])
        @collection = Collection.find(params[:imagevote][:collection_id])
        @imagevote.voted_id = @collection.user_id
        if @imagevote.update_attributes(imagevote_params)
            flash[:success] = "Vote changed."
            redirect_to @imagevote
        else
        flash[:success] = "Vote not accepted."
            redirect_to :back
        end
    end

I think the errors are inserted here in my application layout view

<% flash.each do |key, value| %>
        <%= content_tag(:div, raw(value), class: "alert alert-#{key}") %>
<% end %> 

Following

errors.add(:base, "You cannot vote for your self")

would add errors to an instance of Model Imagevote . They will not be added to flash hash.

In order to display the validation error messages , you need following code in

<% if @imagevote.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@imagevote.errors.count, "error") %>
        prohibited this user from being saved:</h2>
      <ul>
        <% @imagevote.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %> 

in the view where you are creating/updating an Imagevote object.

EDIT

Update the cannot_vote_for_self validation method as below:

   def cannot_vote_for_self
        if voter_id == voted_id
        errors.add(:vote, "You cannot vote for your self")
        end
    end

Set the flash message in controller action as below:

  def create
        @imagevote = Imagevote.new(imagevote_params)
        @collection = Collection.find(params[:imagevote][:collection_id])
        @imagevote.voted_id = @collection.user_id
        if @imagevote.save
            flash[:success] = "Vote accepted."
            redirect_to @imagevote
        else
            flash[:alert] = "Vote not accepted."
            flash[:alert] << @imagevote.errors[:vote].first unless @imagevote.errors[:vote].nil?
            redirect_to :back
        end
    end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM