我已經建立2個顯示數據 - 軌道
平臺:
def change
create_table :platforms do |t|
t.string :name
t.timestamps null: false
end
end
遊戲:
def change
create_table :games do |t|
t.string :title
t.text :description
t.string :image_url
t.decimal :price
t.integer :platform_id
t.timestamps null: false
end
add_index :games, :platform_id
end
,然後這是我_form遊戲中的.html.erb
<%= form_for(@game) do |f| %>
<% if @game.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@game.errors.count, "error") %> prohibited this game from being saved:</h2>
<ul>
<% @game.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :image_url %><br>
<%= f.text_field :image_url %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :platform %><br>
<div>
<%= collection_select(:invoice, :platform_id, Platform.all, :id, :name, {}, {:multiple => false}) %>
</div>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
的情況下,
我創造新的遊戲,我選擇的PlayStation從選擇輸入,當我點擊提交,這將是秀場從遊戲表像標題,描述,價格,圖片網址我想顯示的名稱我所的PlayStation像以前一樣所選擇的平臺,所以我說
<p>
<strong>Platform:</strong>
<%= @platform.name %>
</p>
從遊戲到show.html.erb但是當我嘗試它,我得到錯誤
未定義方法名稱爲零:NilClass
我錯過了什麼嗎? 我在這裏嘗試了一些解決方案,但仍然錯誤 thx
您是否在控制器中設置了@平臺實例變量? – Uzbekjon
什麼是@平臺?你是否確定在創建遊戲時'platform_id'不是'nil'?因爲'collection_select'是錯誤的。 – Pavan
@Uzbekjon尚未 – June