2013-03-25 81 views
0

下面是我的模型:不能得到嵌套形式展现

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 %> 
+0

看着创建行动代码当前设置(attr_accessible:问题你的票类),你应该得到一个**无法mass-assign受保护的属性:logs_attributes **你知道吗? – Kocur4d 2013-03-25 22:18:25

+0

删除@ ticket.logs。从新动作构建 – Kocur4d 2013-03-25 22:25:33

+0

有一次,我得到了那个错误,但我认为它在我使用'accept_nested_attr'时停止了。我删除了@ ticket.logs.build,但新的票据表格仍然没有详细信息字段 – user2189312 2013-03-25 22:36:15

回答

1

票务类你attr_accessible行应该是attr_accessible:问题:logs_attributes

否则,你应该得到(如果你使用的是轨道版本< 4):

Can't mass-assign protected attributes: logs_attributes 

如果你不明白这一点,必须有你的控制器创建行动的问题(你可以更新你的问题?控制器代码)

它应该是这个样子:

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 

这个简单的创建操作连同fields_for在您的tickets_form和中调用accep_nested_attributes_for:logs将在Ticket类中一次打包父级和关联对象。

门票/ new.html.erb应该是这个样子:

<h1>New ticket</h1> 

<%= render 'form' %> 

<%= link_to 'Back', tickets_path %> 

和窗体部分门票/ _form.html.erb应该是:

<%= 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 %> 

离线主题:

多一点Rails方式做t他的(只是一个建议):

<% for log in @ticket.logs %> 
    <p> 
    <%=log.detail %> 
    </p> 
<% end %> 

是:

<% @ticket.logs.each |log| %> 
    <%= content_tag(:p, log.detail) %> 
<% end %> 
+0

我会试试。我添加了控制器代码btw – user2189312 2013-03-25 22:12:20

+0

细节领域仍然不会显示 – user2189312 2013-03-25 22:30:45