user2189312
user2189312

Reputation: 145

Can't get nested form to show

Below are my models:

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

I'm trying to figure out how to create new log via the ticket view, but I can't get the detail field in the log model to show. My attempt on the views:

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

tickets\show

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

My Controller:

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\show

<h1>New ticket</h1>

<%= render 'form' %>

<%= link_to 'Back', tickets_path %>

Upvotes: 0

Views: 105

Answers (1)

Kocur4d
Kocur4d

Reputation: 6931

Your attr_accessible line in Ticket class should be attr_accessible :issue, :logs_attributes.

Otherwise you should get(if you are using rails version < 4):

Can't mass-assign protected attributes: logs_attributes

If you don't get it there must be a problem with your controller create action(can you update your question with a controller code?)

It should look something like this:

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

This simple create action together with fields_for call in your tickets_form and accepts_nested_attributes_for :logs in Ticket class will crate both parent and association object in a one go.

tickets/new.html.erb should look something like this:

<h1>New ticket</h1>

<%= render 'form' %>

<%= link_to 'Back', tickets_path %>

And the form partial tickets/_form.html.erb should be:

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

Off topic:

A bit more Rails way to do this(just a suggestion):

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

Would be:

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

Upvotes: 1

Related Questions