Ruby on Rails错误“验证失败:项目不能为空,category4不能为空,category1不能为空”

时间:2011-11-21 22:59:36

标签: ruby-on-rails ruby forms validation input

  def create
    item = current_administrator.items.build(params[:category1])
    item.created_at = Time.now
    item.save!
    redirect_to root_path
  end

代码不会item.save!,因为它认为表单中的类别是空白的,但它们已经完成。

Validation failed: Item can't be blank, Category4 can't be blank, Category1 can't be blank

表格:

<%= form_for VerbRegular.new do |f| %>
  <div id="add_verb_container">
    <%= f.text_field :category1 %>
    <%= f.text_field :category2 %>
    <%= f.text_field :category3 %>
    <%= f.check_box :type %>
    <%= f.text_field :category4 %>
    <%= f.text_field :comment %>
  </div>
  <%= f.submit "Add to list" %>
<% end %>

1 个答案:

答案 0 :(得分:0)

build(params[:category1])?只是传递 category1 ?可能你需要更像build(params[:verb_regular])

的东西