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

チケットビューを使用して新しいログを作成する方法を理解しようとしていますが、ログモデルの詳細フィールドを表示できません。ビューに対する私の試み:

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

1 に答える 1

1

Ticketクラスのattr_accessible行は、attr_accessible:issue、:logs_attributesである必要があります。

それ以外の場合は、次のように取得する必要があります(railsバージョン<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

この単純な作成アクションは、tickets_formのfields_for呼び出しおよびTicketクラスのaccepts_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の方法(単なる提案):

<% 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 に答える