【问题标题】:No route matches missing required keys: [object_id]没有路由匹配缺少必需的键:[object_id]
【发布时间】:2015-11-01 06:46:26
【问题描述】:

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

我一直在寻找此错误的答案,但无法弄清楚:

编辑添加了完整的错误信息和 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]

嵌套路由: 资源 :industries , 仅: [:show, :create, :edit, :update] 做 资源:类别做 资源 :startups, 模块: :categories
结尾 结束

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

由于某种原因,我在点击 view 按钮时没有调用 Industry_id,或者我的类别与他们的行业没有正确关联。

有什么建议吗?

【问题讨论】:

  • 请使用categories_controller.rb 更新您的问题另外请发布完整的错误消息,而不是附上屏幕截图。很难阅读。

标签: ruby-on-rails associations


【解决方案1】:

show.html.erb

更新

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

【讨论】:

  • 那行得通,现在我遇到了一个新错误,但我想我可以解决这个问题,我会标记为正确的。谢谢!附言新错误来自可启动路线...未定义方法`category_startups_path 我猜我需要重新配置它。
猜你喜欢
  • 2014-03-09
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多