下面是我的模型:不能得到嵌套形式展現
class Ticket < ActiveRecord::Base
attr_accessible :issue, :logs_attributes
has_many :logs
accepts_nested_attributes_for :logs
end
class Log < ActiveRecord::Base
attr_accessible :detail, :ticket_id
belongs_to :ticket
end
我試圖找出如何通過售票視圖中創建新的日誌,但我不能讓在現場詳細要顯示的日誌模型。 我的看法嘗試:
tickets_form
<%= form_for(@ticket) do |f| %>
<% if ... end %>
<div class="field">
<%= f.label :issue %><br />
<%= f.text_area :issue %>
</div>
<% f.fields_for :logs do |builder| %>
<p>
<%= builder.label :detail %><br />
<%= builder.text_area :detail %>
</p>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
票\展會
<p id="notice"><%= notice %></p>
<p>
<b>Issue:</b>
<%= @ticket.issue %>
</p>
<ol>
<% for log in @ticket.logs %>
<p>
<%=log.detail %> *Note:squiggly line under detail said "Cannot find 'detail'"*
</p>
<% end %>
</ol>
<%= link_to 'Edit', edit_ticket_path(@ticket) %> |
<%= link_to 'Back', tickets_path %>
我的控制器:
def new
@ticket = Ticket.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @ticket }
end
end
def create
@ticket = Ticket.new(params[:ticket])
respond_to do |format|
if @ticket.save
format.html { redirect_to @ticket, notice: 'Ticket was successfully created.' }
format.json { render json: @ticket, status: :created, location: @ticket }
else
format.html { render action: "new" }
format.json { render json: @ticket.errors, status: :unprocessable_entity }
end
end
end
票務\顯示
<h1>New ticket</h1>
<%= render 'form' %>
<%= link_to 'Back', tickets_path %>
看着創建行動代碼當前設置(attr_accessible:問題你的票類),你應該得到一個**無法mass-assign受保護的屬性:logs_attributes **你知道嗎? – Kocur4d 2013-03-25 22:18:25
刪除@ ticket.logs。從新動作構建 – Kocur4d 2013-03-25 22:25:33
有一次,我得到了那個錯誤,但我認爲它在我使用'accept_nested_attr'時停止了。我刪除了@ ticket.logs.build,但新的票據表格仍然沒有詳細信息字段 – user2189312 2013-03-25 22:36:15