为什么这不验证该领域?

时间:2015-07-20 12:01:35

标签: ruby-on-rails ruby-on-rails-4

模型:

void Ordina_sol_iniziale() 
{
    int temp = 0;
    int temp2 = 0;
    for (int i = 0; i <= nzaini; i++) //5 rows
    {
        for (int y = 0; y <= nogg; y++) //30 elements of each row
        {
            temp = sol_iniziale[i][y];
            temp2 = sol_iniziale[i][y + 1];
            if (temp < temp2)
            {
                sol_iniziale[i][y] = temp;
            }
            else
            { 
                sol_iniziale[i][y] = temp2;
                sol_iniziale[i][y+1] = temp;
            }
        }
    }
}

路线:

class Video < ActiveRecord::Base
  belongs_to :user

  validates :title, presence: true
end

class User < ActiveRecord::Base
  devise  :database_authenticatable, 
          :registerable,
          :recoverable, 
          :rememberable, 
          :trackable, 
          :validatable
  has_many :videos
end

形式:

  devise_for :users

  resources :users do
    resources :videos
  end

  get 'persons/profile', as: 'user_root'

视频控制器:

<%= form_for [current_user, @video] do |f| %>
    <%= f.text_field :title %>
    <%= f.file_field :video %>
    <%= f.submit %>
<% end %>

如果我发送带有填充字段&#39; title&#39;的表单,则该记录将添加到数据库中。但是,如果我将表单发送到空白字段&#39;标题&#39;,我会收到以下错误消息:

  

def new @video = Video.new end def create p '---------------------' p current_user p '==================' if params[:video][:video] filename = params[:video][:video].original_filename else filename = nil end video = current_user.videos.create( title: params[:video][:title], filename: filename ) if video.save flash[:success] = :post_saved redirect_to user_videos_path(current_user) else flash.now[:error] = :post_not_saved render 'new' end end def video_params params.require(:video).permit(:title, :video) end

控制台显示以下内容:

ArgumentError in Videos#create Showing
  /home/kalinin/rails/vd/app/views/videos/_form.html.erb where line #1
  raised: First argument in form cannot contain nil or be empty

2 个答案:

答案 0 :(得分:4)

您尚未在@video操作中初始化/定义create,因此在发生错误时,它会尝试呈现新的&#39;模板并将@video设为nil

尝试更新create操作,如下所示:

...

@video = current_user.videos.build(
                                    title: video_params[:title], # use your whitelist 
                                    filename: filename
        )

if @video.save

...

答案 1 :(得分:1)

更改 def create p '---------------------' p current_user p '==================' if params[:video][:video] filename = params[:video][:video].original_filename else filename = nil end @video = current_user.videos.create( title: params[:video][:title], filename: filename ) if @video.save flash[:success] = :post_saved redirect_to user_videos_path(current_user) else flash.now[:error] = :post_not_saved render 'new' end end 操作

video

无论您将@video更改为<include android:id="@+id/tool_bar" layout="@layout/tool_bar" ></include>