2013-10-09 2 views
6

나는 많은 관계로이 일이 중첩 특성 (accepts_nested_attributes_for)을 형성레일이

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

하지만 그것은 응용 프로그램에서 작동 할 수 없습니다를/뷰/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 %> 

내가 내 양식을 만들기 위해 추가하거나 제거 할 수있는 다른 있나요은 N 역할을 보여줍니다 ested 속성?

이 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 

난 그냥 중첩 된 속성이 편집에 표시 및 작업 만 표시되고 싶어요.

+0

컨트롤러 동작은 어떻게됩니까? –

답변

6

중첩 된 속성의 양식은 실제로 표시 할 데이터가있는 경우에만 표시됩니다. Programa 인스턴스에 하나 이상의 역할이 연결되어있는 경우

양식을 렌더링하기 전에 컨트롤러에 @programa.roles.build처럼 간단하게 새 역할을 추가 할 수 있습니다. 기존 역할이 렌더링됩니다.

편집 : 실제로 양식을 렌더링해야합니다. <%= f.fields_for (참고 : = 누락).

+0

역할 작성은 다른 양식으로 작성됩니다. 사실 role 테이블에 해당하는 programa_id가있는 데이터가 있습니다. @hector right. – hector

+0

내 편집 참조 :) – sevenseacat

+0

믿을 수 없어, 공식 언어! 감사! – hector