【发布时间】: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