2015-11-01 76 views
-1

我在我的應用程序中有幾個嵌套的路線。我正在建立一個數據庫,根據他們所處的行業以及該行業內的各種競爭對手對初創公司進行分類。沒有路線匹配缺少必需的鍵:[object_id]

我一直在尋找這個問題的答案錯誤,但我有麻煩想出來的:

編輯添加完整的錯誤信息和categories_controller.rb

錯誤消息:

ActionController::UrlGenerationError in Categories#show 

No route matches {:action=>"edit", :controller=>"categories", :id=>"5", :industry_id=>nil} missing required keys: [:industry_id] 

error I'm getting when hitting edit in the index view

嵌套路線: 個資源:行業中,只有:[:秀:創建,編輯,:更新]做 資源:類別做 資源:啓動,模塊:類別
端 端

Category.rb 

class Category < ActiveRecord::Base 
    has_many :startups, as: :startupable 
    belongs_to :industry 
    belongs_to :user 
end 


Industry.rb 

class Industry < ActiveRecord::Base 
    belongs_to :user 
    has_many :categories 
end 

startup.rb 

class Startup < ActiveRecord::Base 
    belongs_to :startupable, polymorphic: true 
    belongs_to :user 
end 

Ç

ategories_controller.rb 

class CategoriesController < ApplicationController 
    before_action :set_category, only: [:show, :edit, :update, :destroy] 
    respond_to :html 

    def index 
    @categories = Category.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 3) 
    authorize @categories 
    end 

    def show 
    end 


    def new 
    @industry = Industry.find(params[:industry_id]) 
    @category = @industry.categories.new 
    flash[:notice] = "Category created." 
    authorize @category 
    end 


    def edit 
    end 


    def create 
    @industry = Industry.find(params[:industry_id]) 
    @category = current_user.categories.build(category_params) 
    respond_with @industry 
    authorize @category 
    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 
    redirect_to @industry 
    flash[:notice] = "You have succesfully deleted the category." 
    end 

    private 
    def set_category 
     @category = Category.find(params[:id]) 
     authorize @category 
    end 
    def correct_user 
     @category = current_user.categories.find_by(id: params[:id]) 
     redirect_to categories_path, notice: "Not authorized to edit this Category" if @category.nil? 
    end 

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

出於某種原因,擊中視圖按鈕或我的類別不與他們的行業關聯適當的時候我不調用Industry_id。

有什麼建議嗎?

+0

請更新與'categories_controller.rb'您的問題還請後完整的錯誤消息,而不是附加的屏幕截圖。這是非常難以閱讀。 – Pavan

回答

1

show.html.erb

UPDATE

<%= link_to "Edit", edit_industry_category_path(@category.industry, @category) %> 
+0

這工作,現在我得到一個新的錯誤,但我認爲我可以解決這個問題,我會標記這是正確的。謝謝!附:新的錯誤是從startupable路由...未定義的方法'category_startups_path我猜我需要重新配置這一些如何。 – user3787971

相關問題