当我尝试显示、编辑、删除或添加区时遇到以下错误:
undefined method `state_path' for #<#<Class:0x007f93a9e9df88>:0x007f93af11f8d8>
我期待它生成一个 country_state_path 链接,因为我有link_to 'Back', [@country, @state]
但由于某种原因它只给了我 state_path。如果我使用点符号而不是 @country.state 我会收到一个 nilClass 错误。
我正在为我的模型使用树结构:
Country
State
District
如果我输入 country_state_path 一切正常。但我宁愿使用模型符号输入它,因为它适用于我的状态模型。
(我使用了正确的术语吗?如果不是,请更正,我还是 Rails 的新手)
代码
地区模式
class District < ActiveRecord::Base
validates_uniqueness_of :name, scope: :state_id
before_destroy :check_for_schools
belongs_to :state
#has_many :schools, :order => 'name'
private
def check_for_schools
=begin
if schools.count > 0
self.errors[:base] << "Cannot delete district while schools exist."
return false
end
=end
end
end
区控制器
class DistrictsController < ApplicationController
# Allows JSON Queries
skip_before_filter :verify_authenticity_token
before_action :set_district, only: [:show, :edit, :update, :destroy]
before_filter :load_state
# GET /districts
# GET /districts.json
def index
@districts = @state.districts.all(:order => 'name ASC')
end
# GET /districts/1
# GET /districts/1.json
def show
end
# GET /districts/new
def new
@district = @state.districts.new
end
# GET /districts/1/edit
def edit
@state.districts.find(params[:id])
end
# POST /districts
# POST /districts.json
def create
@district = @state.districts.new(district_params)
respond_to do |format|
if @district.save
format.html { redirect_to [@country,@state,@district], notice: 'District was successfully created.' }
format.json { render action: 'show', status: :created, location: @district }
else
format.html { render action: 'new' }
format.json { render json: @district.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /districts/1
# PATCH/PUT /districts/1.json
def update
respond_to do |format|
if @district.update(district_params)
format.html { redirect_to [@state, @district], notice: 'District was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @district.errors, status: :unprocessable_entity }
end
end
end
# DELETE /districts/1
# DELETE /districts/1.json
def destroy
@district = @state.district.find(params[:id])
respond_to do |format|
if @district.destroy
format.html { redirect_to @state }
format.json { head :no_content }
else
format.html { redirect_to( @state, :notice => 'Unable to delete a state that has districts.') }
format.json { render json: @district.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_district
@district = District.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def district_params
params.require(:district).permit(:name, :state_id)
end
def load_state
@state = State.find(params[:state_id])
end
end
区“显示”视图
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @district.name %>
</p>
<p>
<strong>State:</strong>
<%= @district.state_id %>
</p>
<%= link_to 'Edit', edit_country_state_district_path(@country, @state, @district) %> |
<%= link_to 'Back', [@country, @state] %>
不工作的线路是"link_to 'Back', [@country, @state]
.