我的应用程序中有几个嵌套路由。我正在建立一个数据库,根据他们所在的行业以及该行业内的各种竞争对手来分类创业公司。
我一直在寻找这个错误的答案,但我很难搞清楚:
修改已添加完整的错误消息和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]
嵌套路线:
资源:行业,只有:[: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,或者我的类别未与其行业正确关联。
有什么建议吗?
答案 0 :(得分:1)
show.html.erb
<强>更新强>
<%= link_to "Edit", edit_industry_category_path(@category.industry, @category) %>