Rails - 当输入不为空时,名称不能为空

时间:2016-02-11 02:42:08

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

我在尝试更新名称时收到“名称不能为空”和“名称仅允许字母,数字和' - '”的错误。这工作正常,但我在为它编写测试时再次经历了它,并意识到它不再起作用,我无法弄清楚为什么或改变了什么来打破它。

类别控制器

def update
  if @category.update category_params
    redirect_to new_guide_category_item_path(@guide, @category)
    flash[:info] = "Updated successfully"
  else
    render 'edit'
  end
end

private

def category_params
  params.require(:category).permit(:name, :template)        
end

edit.html.erb

    <%= render 'shared/error_messages', object: f.object %>
      .....
 <%= form_for([@guide, @category], url: guide_category_path) do |f| %>


   <%= f.label :name, "Category name" %>
   <%= f.text_field :name %>

   <%= f.label :template, "Template" %>    
   <%= f.text_area :template, { :id => 'edit' } %>

   <%= f.submit "Save", :value => "Save Template"  %>

<% end %>

模型

 validates :name,  presence: true, length: { maximum: 255 }, uniqueness: { scope:  :guide_id, case_sensitive: false },
  exclusion: { in: %w( guide guides category categories item items page pages post posts tag tags key keys item key item keys item-key item-keys item_key item_keys mod moderator mods moderators admin admins), message: "%{value} cant be taken." },
  format: { with: /\A[a-zA-Z0-9 -]+\z/, message: "only allows letters, numbers, spaces and '-'" }

路由

resources :guides do
    resources :categories,    only: [:new, :create, :edit, :update] do
    end
end

任何人都知道可能出现的问题?

修改添加的日志输出

Started PATCH "/guides/ghj/categories/ijijij" for ::1 at 2016-02-11 14:08:04 +1100
Processing by CategoriesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"IqiUJ8wF8ZY4t8f91+t4aOMc9xenx/F2beKjKs9GY7JzLD6ZgPYDY1ueC2s+OIDL+PjROVtMe2+GvgYdan1CDQ==", "category"=>{"name"=>"ijijijddd", "template"=>""}, "commit"=>"Save Template", "guide_id"=>"ghj", "id"=>"ijijij"}
  [1m[35mUser Load (0.2ms)[0m  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  [1m[36mGuide Load (0.1ms)[0m  [1mSELECT  "guides".* FROM "guides" WHERE "guides"."slug" = ?  ORDER BY "guides"."id" ASC LIMIT 1[0m  [["slug", "ghj"]]
  [1m[35mCategory Load (0.1ms)[0m  SELECT  "categories".* FROM "categories" WHERE "categories"."slug" = ?  ORDER BY "categories"."id" ASC LIMIT 1  [["slug", "ijijij"]]
  [1m[36m (0.1ms)[0m  [1mSELECT "category_item_keys"."name" FROM "category_item_keys" WHERE "category_item_keys"."category_id" = ?[0m  [["category_id", 6]]
  [1m[35m (0.0ms)[0m  SELECT "check_category_item_keys"."name" FROM "check_category_item_keys" WHERE "check_category_item_keys"."category_id" = ?  [["category_id", 6]]
  [1m[36m (0.1ms)[0m  [1mSELECT "category_item_keys"."name" FROM "category_item_keys" WHERE "category_item_keys"."category_id" = ? AND "category_item_keys"."key_type" = ?[0m  [["category_id", 6], ["key_type", 1]]
  [1m[35m (0.0ms)[0m  SELECT "category_item_keys"."name" FROM "category_item_keys" WHERE "category_item_keys"."category_id" = ? AND "category_item_keys"."key_type" = ?  [["category_id", 6], ["key_type", 2]]
  [1m[36m (0.1ms)[0m  [1mSELECT "category_item_keys"."name" FROM "category_item_keys" WHERE "category_item_keys"."category_id" = ? AND "category_item_keys"."key_type" = ?[0m  [["category_id", 6], ["key_type", 3]]
  [1m[35mGameModsRelationship Exists (0.1ms)[0m  SELECT  1 AS one FROM "game_mods_relationships" WHERE "game_mods_relationships"."user_id" = ? AND "game_mods_relationships"."category_id" = 3 LIMIT 1  [["user_id", 1]]
  [1m[36m (0.1ms)[0m  [1mbegin transaction[0m
  [1m[35mCategoryItemKey Exists (0.1ms)[0m  SELECT  1 AS one FROM "category_item_keys" WHERE ("category_item_keys"."name" IS NULL AND "category_item_keys"."guide_id" IS NULL) LIMIT 1
  [1m[36mCategory Exists (0.1ms)[0m  [1mSELECT  1 AS one FROM "categories" WHERE (LOWER("categories"."name") = LOWER('ijijijddd') AND "categories"."id" != 6 AND "categories"."guide_id" = 3) LIMIT 1[0m
  [1m[35m (0.0ms)[0m  rollback transaction
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT  1 AS one FROM "game_mods_relationships" WHERE "game_mods_relationships"."user_id" = ? AND "game_mods_relationships"."category_id" = 3 LIMIT 1[0m  [["user_id", 1]]
  Rendered shared/_error_messages.html.erb (0.5ms)
  Rendered categories/edit.html.erb within layouts/application (5.6ms)
  Rendered layouts/_shim.html.erb (0.1ms)
  Rendered layouts/_header.html.erb (0.7ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 157ms (Views: 145.1ms | ActiveRecord: 1.1ms)`

2 个答案:

答案 0 :(得分:0)

您需要在partial中添加error_messages帮助器标记。它处理所有错误消息并正确显示。

答案 1 :(得分:0)

您的数据库看起来像是具有相同名称的现有记录。尝试输入一个唯一的名称。在您的模型中,它正在检查:唯一性。