2015-08-24 84 views
0

我的標籤顯示在我的分類中顯示視圖。當有人查看手機類別時,他們會看到頂部的標籤,如iphone,蘋果,16GB,黑色等。當有人點擊16GB時,它應該顯示每個帶有16GB標籤的項目。Act With Taggable Error

我在這個錯誤上工作了12個小時,無法修復它。

當有人點擊標籤時,我得到這個錯誤。

ActiveRecord::RecordNotFound in CategoriesController#show 
Couldn't find Category with 'id'= 

    def set_category 
     @category = Category.find(params[:id]) 
    end 

我想不出如何解決它?

即時通訊使用act_as_taggable寶石。

這裏是類別控制器

class CategoriesController < ApplicationController 
    before_action :set_category, only: [:show] 
    before_action :admin_user,  only: [:destroy, :index, :edit] 

    def index 
    @categories = Category.all 
    end 

    def show 
    if params[:tag] 
     @items = Item.tagged_with(params[:tag]) 
    else 
     @items = Item.where(category_id: @category.id).order("created_at DESC") 
    end 
    end 

    def new 
    @category = Category.new 
    end 

    def edit 
    end 

    def create 
    @category = Category.new(category_params) 

    respond_to do |format| 
     if @category.save 
     format.html { redirect_to @category, notice: 'Category was successfully created.' } 
     format.json { render :show, status: :created, location: @category } 
     else 
     format.html { render :new } 
     format.json { render json: @category.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def update 
    respond_to do |format| 
     if @category.update(category_params) 
     format.html { redirect_to @category, notice: 'Category was successfully updated.' } 
     format.json { render :show, status: :ok, location: @category } 
     else 
     format.html { render :edit } 
     format.json { render json: @category.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @category.destroy 
    respond_to do |format| 
     format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 

    def set_category 
     @category = Category.find(params[:id]) 
    end 

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

    # Confirms an admin user. 
    def admin_user 
     redirect_to(root_url) unless current_user.try(:admin?) 
    end 

end 

這裏是我的類節目視圖。

<p id="notice"><%= notice %></p> 

<div class = "col-md-3"> 
    <h1> 
     <strong><%= @category.name %></strong> 
    </h1> 
</div> 

<div class = "col-md-9"> 
    <div id="tag_cloud"> 
     <% tag_cloud Item.tag_counts, %w[s m l] do |tag, css_class| %> 
     <%= link_to tag.name, tag_path(tag.name), class: css_class %> 
     <% end %> 
    </div> 
</div> 

<div class = "col-md-12"> 
    <div class="line-separator"></div> 
</div> 

<div class = "col-md-12"> 
    <div id="items" class="transitions-enabled"> 
     <% @items.each do |item| %> 
      <div class="box panel panel-default"> 
       <div class="itemlisttitle"><%= item.title %></div>  
      <%= link_to image_tag(item.image.url (:medium)), item %> 
       <div class ="panel-body"> 
       <div class = "itemlistprice">$<%= item.price %></div> 
       <div class = "itemlistretailer"><%= image_tag item.user.avatar(:thumb) %> Retailer: <%= link_to item.user.username, item.user %></div> 
      </div> 
      </div> 
     <% end %> 
    </div> 
</div> 

這裏是路線。所以你可以看到標籤是如何路由的。

Rails.application.routes.draw do 


     resources :categories 

     get 'password_resets/new' 

     get 'password_resets/edit' 

     get 'sessions/new' 

     resources :users 
     get 'user_items' => 'users#show_user_items' 
     root 'items#home' 
     get 'signup' => 'users#new' 
     get 'show' => 'users#show' 
     get 'login' => 'sessions#new' 
     post 'login' => 'sessions#create' 
     delete 'logout' => 'sessions#destroy' 
     resources :account_activations, only: [:edit] 
     resources :password_resets,  only: [:new, :create, :edit, :update] 

     resources :items 
     get 'items_new' => 'items#new' 

     get 'tags/:tags', to: 'categories#show', as: :tag 

Schema.rb

ActiveRecord::Schema.define(version: 20150731101116) do 

    create_table "categories", force: :cascade do |t| 
    t.string "name" 
    t.string "ancestry" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    add_index "categories", ["ancestry"], name: "index_categories_on_ancestry" 

    create_table "items", force: :cascade do |t| 
    t.string "title" 
    t.decimal "price" 
    t.text  "description" 
    t.datetime "created_at",   null: false 
    t.datetime "updated_at",   null: false 
    t.integer "user_id" 
    t.string "image_file_name" 
    t.string "image_content_type" 
    t.integer "image_file_size" 
    t.datetime "image_updated_at" 
    t.integer "category_id" 
    end 

    add_index "items", ["user_id", "created_at"], name: "index_items_on_user_id_and_created_at" 
    add_index "items", ["user_id"], name: "index_items_on_user_id" 

    create_table "taggings", force: :cascade do |t| 
    t.integer "tag_id" 
    t.integer "taggable_id" 
    t.string "taggable_type" 
    t.integer "tagger_id" 
    t.string "tagger_type" 
    t.string "context",  limit: 128 
    t.datetime "created_at" 
    end 

    add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true 
    add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context" 

    create_table "tags", force: :cascade do |t| 
    t.string "name" 
    t.integer "taggings_count", default: 0 
    end 

    add_index "tags", ["name"], name: "index_tags_on_name", unique: true 

    create_table "users", force: :cascade do |t| 
    t.string "username" 
    t.string "email" 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    t.string "password_digest" 
    t.string "remember_digest" 
    t.boolean "admin",    default: false 
    t.string "activation_digest" 
    t.boolean "activated",   default: false 
    t.datetime "activated_at" 
    t.string "reset_digest" 
    t.string ">" 
    t.datetime "reset_sent_at" 
    t.string "avatar_file_name" 
    t.string "avatar_content_type" 
    t.integer "avatar_file_size" 
    t.datetime "avatar_updated_at" 
    t.text  "description" 
    end 

    add_index "users", ["email"], name: "index_users_on_email", unique: true 

end 

回答

1

實際參數發送標記的名稱show動作會工作您只發送params [:tag],但要找到您需要發送id的類別,請嘗試以下步驟

更改

<%= link_to tag.name, tag_path(tag.name), class: css_class %> 

<%= link_to tag.name, tag_path(tag.name, id: @category.id), class: css_class %> 

,並再次檢查

+0

你需要使用'tag_path(標籤:tag.name,ID:tag.id)'因爲這是標籤的ID被點擊。 '@ category.id'是當前正在查看的那個ID,這不是您想要在鏈接中發送的內容。 –

+0

但即使如此,這可能無法正常工作,因爲當路由僅期望一個時,您會發送兩個參數。 –

+0

謝謝。這工作。 – joeyk16

0

當標籤tag.name用戶點擊被放在作爲參數和路由轉到categories控制器的show行動,命名參數tagsshow操作有一個before_action方法集,它嘗試根據id找到相關的Category對象,該對象從名爲id的參數中獲取。

當用戶點擊標籤後set_category方法運行時,沒有參數id

變化的方法來此

def set_category 
    if params[:tag] 
    @category = Category.find_by_name(params[:tag]) 
    end 
    if params[:id] 
    @category = Category.find(params[:id]) 
    end 
end 

有了這個代碼,如果你是一個名爲:tag參數或ID作爲所謂:id

相關問題