2017-02-03 99 views
0

我對Rails非常陌生,遇到了一個問題,如果有人能夠解釋我做錯了什麼,我真的很感激它只是。在這裏看到了很多類似的問題,我不能確定我的錯誤發生在哪一部分。Rails/ActiveAdmin關聯錯誤 - 沒有路由匹配...缺少必需的關鍵字:[:category_id]

我有一個非常基本的網站,工作非常好,我添加了ActiveAdmin。都好。那時我決定以類別的形式介紹一段關係。以前,用戶可能有多個狀態。變更後:

  • 用戶可以具有多個狀態
  • 狀態都屬於一個類別
  • 一類可以與許多狀態有關
  • 類可以通過管理面板來管理
  • 類別無法通過公共網站直接管理

這是我破了東西的地方。網站的公開一方仍然允許我對狀態進行CRUD操作。不過,我現在無法訪問ActiveAdmin頁面的任何,因爲該錯誤僅在嘗試將狀態與類別關聯後才引入。

如果任何人可以指出我的問題的方向,或提供任何額外的閱讀它將非常感激。先謝謝你。

完整的錯誤

http://0.0.0.0:3000/admin回報

Showing /home/vagrant/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/bundler/gems/activeadmin-3941e1550c4d/app/views/active_admin/page/index.html.arb where line #2 raised: 

