我正在创建一个花园应用程序,有托盘和植物。
托盘有许多植物,植物属于托盘等。
我得到上述错误,我不确定如何分配托盘的ID到新工厂正在创建。
这是我托盘显示视图中的“添加植物”按钮

<%= link_to 'ADD PLANT', new_plant_path(@tray.id), class: "btn btn-raised btn-success hoverable" %>

这是我的工厂控制器:
class PlantsController < ApplicationController
before_action :set_plant, only: [:show, :edit, :update, :destroy]


# GET /plants
# GET /plants.json
def index
  @plants = Plant.all
end


def show
end

def new
  @plant = Plant.new
end

def edit
end

def create
  tray = Tray.find(params[:tray_id])
  @plant = tray.plants.create(plant_params)


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


def update
  respond_to do |format|
    if @plant.update(plant_params)
      format.html { redirect_to @plant, notice: 'Plant was successfully updated.' }
      format.json { render :show, status: :ok, location: @plant }
    else
      format.html { render :edit }
      format.json { render json: @plant.errors, status: :unprocessable_entity }
    end
  end
end
def destroy
  @plant.destroy
  respond_to do |format|
    format.html { redirect_to plants_url, notice: 'Plant was successfully destroyed.' }
    format.json { head :no_content }
  end
end

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


def set_plant
  @plant = Plant.find(params[:id])
end

def plant_params
  params.require(:plant).permit(:title, :notes, :category_id, :tray_id, images_files: [])
  end
end

这是我的托盘控制器
class PlantsController < ApplicationController
before_action :set_plant, only: [:show, :edit, :update, :destroy]


def index
  @plants = Plant.all
end

def show
end

def new
  @plant = Plant.new
end

def edit
end

def create
  tray = Tray.find(params[:tray_id])
  @plant = tray.plants.create(plant_params)

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

def update
  respond_to do |format|
    if @plant.update(plant_params)
      format.html { redirect_to @plant, notice: 'Plant was successfully updated.' }
      format.json { render :show, status: :ok, location: @plant }
    else
      format.html { render :edit }
      format.json { render json: @plant.errors, status: :unprocessable_entity }
    end
  end
end

def destroy
  @plant.destroy
  respond_to do |format|
    format.html { redirect_to plants_url, notice: 'Plant was successfully destroyed.' }
    format.json { head :no_content }
  end
end

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


  def set_plant
    @plant = Plant.find(params[:id])
  end


  def plant_params
    params.require(:plant).permit(:title, :notes, :category_id, :tray_id, images_files: [])
  end
end

这是我创造新植物的表格
<%= form_for(@plant) do |f| %>
  <%= f.label 'NAME' %>
      <%= f.text_field :title, class: 'form-control', id: 'focusedInput1', placeholder: 'ENTER NAME' %>
 etc, etc
<% end %>

我做错什么了?谢谢你的帮助:)

最佳答案

在posts控制器中,此行的params[:tray_id]nil
你也不会在你的参数中传递tray = Tray.find(params[:tray_id])。您需要将其作为参数正确传递给新操作:

<%= link_to 'ADD PLANT', new_plant_path(tray_id: @tray.id), class: "btn btn-raised btn-success hoverable" %>

然后添加一个隐藏字段以在表单中传递tray_id
<%= f.hidden_field :tray_id, value: params[:tray_id] %>

现在,您可以使用:tray_id在创建操作中找到托盘。

关于ruby-on-rails - 找不到带有'id'=的纸盘,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36973267/

10-10 11:55