2013-12-17 123 views
-1

我有一個名爲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 

下面是視頻/新觀點看起來像 - >

<% 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的隱藏值,如下所示:在技術上,只有當用戶記錄自己的視頻時纔會通過該窗體,從而更新表單中的隱藏值 - >

// 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(); 
} 

這是控制器的樣子 - >

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 %> 

回答

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實例。

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 
+0

再次檢查。我已經更新了我的答案。 – Jeet