lundi 3 août 2015

Couldn't find Category with 'id'=

I have tags showing in my category show view. When someone views the Mobile Phones Category they will see tags at the top such as iphone, apple, 16gb, black and ect. When someone clicks on 16gb, it should show everything item with 16gb tagged in it.

When someone clicks on a tag i get this error.

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

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

I can't figure out how to fix it?

Im using act_as_taggable gem.

Here is category controller

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

Here is my category show view.

<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>

Here is routes. So you can see how the tags are routed.

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

Aucun commentaire:

Enregistrer un commentaire