我希望有一个用户可以创建社区的视图。但是,我希望这些社区在被保存在邻居表之前得到管理员的批准。
我想有一个temp_neighborhood表,其中记录将被保存,直到管理员批准它们,然后数据被移动到邻域表。此临时表将具有与常规表相同的属性。
所以这是我想象的流程:
邻居控制器:
class NeighborhoodsController < ApplicationController
before_action :set_neighborhood, only: [:show, :edit, :update, :destroy]
#index, show, edit, update, and delete methods removed for brevity
def new
@neighborhood = Neighborhood.new
end
def create
@neighborhood = Neighborhood.new(neighborhood_params)
respond_to do |format|
if @neighborhood.save
format.html { redirect_to @neighborhood, notice: 'Neighborhood was successfully created.' }
format.json { render :show, status: :created, location: @neighborhood }
else
format.html { render :new }
format.json { render json: @neighborhood.errors, status: :unprocessable_entity }
end
end
end
private
def set_neighborhood
@neighborhood = Neighborhood.find_by_slug(params[:id])
end
def neighborhood_params
params.require(:neighborhood).permit(:name, :address)
end
end
邻里模型:
class Neighborhood < ActiveRecord::Base
geocoded_by :address
after_validation :geocode
has_many :users
validates :name, presence: true, uniqueness: true
validates :address, presence: true
after_validation :create_slug
def to_param
slug
end
private
def create_slug
self.slug = name.parameterize
end
end
上面的代码只是将用户创建的邻域保存在永久表中。为了将数据保存在临时表中,我制作了一个名为TempNeighborhood的表和一个控制器。 TempNeighborhood控制器只有一个新的create方法,看起来就像邻居控制器。
回到邻居控制器,在create方法中我更改了以下行:
@neighborhood = Neighborhood.new(neighborhood_params)
到此:
@neighborhood = TempNeighborhood.new(neighborhood_params)
然而,这给了我以下错误:
uninitialized constant NeighborhoodsController::TempNeighborhood
将数据从一个控制器保存到另一个表中的最佳方法是什么?
答案 0 :(得分:1)
您确定将TempNeighborhood控制器设置为复数吗? TempNeighborhoods控制器和模型是单一的&#34; TempNeighborhood&#34;。