简体   繁体   English

从Rails 4中的其他控制器/视图将数据保存在表中

[英]Save data in table from different controller/view in Rails 4

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. 我想要一个temp_neighborhood表,在该表中将保存记录,直到管理员批准它们为止,然后将数据移至近邻表。 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. 他们填写信息,然后邻居控制器获取该数据,并将其保存在temp_neighborhood表中。
  • 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. 为了将数据保存在临时表中,我制作了该表和一个名为TempNeighborhood的控制器。 The TempNeighborhood controller only had a new and create method, which looked just like the neighborhood controller. TempNeighborhood控制器仅具有一个新的create方法,该方法看起来像邻居控制器。

Back in the neighborhood controller, in the create method I changed the following line: 回到邻域控制器中,在create方法中,我更改了以下行:

@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? 将数据从一个控制器保存到另一个表的最佳方法是什么?

Are you sure that you have the TempNeighborhood Controller set up as plural? 您确定已将TempNeighborhood Controller设置为复数吗? TempNeighborhoods Controller and the Model is singular "TempNeighborhood". TempNeighbourhood控制器和模型是单数形式的“ TempNeighborhood”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM