Skip to content

Instantly share code, notes, and snippets.

@ashleygwilliams
Created July 9, 2013 20:30
Show Gist options
  • Save ashleygwilliams/5960998 to your computer and use it in GitHub Desktop.
Save ashleygwilliams/5960998 to your computer and use it in GitHub Desktop.

Make the forms!

You are making a deck-building game. AWESOME. To test the efficacy and balance of the potential decks, your are building a program that simulates potential decks. Your friend and dungeon master has already written the create action for you.

  1. Using this, create the decks form in a partial called decks/_form. This form should allow you to create a deck and add multiple cards to it.
def create
  @deck = Deck.new()
  @deck.name = params[:deck][:name]

  @deck.description = deck[:description]

  params[:cards].each do |card|
    card = Card.new
    card[:name] = card.name
    card[:strength] = card.strength

    @deck.card << card
  end

  if @deck.save
    redirect_to @deck, notice: 'Deck was successfully created.'
  else
    render action: "new"
  end
end

While you worked on that, your amazing dungeon master made the create action for the Card controller. Now create the card's form in cards/_form so that you can create a card an associate it with a specific deck.

def create
  @card = Card.new
  @card.name = params[:card][:name]

  @card.deck = Deck.find(params[:deck_id])

  if @card.save
    redirect_to @card, notice: 'Card was successfully created.'
  else
    render action: "new"
  end
end

def show
  @card = Card.find(params[:id])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment