在单独的控制器上调用新操作时创建新类别

时间:2016-04-27 18:12:19

标签: ruby-on-rails ruby ruby-on-rails-4

我创造了4个模型:成长,托盘,植物,类别。我的目的是为每个模型分配一个类别,以便我可以通过类别进行组织。当我创建新的增长时,会创建一个新类别。然后,当我创建一个新托盘时,我可以将托盘分配给新创建的增长类别。

Grow belongs_to类别 托盘属于类别 plant belongs_to category

class GrowsController < ApplicationController
before_action :set_grow, only: [:show, :edit, :update, :destroy]


def index
  @grows = Grow.all
end


def show
end


def new
  @grow = Grow.new
end


def edit
end


def create
  @grow = Grow.new(grow_params.merge(category: Category.create(cat_params)))

  respond_to do |format|
    if @grow.save
      format.html { redirect_to @grow, notice: 'Grow was successfully created.' }
      format.json { render :show, status: :created, location: @grow }
    else
      format.html { render :new }
      format.json { render json: @grow.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /grows/1
# PATCH/PUT /grows/1.json
def update
  respond_to do |format|
    if @grow.update(grow_params)
      format.html { redirect_to @grow, notice: 'Grow was successfully updated.' }
      format.json { render :show, status: :ok, location: @grow }
    else
      format.html { render :edit }
      format.json { render json: @grow.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /grows/1
# DELETE /grows/1.json
def destroy
  @grow.destroy
  respond_to do |format|
    format.html { redirect_to grows_url, notice: 'Grow was successfully destroyed.' }
    format.json { head :no_content }
   end
  end

private
  # Use callbacks to share common setup or constraints between actions.

  def set_grow
    @grow = Grow.find(params[:id])
  end
  def cat_params
    params.require(:category).permit(:name)
  end
    def grow_params
      params.require(:grow).permit(:title, :notes, :category_id)
    end
  end

等等,它是视图和控制器的标准设置。我不知道如何实现这一目标。请帮助引导我朝正确的方向前进:)

1 个答案:

答案 0 :(得分:0)

if let str = String(data:finalData, encoding: NSUTF8StringEncoding) {
    response = str
}