Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- == CheatCodesController.rb ==
- class CheatCodesController < ApplicationController
- ...
- # GET /cheat_codes/new
- # GET /cheat_codes/new.xml
- def new
- @cheat_code = CheatCode.new
- @cheat_code.game = Game.new
- respond_to do |format|
- format.html # new.html.erb
- format.xml { render :xml => @cheat_code }
- end
- end
- # POST /cheat_codes
- # POST /cheat_codes.xml
- def create
- @cheat_code = CheatCode.new(params[:cheat_code])
- @cheat_code.game = Game.new(params[:cheat_code][:game_attributes])
- respond_to do |format|
- if @cheat_code.game.save && @cheat_code.save
- flash[:notice] = "CheatCode with game id #{@cheat_code.game.id} was successfully created."
- format.html { redirect_to(@cheat_code) }
- format.xml { render :xml => @cheat_code, :status => :created, :location => @cheat_code }
- else
- format.html { render :action => "new" }
- format.xml { render :xml => @cheat_code.errors, :status => :unprocessable_entity }
- end
- end
- ...
- end
- == CheatCodes controller new.html.erb ===
- <h1>New Cheat Code</h1>
- <% @cheat_code.game.build %>
- <% form_for(@cheat_code) do |form_cheat_code| %>
- <%= form_cheat_code.error_messages %>
- <% form_cheat_code.fields_for :game do |games_form| %>
- <p>
- <%= games_form.label :name, "Game" %><br />
- <%= games_form.text_field :name %>
- </p>
- <% end %>
- <p>
- <%= form_cheat_code.label :title %><br />
- <%= form_cheat_code.text_field :title %>
- </p>
- <p>
- <%= form_cheat_code.label :content %><br />
- <%= form_cheat_code.text_area :content, :rows => 6 %>
- </p>
- <p>
- <%= form_cheat_code.label :submitter %><br />
- <%= form_cheat_code.text_field :submitter %>
- </p>
- <p>
- <%= form_cheat_code.submit 'Create' %>
- </p>
- <% end %>
- <%= link_to 'Back', cheat_codes_path %>
- == Game.rb Model ==
- class Game < ActiveRecord::Base
- validates_presence_of :name
- belongs_to :cheat_code
- end
- == CheatCode.rb Model ==
- class CheatCode < ActiveRecord::Base
- validates_presence_of :title, :content, :submitter
- has_one :game
- has_many :systems
- accepts_nested_attributes_for :game, :allow_destroy => :true ,
- :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
- end
- === Migration ===
- class CreateGames < ActiveRecord::Migration
- def self.up
- create_table :games do |t|
- t.string :name
- t.string :wikipedia
- t.timestamps
- end
- end
- def self.down
- drop_table :games
- end
- end
- class CreateCheatCodes < ActiveRecord::Migration
- def self.up
- create_table :cheat_codes do |t|
- t.string :title
- t.text :content
- t.string :submitter
- t.integer :game_id
- t.integer :system_id
- t.timestamps
- end
- end
- def self.down
- drop_table :cheat_codes
- end
- end
- == Results ===
- After creating a new CheatCode, game_id is nil should it should have been 12
- #<CheatCode id: 13, title: "blah", content: "blah", submitter: "test submitter", game_id: nil, system_id: nil, created_at: "2009-05-28 23:18:05", updated_at: "2009-05-28 23:18:05">]
- #<Game id: 12, name: "newgame", wikipedia: nil, created_at: "2009-05-28 23:18:05", updated_at: "2009-05-28 23:18:05">]
Add Comment
Please, Sign In to add comment