2015-11-01 39 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

相关问题