没有路由匹配缺少必需的键:[object_id]

时间:2015-11-01 06:46:27

标签: ruby-on-rails associations

我的应用程序中有几个嵌套路由。我正在建立一个数据库,根据他们所在的行业以及该行业内的各种竞争对手来分类创业公司。

我一直在寻找这个错误的答案,但我很难搞清楚:

修改已添加完整的错误消息和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

嵌套路线:      资源:行业,只有:[:show,:create,:edit,:update]做         资源:类别           资源:初创公司,模块::类别
        结束       端

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

C

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,或者我的类别未与其行业正确关联。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

show.html.erb

<强>更新

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