I want to have a view where a user can create a neighborhood. But, I want these neighborhoods to be approved by an admin before they are saved in the neighborhood table.
I would like to have a temp_neighborhood table, where the records will be saved until an admin approves them, then the data is moved to the neighborhoods table. This temp table will have the same attributes as the regular table.
So this is the flow I visualize:
- A user visits the neighborhoods/index page
- The user clicks a button to create their neighborhood
- They fill in the information, and the neighborhood controller takes that data, and saves it in the temp_neighborhood table.
- Later an admin will visit a page to approve/deny user created neighborhoods.
- Upon approval, a temporary neighborhood will be moved to the permanent neighborhoo table.
Neighborhood controller:
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
Neighborhood model:
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
The above code just saves a user created neighborhood in the permanent table. To save the data in a temp table, I made the table and a controller named TempNeighborhood. The TempNeighborhood controller only had a new and create method, which looked just like the neighborhood controller.
Back in the neighborhood controller, in the create method I changed the following line:
@neighborhood = Neighborhood.new(neighborhood_params)
to this:
@neighborhood = TempNeighborhood.new(neighborhood_params)
However this gave me the following error:
uninitialized constant NeighborhoodsController::TempNeighborhood
What is the best way to go about saving data from one controller into another table?