2013-10-09 8 views
6

ich dieses zu viele Beziehung haben:Rails bilden mit verschachtelten Attributen (accepts_nested_attributes_for)

class Programa < ActiveRecord::Base 
    attr_accessible :descripcion, :nombre, :roles_attributes 
    has_many :roles, :dependent => :restrict 
    accepts_nested_attributes_for :roles 
    ... 
end 

class Role < ActiveRecord::Base 
    attr_accessible :description, :name, :programa_id 
    belongs_to :programa 
    ... 
end 

Es in Schienen Konsole funktioniert:

> params = { programa: { nombre: 'nuevo', roles_attributes: [ {name: 'role1'}, {name: 'role2'}] }} 
> p = Programa.create(params[:programa]) 
> p 
=> #<Programa id: 7, nombre: "nuevo", descripcion: nil, created_at: "2013-10-09 14:07:46", updated_at: "2013-10-09 14:07:46"> 
> p.roles 
=> [#<Role id: 15, name: "role1", description: nil, created_at: "2013-10-09 14:07:46", updated_at: "2013-10-09 14:07:46", programa_id: 7>, #<Role id: 16, name: "role2", description: nil, created_at: "2013-10-09 14:07:46", updated_at: "2013-10-09 14:07:46", programa_id: 7>] 

Aber ich kann es nicht in der App funktioniert/views/programas/_form:

<%= form_for(@programa) do |f| %> 
    <%= render 'shared/form_error_messages', object: f.object %> 
    <div class="field"> 
    <%= f.label :nombre %> 
    <%= f.text_field :nombre %> 
    </div> 
    <div class="field"> 
    <%= f.label :descripcion %> 
    <%= f.text_field :descripcion %> 
    </div> 

    <% f.fields_for :roles do |builder| %> 
    <div class="field"> 
     <%= builder.label :name %> 
     <%= builder.text_field :name %> 
    </div> 
    <div class="field"> 
     <%= builder.label :description %> 
     <%= builder.text_field :description %> 
    </div> 
    <% end %> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

gibt es noch etwas, das ich hinzufügen müssen oder entfernen meine Form zu machen, zeigt die Rollen n este Attribute?

Dies ist mein der Controller für programas:

class ProgramasController < ApplicationController 
    # GET /programas 
    # GET /programas.json 
    def index 
    @programas = Programa.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @programas } 
    end 
    end 

    # GET /programas/1 
    # GET /programas/1.json 
    def show 
    @programa = Programa.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @programa } 
    end 
    end 

    # GET /programas/new 
    # GET /programas/new.json 
    def new 
    @programa = Programa.new 
    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @programa } 
    end 
    end 

    # GET /programas/1/edit 
    def edit 
    @programa = Programa.find(params[:id]) 
    end 

    # POST /programas 
    # POST /programas.json 
    def create 
    @programa = Programa.new(params[:programa]) 

    respond_to do |format| 
     if @programa.save 
     format.html { redirect_to @programa, notice: 'Programa was successfully created.' } 
     format.json { render json: @programa, status: :created, location: @programa } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @programa.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /programas/1 
    # PUT /programas/1.json 
    def update 
    @programa = Programa.find(params[:id]) 

    respond_to do |format| 
     if @programa.update_attributes(params[:programa]) 
     format.html { redirect_to @programa, notice: 'Programa was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @programa.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /programas/1 
    # DELETE /programas/1.json 
    def destroy 
    @programa = Programa.find(params[:id]) 
    @programa.destroy 

    respond_to do |format| 
     format.html { redirect_to programas_url } 
     format.json { head :no_content } 
    end 
    end 
end 

Ich möchte nur die verschachtelten Attribute nur in der Bearbeitung und zeigen Aktionen gezeigt werden.

+0

Wie sieht Ihre Controller-Aktion aus? –

Antwort

6

Das Formular für die geschachtelten Attribute wird nur angezeigt, wenn tatsächlich Daten angezeigt werden, z. Wenn Ihrer Programa-Instanz eine oder mehrere Rollen zugeordnet sind.

Dies kann so einfach sein wie @programa.roles.build in Ihrem Controller, vor dem Rendern des Formulars, um eine neue Rolle hinzuzufügen. Alle vorhandenen Rollen werden gerendert.

bearbeiten: Sie müssen auch tatsächlich das Formular rendern, dh. <%= f.fields_for (zu beachten: =).

+0

Die Erstellung einer Rolle erfolgt aus einer anderen Form. Ich habe tatsächlich Daten in Rollen Tabelle mit der entsprechenden programa_id. – hector

+0

@hector rechts. siehe meine Bearbeitung :) – sevenseacat

+0

Kann es nicht glauben, formale Sprachen! Vielen Dank! – hector

Verwandte Themen