mercredi 23 septembre 2015

Validation errors not showing up - Rails 4

For some reason validation errors are not showing up.

my form

<%= form_for [@question.category, @question] do |f| %>

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

      <ul>
      <% @question.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <div class="field panel">
    <%= f.label :question_type %><br>
    <%= f.select :question_type, [ ["single","single"],["multiple","multiple"] ], selected: f.object.question_type %>
  </div>
  <div class="field panel">
    <%= f.label :description %><br>
    <%= f.text_field :description %>
  </div>
  <div class="field panel">
    <%= f.label :image %><br>
    <% if @question.image? %>
      <div class="explanation-image text-center">
        <%= image_tag @question.image_url(:resized) %>
        <p>
          <label>
            <%= f.check_box :remove_image %>
            Remove image
          </label>
        </p>
      </div>
    <% end %>
    <%= f.file_field :image %>
    <%= f.hidden_field :image_cache %>
  </div>
  <div class="field panel">
    <%= f.label :explanation %><br>
    <%= f.text_area :explanation, size: "30x10" %>
  </div>
  <div class="field panel">
    <%= f.label :link_name %><br>
    <%= f.text_field :link_name %>
  </div>
  <div class="field panel">
    <%= f.label :link_url %><br>
    <%= f.text_area :link %>
  </div>
  <div class="field panel">
    <%= f.label :video_url %><br>
    <%= f.text_area :video_url %>
  </div>
  <div class="field panel">
    <%= f.label :category_id %><br><%= @category.title %>
    <%= f.hidden_field :category_id %>
  </div>
  <div class="actions">
    <br>
    <%= f.submit 'Submit', class:"button round success" %> <%= link_to 'Back', category_questions_path, class: "button round alert" %>
  </div>
<% end %>

this is the model

class Question < ActiveRecord::Base
  belongs_to :category
  has_many :choices
  mount_uploader :image, ImageUploader

  validates :description, length: {
    minimum: 6
  }
  validates :link, presence: true
end

and parts of the controller

def edit
    @category = Category.find(params[:category_id])
    @question = @category.questions.find_by(id: params[:id])
  end

def update
    respond_to do |format|
      if @question.update(question_params)
        format.html { redirect_to category_question_url(@question.category, @question), notice: 'Question was successfully updated.' }
        format.json { render :show, status: :ok, location: @question }
      else
        format.html {
          @category = Category.find(params[:category_id])
          @question = @category.questions.find_by(id: params[:id])
          render action: :edit
        }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end

the error messages code is directly from the scaffolding. i haven't touched it. if i try to edit a question and save it while lets say link field is empty it will reload the edit action correctly but no error message will pop up.

Any clues?

Aucun commentaire:

Enregistrer un commentaire