错误消息部分未显示

时间:2013-12-17 08:35:50

标签: ruby-on-rails

我有一个名为Video的模型,它接受user_id,question和video_cid。

验证似乎已设置,因为如果表单不符合要求,表单将无法保存。但是,错误消息部分显示没有错误消息:(。

以下是模型的外观 - >

# == Schema Information
#
# Table name: videos
#
#  id         :integer          not null, primary key
#  user_id    :integer
#  video_cid  :string(255)
#  question   :string(255)
#  created_at :datetime
#  updated_at :datetime
#

class Video < ActiveRecord::Base
 belongs_to :user

 validates  :user_id, presence: true
 validates  :question, presence: true
 validates  :video_cid, presence: true

end

以下是视频/新视图的外观 - &gt;

<% provide(:title, "Final step, Record a video of yourself") %>

<%= form_for @video do |f| %>
 <%= render 'shared/error_messages', object: f.object %>
 <%= f.label :question %>
 <%= select(:video, :question, 
                [
                    ['Why would you be effective in a sales/business development role in China?', 
                        'Why would you be a valuable addition to an international team in China? '], 
                    ['What is your most significant accomplishment or the best example of your leadership skills in China?', 
                        'What is your most significant accomplishment or the best example of your leadership skills in China?'],
                     ['How would you help solve the biggest challenges Chinese companies and investors face when doing business abroad?',
                        'How would you help solve the biggest challenges Chinese companies and investors face when doing business abroad? ']
                ]) %>

 <%= render 'nimbb' %>
 <%= f.hidden_field :video_cid, value: "" %>
 <%= f.submit "Submit the Video", class: "button" %>
<% end %>

我使用javascript设置隐藏值:video_cid就像这样。如果用户记录他自己的视频,表格在技术上应该只通过,因此更新表格中的隐藏值 - &gt;

// Global variable to hold player's reference.
var _Nimbb;

// Global variable to hold the guid of the recorded video.

// Event: Nimbb Player has been initialized and is ready.
function Nimbb_initCompleted(idPlayer)
{
  // Get a reference to the player since it was successfully created.
  _Nimbb = document[idPlayer];
}

// Event: the video was saved.
function Nimbb_videoSaved(idPlayer)
{
  document.getElementById('video_video_cid').value = _Nimbb.getGuid();
}

这就是控制器的样子 - &gt;

class VideosController < ApplicationController
  before_action :signed_in_user

  def new
    if current_user.video.present?
      redirect_to current_user
    else
      @video = current_user.build_video
    end
  end

  def create 
    @video = current_user.build_video(video_params)
    if @video.save
        flash[:success] = "Video Created!"
        redirect_to root_url
    else
        redirect_to new_video_path
    end
  end


  private

    def video_params
      params.require(:video).permit(:video_cid,:question)
    end
end

这是错误消息部分的样子:

<% if object.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-error">
      The form contains <%= pluralize(object.errors.count, "error") %>.
    </div>
    <ul>
    <% object.errors.full_messages.each do |msg| %>
      <li>* <%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

2 个答案:

答案 0 :(得分:1)

create方法更改为:

def create 
  @video = current_user.build_video(video_params)
  if @video.save
    flash[:success] = "Video Created!"
    redirect_to root_url
  else
    render :new
  end
end

现在,您的应用程序在没有重定向的情况下拒绝表单数据后会直接显示该表单,因此它会在Video操作中实例化create对象,并显示错误。在原始格式中,您在视频保存失败后将用户重定向到新的视频路径,因此new操作再次被触发,并使用新的“干净”Video实例。

答案 1 :(得分:0)

使用

<%= form_for(@video, :validate => true) do |f| %>

而不是

<%= form_for @video do |f| %>

在您的视频控制器中,您的创建方法应该是这样的

def create 
  @video = current_user.build_video(video_params)
  respond_to do |format|
    if @video.save
      flash[:success] = "Video Created!"
      format.html {redirect_to( :controller => "controller_name", :action => "action_name" )}
    else
      format.html {render :action => "action_name"}
    end
  end
end