Guest User

Untitled

a guest
Jun 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. == CheatCodesController.rb ==
  2.  
  3. class CheatCodesController < ApplicationController
  4. ...
  5.  
  6. # GET /cheat_codes/new
  7. # GET /cheat_codes/new.xml
  8. def new
  9. @cheat_code = CheatCode.new
  10. @cheat_code.game = Game.new
  11.  
  12. respond_to do |format|
  13. format.html # new.html.erb
  14. format.xml { render :xml => @cheat_code }
  15. end
  16. end
  17.  
  18. # POST /cheat_codes
  19. # POST /cheat_codes.xml
  20. def create
  21. @cheat_code = CheatCode.new(params[:cheat_code])
  22. @cheat_code.game = Game.new(params[:cheat_code][:game_attributes])
  23.  
  24. respond_to do |format|
  25. if @cheat_code.game.save && @cheat_code.save
  26. flash[:notice] = "CheatCode with game id #{@cheat_code.game.id} was successfully created."
  27. format.html { redirect_to(@cheat_code) }
  28. format.xml { render :xml => @cheat_code, :status => :created, :location => @cheat_code }
  29. else
  30. format.html { render :action => "new" }
  31. format.xml { render :xml => @cheat_code.errors, :status => :unprocessable_entity }
  32. end
  33. end
  34. ...
  35. end
  36.  
  37.  
  38. == CheatCodes controller new.html.erb ===
  39.  
  40. <h1>New Cheat Code</h1>
  41.  
  42. <% @cheat_code.game.build %>
  43.  
  44. <% form_for(@cheat_code) do |form_cheat_code| %>
  45. <%= form_cheat_code.error_messages %>
  46.  
  47. <% form_cheat_code.fields_for :game do |games_form| %>
  48. <p>
  49. <%= games_form.label :name, "Game" %><br />
  50. <%= games_form.text_field :name %>
  51. </p>
  52. <% end %>
  53.  
  54. <p>
  55. <%= form_cheat_code.label :title %><br />
  56. <%= form_cheat_code.text_field :title %>
  57. </p>
  58.  
  59. <p>
  60. <%= form_cheat_code.label :content %><br />
  61. <%= form_cheat_code.text_area :content, :rows => 6 %>
  62. </p>
  63.  
  64. <p>
  65. <%= form_cheat_code.label :submitter %><br />
  66. <%= form_cheat_code.text_field :submitter %>
  67. </p>
  68.  
  69. <p>
  70. <%= form_cheat_code.submit 'Create' %>
  71. </p>
  72. <% end %>
  73.  
  74. <%= link_to 'Back', cheat_codes_path %>
  75.  
  76.  
  77. == Game.rb Model ==
  78.  
  79. class Game < ActiveRecord::Base
  80. validates_presence_of :name
  81. belongs_to :cheat_code
  82. end
  83.  
  84.  
  85. == CheatCode.rb Model ==
  86.  
  87. class CheatCode < ActiveRecord::Base
  88. validates_presence_of :title, :content, :submitter
  89. has_one :game
  90. has_many :systems
  91.  
  92. accepts_nested_attributes_for :game, :allow_destroy => :true ,
  93. :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
  94.  
  95. end
  96.  
  97. === Migration ===
  98.  
  99. class CreateGames < ActiveRecord::Migration
  100. def self.up
  101. create_table :games do |t|
  102. t.string :name
  103. t.string :wikipedia
  104.  
  105. t.timestamps
  106. end
  107. end
  108.  
  109. def self.down
  110. drop_table :games
  111. end
  112. end
  113.  
  114.  
  115. class CreateCheatCodes < ActiveRecord::Migration
  116. def self.up
  117. create_table :cheat_codes do |t|
  118. t.string :title
  119. t.text :content
  120. t.string :submitter
  121. t.integer :game_id
  122. t.integer :system_id
  123.  
  124. t.timestamps
  125. end
  126. end
  127.  
  128. def self.down
  129. drop_table :cheat_codes
  130. end
  131. end
  132.  
  133. == Results ===
  134.  
  135. After creating a new CheatCode, game_id is nil should it should have been 12
  136.  
  137. #<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">]
  138.  
  139. #<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