无法获取嵌套表单来显示

时间:2013-03-25 16:44:12

标签: ruby-on-rails nested

以下是我的模特:

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

1 个答案:

答案 0 :(得分:1)

Ticket 类中的attr_accessible行应为 attr_accessible:issue,:logs_attributes

否则你应该得到(如果你使用的是rails版本&lt; 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

这个简单的创建操作与ticket_form中的 fields_for 调用以及Ticket类中的 accepts_nested_attributes_for:logs 将一次性创建父对象和关联对象。

tickets / new.html.erb 应如下所示:

<h1>New ticket</h1>

<%= render 'form' %>

<%= link_to 'Back', tickets_path %>

表单部分 ticket / _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方式(仅提供建议):

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

将是:

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