No route matches {:action=>"index", :controller=>"admin/statuses"} missing required keys: [:category_id] 
Extracted source (around line #2): 
insert_tag active_admin_application.view_factory["page"] 

schema.rb

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

    create_table "active_admin_comments", force: true do |t| 
    t.string "namespace" 
    t.text  "body" 
    t.string "resource_id", null: false 
    t.string "resource_type", null: false 
    t.integer "author_id" 
    t.string "author_type" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    add_index "active_admin_comments", ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id" 
    add_index "active_admin_comments", ["namespace"], name: "index_active_admin_comments_on_namespace" 
    add_index "active_admin_comments", ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id" 

    create_table "admin_users", force: true do |t| 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.string "current_sign_in_ip" 
    t.string "last_sign_in_ip" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    add_index "admin_users", ["email"], name: "index_admin_users_on_email", unique: true 
    add_index "admin_users", ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true 

    create_table "categories", force: true do |t| 
    t.string "cat_name", null: false 
    end 

    create_table "statuses", force: true do |t| 
    t.text  "content" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "user_id" 
    t.boolean "private",  default: false 
    t.integer "category_id" 
    end 

    add_index "statuses", ["category_id"], name: "index_statuses_on_category_id" 
    add_index "statuses", ["user_id"], name: "index_statuses_on_user_id" 

    create_table "users", force: true do |t| 
    t.string "first_name" 
    t.string "last_name" 
    t.string "profile_name" 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.string "current_sign_in_ip" 
    t.string "last_sign_in_ip" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "provider" 
    t.string "uid" 
    end 

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

end 

的routes.rb

Rails.application.routes.draw do 

    devise_for :admin_users, ActiveAdmin::Devise.config 
    ActiveAdmin.routes(self) 
    get '/dashboard', to: 'profiles#dashboard', as: :dashboard 

    get 'profiles/show' 

    get '/home', to: 'pages#home', as: :homepage 
    get '/about', to: 'pages#about', as: :aboutpage 
    get '/credits', to: 'pages#credits', as: :creditspage 

    devise_for :users, :controllers => {:registrations => "users/registrations"} 

    devise_scope :user do 
    get 'register', to: 'devise/registrations#new', as: :register 
    get 'login', to: 'devise/sessions#new', as: :login 
    get 'logout', to: 'devise/sessions#destroy', as: :logout 
    end 

    resources :statuses 

    get 'feed', to: 'statuses#index', as: :feed 
    root to: 'pages#home' #Nothing in the path - index 

    get '/:id', to: 'profiles#show' 
end 

模式

class Category < ActiveRecord::Base 
    has_many :statuses 
end 

class Status < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :category 

    validates :content, presence: true, length: {minimum: 2} 
    validates :user_id, presence: true 
end 

控制器 statuses_controller.rb

class StatusesController < ApplicationController 
    before_filter :authenticate_user!, only: [:new, :create, :edit, :update] 
    before_action :set_status, only: [:show, :edit, :update, :destroy] 
    before_action :correct_user, only: [:destroy, :edit] 

    def index 
    @statuses = Status.paginate(page: params[:page], per_page: 50).order('created_at DESC') 
    end 

    def show 
    if can? :read, @status 
     render action: :show 
    else 
     render file: 'public/pleaseregister' 
    end 
    end 

    def new 
    @status = Status.new 
    end 

    def edit 
    end 

    def create 
    @user = current_user 
    @status = @user.statuses.build(status_params) 

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

    def update 
    if can? :update, @status 
    respond_to do |format| 

     if @status.update(status_params) 

      format.html { redirect_to @status, notice: 'Status was successfully updated.' } 
      format.json { render :show, status: :ok, location: @status } 
     else 
      format.html { render :edit } 
      format.json { render json: @status.errors, status: :unprocessable_entity } 
     end 

    end 
    end 
    end 

    def destroy 
    if can? :delete, @status 
     @status.destroy 
     respond_to do |format| 

      format.html { redirect_to statuses_url, notice: 'Status was successfully destroyed.' } 
      format.json { head :no_content } 
     end 
    end 
    end 


    private 
    def set_status 
     @status = Status.find(params[:id]) 
    end 

    def status_params 
     params.require(:status).permit(:name, :content, :user_id, :private, :category_id) 
    end 

    def correct_user 
     @status = current_user.statuses.find_by(id: params[:id]) 
     redirect_to root_url if @status.nil? 
    end 
end 

categories_controller.rb

class CategoriesController < ApplicationController 

end 

耙路線

Prefix Verb  URI Pattern             Controller#Action 
       new_admin_user_session GET  /admin/login(.:format)           active_admin/devise/sessions#new 
        admin_user_session POST  /admin/login(.:format)           active_admin/devise/sessions#create 
      destroy_admin_user_session DELETE|GET /admin/logout(.:format)          active_admin/devise/sessions#destroy 
       admin_user_password POST  /admin/password(.:format)          active_admin/devise/passwords#create 
      new_admin_user_password GET  /admin/password/new(.:format)         active_admin/devise/passwords#new 
      edit_admin_user_password GET  /admin/password/edit(.:format)         active_admin/devise/passwords#edit 
            PATCH  /admin/password(.:format)          active_admin/devise/passwords#update 
            PUT  /admin/password(.:format)          active_admin/devise/passwords#update 
          admin_root GET  /admin(.:format)            admin/dashboard#index 
     batch_action_admin_admin_users POST  /admin/admin_users/batch_action(.:format)      admin/admin_users#batch_action 
        admin_admin_users GET  /admin/admin_users(.:format)         admin/admin_users#index 
            POST  /admin/admin_users(.:format)         admin/admin_users#create 
       new_admin_admin_user GET  /admin/admin_users/new(.:format)        admin/admin_users#new 
       edit_admin_admin_user GET  /admin/admin_users/:id/edit(.:format)       admin/admin_users#edit 
        admin_admin_user GET  /admin/admin_users/:id(.:format)        admin/admin_users#show 
            PATCH  /admin/admin_users/:id(.:format)        admin/admin_users#update 
            PUT  /admin/admin_users/:id(.:format)        admin/admin_users#update 
            DELETE  /admin/admin_users/:id(.:format)        admin/admin_users#destroy 
     batch_action_admin_categories POST  /admin/categories/batch_action(.:format)      admin/categories#batch_action 
        admin_categories GET  /admin/categories(.:format)         admin/categories#index 
            POST  /admin/categories(.:format)         admin/categories#create 
        new_admin_category GET  /admin/categories/new(.:format)        admin/categories#new 
       edit_admin_category GET  /admin/categories/:id/edit(.:format)       admin/categories#edit 
         admin_category GET  /admin/categories/:id(.:format)        admin/categories#show 
            PATCH  /admin/categories/:id(.:format)        admin/categories#update 
            PUT  /admin/categories/:id(.:format)        admin/categories#update 
            DELETE  /admin/categories/:id(.:format)        admin/categories#destroy 
        admin_dashboard GET  /admin/dashboard(.:format)          admin/dashboard#index 
batch_action_admin_category_statuses POST  /admin/categories/:category_id/statuses/batch_action(.:format) admin/statuses#batch_action 
      admin_category_statuses GET  /admin/categories/:category_id/statuses(.:format)    admin/statuses#index 
            POST  /admin/categories/:category_id/statuses(.:format)    admin/statuses#create 
      new_admin_category_status GET  /admin/categories/:category_id/statuses/new(.:format)   admin/statuses#new 
      edit_admin_category_status GET  /admin/categories/:category_id/statuses/:id/edit(.:format)  admin/statuses#edit 
       admin_category_status GET  /admin/categories/:category_id/statuses/:id(.:format)   admin/statuses#show 
            PATCH  /admin/categories/:category_id/statuses/:id(.:format)   admin/statuses#update 
            PUT  /admin/categories/:category_id/statuses/:id(.:format)   admin/statuses#update 
            DELETE  /admin/categories/:category_id/statuses/:id(.:format)   admin/statuses#destroy 
      batch_action_admin_users POST  /admin/users/batch_action(.:format)       admin/users#batch_action 
         admin_users GET  /admin/users(.:format)           admin/users#index 
            POST  /admin/users(.:format)           admin/users#create 
         new_admin_user GET  /admin/users/new(.:format)          admin/users#new 
        edit_admin_user GET  /admin/users/:id/edit(.:format)        admin/users#edit 
          admin_user GET  /admin/users/:id(.:format)          admin/users#show 
            PATCH  /admin/users/:id(.:format)          admin/users#update 
            PUT  /admin/users/:id(.:format)          admin/users#update 
            DELETE  /admin/users/:id(.:format)          admin/users#destroy 
         admin_comments GET  /admin/comments(.:format)          admin/comments#index 
            POST  /admin/comments(.:format)          admin/comments#create 
         admin_comment GET  /admin/comments/:id(.:format)         admin/comments#show 
            DELETE  /admin/comments/:id(.:format)         admin/comments#destroy 
          dashboard GET  /dashboard(.:format)           profiles#dashboard 
         profiles_show GET  /profiles/show(.:format)          profiles#show 
          homepage GET  /home(.:format)            pages#home 
          aboutpage GET  /about(.:format)            pages#about 
         creditspage GET  /credits(.:format)            pages#credits 
        new_user_session GET  /users/sign_in(.:format)          devise/sessions#new 
         user_session POST  /users/sign_in(.:format)          devise/sessions#create 
       destroy_user_session DELETE  /users/sign_out(.:format)          devise/sessions#destroy 
         user_password POST  /users/password(.:format)          devise/passwords#create 
        new_user_password GET  /users/password/new(.:format)         devise/passwords#new 
        edit_user_password GET  /users/password/edit(.:format)         devise/passwords#edit 
            PATCH  /users/password(.:format)          devise/passwords#update 
            PUT  /users/password(.:format)          devise/passwords#update 
      cancel_user_registration GET  /users/cancel(.:format)          users/registrations#cancel 
        user_registration POST  /users(.:format)            users/registrations#create 
       new_user_registration GET  /users/sign_up(.:format)          users/registrations#new 
       edit_user_registration GET  /users/edit(.:format)           users/registrations#edit 
            PATCH  /users(.:format)            users/registrations#update 
            PUT  /users(.:format)            users/registrations#update 
            DELETE  /users(.:format)            users/registrations#destroy 
          register GET  /register(.:format)           devise/registrations#new 
           login GET  /login(.:format)            devise/sessions#new 
           logout GET  /logout(.:format)            devise/sessions#destroy 
          statuses GET  /statuses(.:format)           statuses#index 
            POST  /statuses(.:format)           statuses#create 
          new_status GET  /statuses/new(.:format)          statuses#new 
         edit_status GET  /statuses/:id/edit(.:format)         statuses#edit 
           status GET  /statuses/:id(.:format)          statuses#show 
            PATCH  /statuses/:id(.:format)          statuses#update 
            PUT  /statuses/:id(.:format)          statuses#update 
            DELETE  /statuses/:id(.:format)          statuses#destroy 
           feed GET  /feed(.:format)            statuses#index 
           root GET  /               pages#home 
            GET  /:id(.:format)             profiles#show 

應用程序/管理/狀態。RB

ActiveAdmin.register Status do 

    permit_params :user_id, :content, :private, :category_id 
    belongs_to :user 
    belongs_to :category 
    menu priority: 4 

end 

應用程序/管理/ category.rb

ActiveAdmin.register Category do 

    permit_params :cat_name 
    has_many :statuses 
    menu priority: 5 

end 
+0

請,張貼'耙的擡起頭routes'輸出 – Bmxer

+0

@BMxer謝謝 - 現在加入。 – eponymous

+0

看起來路由解決了'admin_category_statuses GET /admin/categories/:category_id/statuses(.:format)admin/statuses#index'和'category_id'丟失,這對應於錯誤消息。但我不明白爲什麼訪問**:3000 /管理**你進入該路線。 – Bmxer

回答

0

app/views/active_admin/page/index.html.arb(也許你有沒有菜單?)你正在建設一個鏈接到/管理員可能介於/狀態頁( admin_category_statuses_path),但您不提供類別參數。

0

固定!這個問題出現在管理員目錄的rb文件中 - 我錯誤地試圖在此處定義類別和狀態之間的關係,我認爲這是因爲我誤解了參考文獻:http://activeadmin.info/docs/2-resource-customization.html @ruby_newbie你很近,只是錯誤的文件,但它讓我再看看這個:)

ActiveAdmin中的關係仍然不太正確(過濾器顯示對象的引用而不是名稱等),但是它再次加載,我將從此處開始。

非常感謝你對你的所有建議

相關問題