'id' =

2016-05-01 6 views
2

トレイを見つけられませんでしたトレイと植物を持つガーデンアプリを作成しています。 トレイhas_many、植物belongs_toトレイなど'id' =

上記のエラーが表示され、作成する新しいプラントに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 %> 

私は間違っていますか?助けてくれてありがとう

答えて

2

params[:tray_id]は、この行のnilです。あなたの投稿コントローラにtray = Tray.find(params[:tray_id])です。

また、tray_idをあなたのパラメータのどこにも渡していません。あなたは適切にあなたの新しいアクションへのparamとしてそれを渡す必要があります:

<%= 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])を使用してアクションを作成します。

+0

これはまだ私に同じエラーを与えています – mGarsteck

+0

素晴らしい、今は素晴らしい仕事です。答えをありがとう。あなたのためにカルマの多くの何百万人が:) – mGarsteck

+0

幸運の最高を聞いてうれしい! –

関連する問題