2013-03-25 47 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