2016-05-01 57 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])在您的帖子控制器。

你還沒有在你的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 = Tray.find(params[:plant][:tray_id])創建操作。

+0

這仍然給了我同樣的錯誤 – mGarsteck

+0

真棒,現在它工作的偉大。感謝你的回答。爲你的數百萬噶爾先生:) – mGarsteck

+0

很高興聽到,祝你好運! –