Rails 4 - 销毁不工作
Rails 4 - Destroy not working
我正在尝试在 Rails 4.
中制作一个应用程序
我有一个行业模型。它与我的个人资料模型和组织模型中的每一个都有并且属于许多关联。
协会是:
industry.rb
has_and_belongs_to_many :organisations
has_and_belongs_to_many :profiles
organisation.rb
has_and_belongs_to_many :industries
profile.rb
has_and_belongs_to_many :industries
has_and_belongs_to_many :projects
我的架构有:
create_table "industries", force: :cascade do |t|
t.string "sector"
t.string "icon"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "industries_profiles", id: false, force: :cascade do |t|
t.integer "industry_id"
t.integer "profile_id"
end
add_index "industries_profiles", ["industry_id"], name: "index_industries_profiles_on_industry_id", using: :btree
add_index "industries_profiles", ["profile_id"], name: "index_industries_profiles_on_profile_id", using: :btree
我有一个行业索引,它是我在我的开发环境中创建的。我现在正在尝试删除一个。
当我在索引中单击删除 link 时,出现此错误:
ActiveRecord::StatementInvalid in IndustriesController#destroy
PG::UndefinedTable: ERROR: relation "industries_organisations" does
not exist LINE 5: WHERE a.attrelid = '"industries_organisations... ^ :
SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid),
a.attnotnull, a.atttypid, a.atttypmod FROM pg_attribute a LEFT JOIN pg_attrdef
d ON a.attrelid = d.adrelid AND a.attnum = d.adnum WHERE a.attrelid =
'"industries_organisations"'::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum
如何删除记录?
行业控制器有:
class IndustriesController < ApplicationController
before_action :set_industry, only: [:show, :edit, :update, :destroy]
# GET /industries
# GET /industries.json
def index
#@industries = Industry.all
@industries = Industry.alphabetically
end
# GET /industries/1
# GET /industries/1.json
def show
end
# GET /industries/new
def new
@industry = Industry.new
end
# GET /industries/1/edit
def edit
end
# POST /industries
# POST /industries.json
def create
@industry = Industry.new(industry_params)
respond_to do |format|
if @industry.save
format.html { redirect_to @industry, notice: 'Industry was successfully created.' }
format.json { render :show, status: :created, location: @industry }
else
format.html { render :new }
format.json { render json: @industry.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /industries/1
# PATCH/PUT /industries/1.json
def update
respond_to do |format|
if @industry.update(industry_params)
format.html { redirect_to @industry, notice: 'Industry was successfully updated.' }
format.json { render :show, status: :ok, location: @industry }
else
format.html { render :edit }
format.json { render json: @industry.errors, status: :unprocessable_entity }
end
end
end
# DELETE /industries/1
# DELETE /industries/1.json
def destroy
@industry.destroy
respond_to do |format|
format.html { redirect_to industries_url, notice: 'Industry was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_industry
@industry = Industry.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def industry_params
params.require(:industry).permit(:id, :sector, :icon)
end
end
如果您要建立 many to many
关系,则需要 table industries_organisations
关系。有关详细信息,请阅读此处的文档:
我正在尝试在 Rails 4.
中制作一个应用程序我有一个行业模型。它与我的个人资料模型和组织模型中的每一个都有并且属于许多关联。
协会是:
industry.rb
has_and_belongs_to_many :organisations
has_and_belongs_to_many :profiles
organisation.rb
has_and_belongs_to_many :industries
profile.rb
has_and_belongs_to_many :industries
has_and_belongs_to_many :projects
我的架构有:
create_table "industries", force: :cascade do |t|
t.string "sector"
t.string "icon"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "industries_profiles", id: false, force: :cascade do |t|
t.integer "industry_id"
t.integer "profile_id"
end
add_index "industries_profiles", ["industry_id"], name: "index_industries_profiles_on_industry_id", using: :btree
add_index "industries_profiles", ["profile_id"], name: "index_industries_profiles_on_profile_id", using: :btree
我有一个行业索引,它是我在我的开发环境中创建的。我现在正在尝试删除一个。
当我在索引中单击删除 link 时,出现此错误:
ActiveRecord::StatementInvalid in IndustriesController#destroy
PG::UndefinedTable: ERROR: relation "industries_organisations" does
not exist LINE 5: WHERE a.attrelid = '"industries_organisations... ^ :
SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid),
a.attnotnull, a.atttypid, a.atttypmod FROM pg_attribute a LEFT JOIN pg_attrdef
d ON a.attrelid = d.adrelid AND a.attnum = d.adnum WHERE a.attrelid =
'"industries_organisations"'::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum
如何删除记录?
行业控制器有:
class IndustriesController < ApplicationController
before_action :set_industry, only: [:show, :edit, :update, :destroy]
# GET /industries
# GET /industries.json
def index
#@industries = Industry.all
@industries = Industry.alphabetically
end
# GET /industries/1
# GET /industries/1.json
def show
end
# GET /industries/new
def new
@industry = Industry.new
end
# GET /industries/1/edit
def edit
end
# POST /industries
# POST /industries.json
def create
@industry = Industry.new(industry_params)
respond_to do |format|
if @industry.save
format.html { redirect_to @industry, notice: 'Industry was successfully created.' }
format.json { render :show, status: :created, location: @industry }
else
format.html { render :new }
format.json { render json: @industry.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /industries/1
# PATCH/PUT /industries/1.json
def update
respond_to do |format|
if @industry.update(industry_params)
format.html { redirect_to @industry, notice: 'Industry was successfully updated.' }
format.json { render :show, status: :ok, location: @industry }
else
format.html { render :edit }
format.json { render json: @industry.errors, status: :unprocessable_entity }
end
end
end
# DELETE /industries/1
# DELETE /industries/1.json
def destroy
@industry.destroy
respond_to do |format|
format.html { redirect_to industries_url, notice: 'Industry was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_industry
@industry = Industry.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def industry_params
params.require(:industry).permit(:id, :sector, :icon)
end
end
如果您要建立 many to many
关系,则需要 table industries_organisations
关系。有关详细信息,请阅读此处的文档: