mGarsteck mGarsteck - 7 months ago 11
Ruby Question

Couldn't find Tray with 'id'=

Im creating a garden app that has trays and plants.
tray has_many plants, plant belongs_to tray etc.

Im getting the above error and Im not sure how to assign the tray_id to the new plant being created.

Here is the add plant button in my tray's show view

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


Here is my 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


Here is my trays controller

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


Here is my form for creating new plants

<%= form_for(@plant) do |f| %>
<%= f.label 'NAME' %>
<%= f.text_field :title, class: 'form-control', id: 'focusedInput1', placeholder: 'ENTER NAME' %>
etc, etc
<% end %>


What am I doing wrong? Thanks for the help :)

Answer

The params[:tray_id] is nil on this line tray = Tray.find(params[:tray_id]) in your Posts controller.

You're also not passing tray_id anywhere in your params. You'll need to properly pass it as a param to your new action:

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

Then add a hidden field to pass :tray_id in your form:

<%= f.hidden_field :tray_id, value: params[:tray_id] %>

Now, you can find tray in your create action using tray = Tray.find(params[:plant][:tray_id]).