2016-12-28 95 views
0

我不確定我出錯的地方。您的幫助將不勝感激。 我想爲每個事件創建一張卡片。用has_one關係創建新的嵌套資源 - Rails 4

這是目前的錯誤,我得到 - 我不確定如何解決它:

undefined method `create' for nil:NilClass 

enter image description here

我的路線文件

Rails.application.routes.draw do 

    resources :events do 
    resources :cards 
    member do 
     get 'attendants' 
    end 
    end 

end 

模式

ActiveRecord::Schema.define(version: 20161228113809) do 

    create_table "cards", force: :cascade do |t| 
    t.string "title" 
    t.integer "event_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    create_table "events", force: :cascade do |t| 
    t.string "title" 
    t.text  "description" 
    t.string "address" 
    t.string "slug" 
    end 

    add_index "events", ["slug"], name: "index_events_on_slug", unique: true 
end 

個模型[event.rb & card.rb]

class Event < ActiveRecord::Base 
    has_one :card 
end 

class Card < ActiveRecord::Base 
    belongs_to :event 
end 

cards_controller.rb

class CardsController < ApplicationController 
    before_action :set_card, only: [:show, :edit, :update, :destroy] 

    def index 
    @cards = Card.all 
    end 

    def show 
    end 

    def new 
    @event = Event.friendly.find(params[:event_id]) 
    @card = @event.cards.build 
    end 

    def edit 
    @event = Event.friendly.find(params[:event_id]) 
    end 

    def create 
    @event = Event.friendly.find(params[:event_id]) 
    @card = @event.card.create(card_params) 

    respond_to do |format| 
     if @card.save 
     format.html { redirect_to([@card.event, @card], notice: 'Card was successfully created.') } 
     format.json { render :show, status: :created, location: @card } 
     else 
     format.html { render :new } 
     format.json { render json: @card.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def update 
    respond_to do |format| 
     if @card.update(card_params) 
     format.html { redirect_to([@card.event, @card], notice: 'Card was successfully updated.') } 
     format.json { render :show, status: :ok, location: @card } 
     else 
     format.html { render :edit } 
     format.json { render json: @card.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @event = Event.friendly.find(params[:event_id]) 
    @card = @event.cards.find(params[:id]) 
    @card.destroy 
    respond_to do |format| 
     format.html { redirect_to cards_url, notice: 'Card was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    def set_card 
     @card = Card.find(params[:id]) 
    end 

    def card_params 
     params.require(:card).permit(:title, :event_id) 
    end 
end 

的意見/卡/ _form.html.erb

<%= simple_form_for [@event, @card] do |f| %> 
    <%= f.error_notification %> 

    <div class="form-inputs"> 
    <%= f.input_field :title, value: @event.title %> 
    <%= f.input_field :event_id, value: @event.id %> 
    </div> 

    <div class="form-actions"> 
    <%= f.button :submit %> 
    </div> 
<% end %> 

回答

1

正如錯誤所說,錯誤位於您的CardsController在26行:

@card = @event.card.create(card_params) 

@event.card等於零,因爲卡尚未創建。 爲了這個工作,做:

def create 
    @event = Event.friendly.find(params[:event_id]) 
    @card = @event.build_card(card_params) 

    respond_to do |format| 
    if @card.save 
     format.html { redirect_to([@card.event, @card], notice: 'Card was successfully created.') } 
     format.json { render :show, status: :created, location: @card } 
    else 
     format.html { render :new } 
     format.json { render json: @card.errors, status: :unprocessable_entity } 
    end 
    end 
end 
+0

謝謝你soo! – ARTLoe