2016-02-11 54 views
0

嘗試更新a時,出現'Name can not be blank'和'Name only只允許字母,數字和' - ''的錯誤名稱。這工作正常,但我剛剛經歷了一遍,同時寫了一個測試,並意識到它不再工作,我無法弄清楚爲什麼或改變了什麼以打破它。Rails - 當輸入不爲空時,名稱不能爲空白錯誤

分類控制器

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

當您嘗試更新類別時,您可以粘貼您的日誌嗎? – JCorcuera

+0

@JCorcuera https://docs.google.com/document/d/1u9yr5vkeuKmTd9clOJb7LID7PDOIqRyy50KeL-CZrWM/edit?usp=sharing將其放入此Google文檔中。 – Rob

+0

請不要讓人們爲了幫助你而跳過籃球。編輯您的問題以包含日誌輸出。 – MarsAtomic

回答

0

您需要在局部添加error_messages幫手標籤。這將處理所有錯誤信息並正確顯示。

+0

在視圖中有一個,但不是直接在表單中。 – Rob

+0

它應該在窗體中或有權訪問實例變量。 –

0

它看起來像你的數據庫有一個同名的現有記錄。嘗試輸入一個唯一的名稱。在您的模型中,它檢查:唯一性。

相關問題