控制器中的Rails 3.2错误:"无法找到ID为&#34的类别;

时间:2013-11-21 02:34:13

标签: ruby-on-rails ruby ruby-on-rails-3 devise

首先,我想说我在轨道上非常新,所以我是一个菜鸟。

当我尝试从应用程序(论坛怪物)root / forums / new访问“新论坛页面”时,我收到此错误:

ActiveRecord::RecordNotFound in ForumsController#new

Couldn't find Category with id=

这是我的用户模型:

class User < ActiveRecord::Base

  include Gravtastic
  gravtastic :size => 165, :filetype => :png, :rating => 'R'

  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  validates :email, :username, :presence => true, :uniqueness => true

  # Setup accessible (or protected) attributes for your model
  attr_accessible :name, :username, :email, :password, :password_confirmation, :remember_me

  has_many :topics, :dependent => :destroy
  has_many :posts, :dependent => :destroy

  def admin?
    true if self.username == 'admin'
  end
end

这是我的路线:

Community::Application.routes.draw do
  resources :categories, :except => [:index, :show]

  resources :forums, :except => :index do
    resources :topics, :shallow => true, :except => :index do
      resources :posts, :shallow => true, :except => [:index, :show]
    end
    root :to => 'categories#index', :via => :get
  end

  devise_for :users
  root :to => 'categories#index', :via => :get

  resources :users

  # The priority is based upon order of creation:
  # first created -> highest priority.

  # Sample of regular route:
  #   match 'products/:id' => 'catalog#view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  #   match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
  # This route can be invoked with purchase_url(:id => product.id)

  # Sample resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Sample resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Sample resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Sample resource route with more complex sub-resources
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', :on => :collection
  #     end
  #   end

  # Sample resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end

  # You can have the root of your site routed with "root"
  # just remember to delete public/index.html.
  # root :to => "welcome#index"

  # See how all your routes lay out with "rake routes"

  # This is a legacy wild controller route that's not recommended for RESTful applications.
  # Note: This route will make all actions in every controller accessible via GET requests.
  # match ':controller(/:action(/:id(.:format)))'
end

这是我的视图文件:

<div class="module">
  <div class="module_header"><%= action_name.humanize %> Forum</div>
    <div class="module_subheader smaller">
      <em>To create a category, leave the category field unselected.</em>
    </div>
    <div class="module_body">
      <%= form_for @forum do |f| %>
      <% if @forum.errors.any? %>
        <% flash.now[:error] = @forum.errors.full_messages.join(', and ') %>
      <% end %>
      <div class="fieldset">
        <span class="label indent smaller">
          <%= f.label :category_id %><br />
          <small>(Required)</small>
        </span>
        <span class="input indent smaller">
          <%= f.collection_select :category_id, Category.all, :id, :title %>
        </span>
        <div class="clear"></div>
      </div>
      <div class="fieldset">
        <span class="label indent smaller">
          <%= f.label :title %><br />
          <small>(Required)</small>
        </span>
        <span class="input indent smaller"><%= f.text_field :title, :size => 75 %></span>
        <div class="clear"></div>
      </div>
      <div class="fieldset">
        <span class="label indent smaller">
          <%= f.label :description %><br />
          <small>(Required)</small>
        </span>
        <span class="input indent smaller"><%= f.text_area :description, :cols => 60, :rows => 5 %></span>
        <div class="clear"></div>
      </div>
      <div class="fieldset">
        <span class="label indent smaller">
          <%= f.label :position %>
        </span>
        <span class="input indent smaller"><%= f.text_field :position %></span>
        <div class="clear"></div>
      </div>
      <div class="fieldset">
        <span class="label indent smaller"></span>
        <span class="input indent smaller">
          <%= f.check_box :state %>
          <%= f.label :state %>
        </span>
        <div class="clear"></div>
      </div>
    </div>
    <div class="module_footer">
      <div class="fieldset">
        <span class="input"><%= f.submit "submit" %> or <%= link_to "cancel", @forum.nil? ? forum_path(@forum) : forums_path %></span>
        <div class="clear"></div>
      </div>
    </div>
    <% end %>
  </div>
</div>

编辑:

这是我的论坛控制器:

class ForumsController < ApplicationController
  load_and_authorize_resource :category
  load_and_authorize_resource :forum, :through => :category, :shallow => true

  def create
    if @forum.save
      flash[:notice] = "Forum was successfully created."
      redirect_to forums_url
    else
      render :action => 'new'
    end
  end

  def update
    if @forum.update_attributes(params[:forum])
      flash[:notice] = "Forum was updated successfully."
      redirect_to forum_url(@forum)
    end
  end

  def destroy
    if @forum.destroy
      flash[:notice] = "Category was deleted."
      redirect_to forums_url
    end
  end
end

这里也是我的分类控制器:

class CategoriesController < ApplicationController
  load_and_authorize_resource :category

  def create
    if @category.save
      flash[:notice] = "Category was successfully created."
      redirect_to forums_url
    else
      render :action => 'new'
    end
  end

  def update    
    if @category.update_attributes(params[:category])
      flash[:notice] = "Category was updated successfully."
      redirect_to forums_url
    end
  end

  def destroy
    if @category.destroy
      flash[:notice] = "Category was deleted."
      redirect_to forums_url
    end
  end
end

EDIT2: 我的gemfile:

source 'http://rubygems.org'

gem 'rails', '3.2.14'
gem 'sqlite3'

gem 'forum_monster', '~> 1.0.3'
gem 'devise'
gem 'cancan'
gem 'gravtastic', :git => 'https://github.com/chrislloyd/gravtastic.git'
gem 'bb-ruby'


group :development do
  gem 'hirb'
  gem 'heroku'
end

group :production do
  gem 'unicorn'
end

对不起是noob,我是新手。

1 个答案:

答案 0 :(得分:1)

我对CanCan没有足够的经验用尽可能多的信心来说这个,但我认为你的load_and_authorize_resource在ForumsController中已经关闭了。

如果您的资源是这样的话,我认为您正在做的事情会有意义:

resources :categories do 
  resources :forums
  ...      

但是,如果没有这种嵌套,论坛控制器中的load_and_authorize_resource :category正在寻找使用不存在的数据初始化@category

如果我的怀疑是正确的(我可能会错误地解释这里发生了什么),我认为修复应该很简单 - 只需将头部的load_and_authorize_resource语句减少为单load_and_authorize_resource个语句没有参数。我不认为这样做会有问题,但我认为这取决于@category背后的安全性。