Show nested in show of the parent model

0

I have 2 models I have a father and another son, I can create the child form without problems within the father.

<h3>Agregar Programa</h3>
<%= link_to_add_association 'Agregar', f, :lists, 
  'data-association-insertion-node' => "#lists ol",
  'data-association-insertion-method' => "append",
  :wrap_object => Proc.new {|list| list}, class: 'btn btn-primary'  %>

<hr/>
<fieldset id="lists">
  <ol>
    <%= f.fields_for :lists do |list| %>
      <%= render partial: "list_fields", locals: { f: list} %>
    <% end %>
  </ol>
</fieldset>

but now, how can I make the% of the parent's% share what was entered in the child?

I do not know if I explain.

    
asked by Angel Ac 14.09.2016 в 17:30
source

1 answer

0

There are several ways to do it. the first would be to load a global variable from the action of your controller this I imagine would be the show then it would be something like that.

def show
  @padre = Padre.find(params[id])
  # Este metodo hijos que escribo seria el nombre de tu relacion has_many
  # que tengas en tu modelo.
  @hijos = @padre.hijos
end

# Luego en tu show.html.erb tendrias que recorrer tu variable
@hijos.each do |hijo|
  # aqui escribirias todo el html para renderizar tus hijos
  <p><%= hijo.nombre %></p>
end

The other way would be that with your global variable parent from the controller in the action show call your relationship from the view. example

def show
  # solo retornarias el padre en la accion
  @padre = Padre.find(params[id])
end


# y en tu vista show.html.erb tendrias que llamar a tu relacion con la 
# variable padre
# el metodo hijos seria el nombre de tu relacion has_many que tengas en tu modelo.
@padre.hijos.each do |hijo|
  # aqui escribirias todo el html para renderizar tus hijos
  <p><%= hijo.nombre %></p>
end

I hope I help you.

    
answered by 24.11.2016 в 17:10