使用has_one关系创建新的嵌套资源 - Rails 4

时间:2016-12-28 13:48:59

标签: ruby-on-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 个答案:

答案 0 :(得分:1)

如错误所示,错误位于第26行的CardsController

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

@event.card等于nil,因为尚未创建该卡。 为了实现这一点,请执行以下操作:

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