我正在创建一个带有托盘和植物的花园应用程序。 托盘has_many植物,植物属于托盘等。
我收到上述错误,我不知道如何将tray_id分配给正在创建的新工厂。
以下是托盘展示视图中的添加工厂按钮
<%= link_to 'ADD PLANT', new_plant_path(@tray.id), class: "btn btn-raised btn-success hoverable" %>
这是我的plants_controller:
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 %>
我做错了什么?谢谢你的帮助:)
答案 0 :(得分:2)
帖子控制器中此行params[:tray_id]
上的nil
为tray = Tray.find(params[:tray_id])
。
你也没有将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 = Tray.find(params[:plant][:tray_id])
在创建操作中找到托盘。