【问题标题】:Ruby on Rails Error "Validation failed: Item can't be blank, category4 can't be blank, category1 can't be blank"Ruby on Rails 错误“验证失败:项目不能为空白,类别 4 不能为空白,类别 1 不能为空白”
【发布时间】:2012-01-03 10:05:48
【问题描述】:
  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 %>

【问题讨论】:

  • 你能从你的模型中发布代码吗?此外,您发布的控制器代码中存在一些语法错误(我认为第 3 行应该是项目),您确定这就是您所拥有的吗?

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


【解决方案1】:

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

【讨论】:

  • 现在我只得到Validation failed: Item can't be blank
猜你喜欢
  • 1970-01-01
  • 2021-06-23
  • 2015-06-25
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 1970-01-01
  • 2015-02-02
  • 1970-01-01
相关资源
最近更新 更多