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

我试图弄清楚如何通过工单视图创建新日志,但我无法显示日志模型中的详细信息字段。我对观点的尝试:

门票表格

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

1 回答 1

1

您在Ticket类中的 attr_accessible 行应该是attr_accessible :issue, :logs_attributes

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

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 和 accept_nested_attributes_for 中的fields_for调用一起:Ticket 类中的日志将一次性创建父对象和关联对象。

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 方法来做到这一点(只是一个建议):

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

将会:

<% @ticket.logs.each |log| %>  
  <%= content_tag(:p, log.detail) %>
<% end %>
于 2013-03-25T21:58:19.817 回答