下面是我的模型:不能得到嵌套形式展现
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