我發現了很多關於這個Railscast的帖子,但所有的建議都沒有幫助我。 我已經能夠在視圖中呈現一個嵌套的表單域,但只有一個,而不是我在控制器中調用的3個域。當我提出,我得到的錯誤:無法大規模指派保護的屬性:線索Rails 3 railscast#196,3次循環不工作
Chapter.rb
class Chapter < ActiveRecord::Base
belongs_to :trail
has_many :clues, :dependent => :destroy
accepts_nested_attributes_for :clues
attr_accessible :asset, :assetkind, :description, :gate, :name, :trail, :trail_id, :cover
.
.
.
end
Clue.rb
class Clue < ActiveRecord::Base
attr_accessible :chapter_id, :theclue, :typeof, :chapter
.
.
.
belongs_to :chapter
end
在它說,使用railcast相當於:線索,並且這呈現3個字段。但在我的情況下,它並沒有渲染田野。相反,我使用@ chapter.clues,它只呈現一個。
我在創建新章節時的表格。
<h1>Add a New Chapter</h1>
<h3>Add To Trail : <%= @trail.title %></h3><br>
<%= form_for [@trail, @trail.chapters.build] do |f| %>
<h6>About the Chapter</h6>
<%= f.label :name, 'Chapter Name' %>
.
.
.
<h6>Progressing the Story</h6>
<%= f.fields_for @chapter.clues do |builder| %>
<p>
<%= builder.label :theclue, "Enter Clue" %>
<%= builder.text_area :theclue, :rows => 2 %>
</p>
<% end %>
.
.
.
<% end %>
我chapters_controller.rb新
class ChaptersController < ApplicationController
def new
@trail = Trail.find(params[:trail_id])
@chapter = Chapter.new
@title = "Chapter"
3.times { @chapter.clues.build }
logger.debug "CHAPTER!!!!!!!!!!!!new: am i in a trail? #{@trail.to_yaml}"
logger.debug "CHAPTER!!!!!!!!!!!!new: am i in a clue? #{@chapter.clues.to_yaml}"
end
我的日誌顯示我3條線索,但屬性是空的(無:ID)。這是一個錯誤的跡象?所以即使我的日誌顯示3個線索對象,我的視圖也只顯示一個。
想法?我已經,由於對計算器的建議,加入到chapter.rb
attr_accessible :clues_attributes
,並沒有運氣,同樣的行爲和錯誤並且沒有。
預先感謝您的時間
感謝您的回覆。我試圖添加:線索和:clues_attributes到atrr_accessible這一章,我得到了同樣的錯誤:無法批量分配受保護的屬性:線索....當我使用:線索我得到的錯誤:未知屬性:線索...... ? – HappaGirl