多级嵌套表单无法批量分配受保护的属性:

时间:2013-04-29 21:56:39

标签: ruby-on-rails ruby ruby-on-rails-3

我在使用Rails时遇到了很大的问题。问题是,我有32个模型与一个主模型相关联。

模型名为Problem - 每个问题都有leveloneeffectlevelonecauseleveloneendleveloneway

现在,每个上一个级别都有与leveltwoeffect相关联的其他模型与leveloneeffect相关联等等。

每个模型都与其他模型正确关联,表单显示正确提交的所有标签和texfield;但是当我提交时,我收到以下错误:

无法批量分配受保护的属性:levelonecause_id

app / controllers / problems_controller.rb:27:在`update'

现在在控制器中,我在27行中有以下代码

def update
    @problem = Problem.find(params[:id])
    @problem.update_attributes(params[:problem])   <--- LINE 27
flash[:notice] = "Programa editado correctamente."
redirect_to edit_problem_path(@problem)     
end

有什么想法吗?

这是我的问题控制器

    def index
        @problem = Problem.all
    end
    def new
        @problem = Problem.new
    end
    def create
        @problem = Problem.new(params[:problem])
        @problem.user_id = current_user.id
        @problem.save
        flash[:notice] = "Prorgrama Creado."
        redirect_to edit_problem_path(@problem)
    end
    def show
    @problem = Problem.find(params[:id])
    end

    def edit
        @problem = Problem.find(params[:id])
    end

    def update
        @problem = Problem.find(params[:id])
        @problem.update_attributes(params[:problem])
        flash[:notice] = "Programa editado correctamente."
        redirect_to edit_problem_path(@problem)


    end

    def destroy
        @problem = Problem.find(params[:id])
        @Problem.destroy
        flash[:notice] = "Programa borrado correctamente."
        redirect_to problems_path
    end
end

这里添加问题模型

 attr_accessible :leveltwocause_attributes,:indicator_attributes, :leveloneeffect_attributes, :levelonecause_attributes, :budget_program, :city, :department, :email, :name, :responsable_unit, :init_date, :end_date, :organism, :definition, :idea_attributes


  belongs_to :user
  has_many :idea
  has_many :levelonecause
  has_many :leveltwocause, :through => :levelonecause
  has_many :levelthreecause, :through => :leveltwocause
  has_many :levelfourcause, :through => :levelthreecause
  has_many :levelfivecause, :through => :levelfourcause
  has_many :levelsixcause, :through => :levelfivecause
  has_many :levelsevencause, :through => :levelsixcause
  has_many :leveleightcause, :through => :levelsevencause

  has_many :indicator

  has_many :leveloneeffect
  has_many :leveltwoeffect, :through => :leveloneeffect
  has_many :levelthreeeffect, :through => :leveltwoeffect
  has_many :levelfoureffect, :through => :levelthreeeffect
  has_many :levelfiveeffect, :through => :levelfoureffect
  has_many :levelsixeffect, :through => :levelfiveeffect
  has_many :levelseveneffect, :through => :levelsixeffect
  has_many :leveleighteffect, :through => :levelseveneffect

  accepts_nested_attributes_for :idea, :allow_destroy => true
  accepts_nested_attributes_for :levelonecause, :allow_destroy => true
  accepts_nested_attributes_for :leveltwocause, :allow_destroy => true
  accepts_nested_attributes_for :levelthreecause, :allow_destroy => true
  accepts_nested_attributes_for :levelfourcause, :allow_destroy => true
  accepts_nested_attributes_for :levelfivecause, :allow_destroy => true
  accepts_nested_attributes_for :levelsixcause, :allow_destroy => true
  accepts_nested_attributes_for :levelsevencause, :allow_destroy => true
  accepts_nested_attributes_for :leveleightcause, :allow_destroy => true

  accepts_nested_attributes_for :leveloneeffect, :allow_destroy => true
  accepts_nested_attributes_for :leveltwoeffect, :allow_destroy => true
  accepts_nested_attributes_for :levelthreeeffect, :allow_destroy => true
  accepts_nested_attributes_for :levelfoureffect, :allow_destroy => true
  accepts_nested_attributes_for :levelfiveeffect, :allow_destroy => true
  accepts_nested_attributes_for :levelsixeffect, :allow_destroy => true
  accepts_nested_attributes_for :levelseveneffect, :allow_destroy => true
  accepts_nested_attributes_for :leveleighteffect, :allow_destroy => true

  accepts_nested_attributes_for :indicator, :allow_destroy => true

end

这是表格

<%= simple_nested_form_for @problem, :html => { :class => " widgetbox box-inverse form-horizontal" }  do |f| %>




<div class="tabbable"> <!-- Only required for left/right tabs -->
  <ul class="nav nav-pills">
    <li class="active"><a href="#definicion" data-toggle="tab">Definicion del Problema</a></li>
    <li><a href="#arbolproblema" data-toggle="tab">Arbol del Problema</a></li>
     <li><a href="#arbolproblema" data-toggle="tab">Arbol de Objetivos</a></li>
     <li><a href="#arbolproblema" data-toggle="tab">Matriz Marco Logico</a></li>
  </ul>
  <div class="tab-content">
    <div class="tab-pane fade in active" id="definicion">

<ul class="nav nav-tabs">
    <li><a href="#infbasica" data-toggle="tab">Informacion Basica</a></li>
    <li><a href="#nombrar" data-toggle="tab">Definicion del Problema</a></li>
    <li><a href="#lluvia" data-toggle="tab">Lluvia de Ideas</a></li>
  </ul>


<div class="tab-content">
    <div class="tab-pane fade" id="infbasica">
       <div class="span4 offset1"> 
    <%= f.input :name, label: 'Nombre del Programa' %>
    <%= f.input :organism, label: 'Dependencia', collection: ["Dependencia 1", "Dependencia 2", "Dependencia 3"] %>
    <%= f.input :responsable_unit, label: 'Nombre del Responsable del Programa' %>
    <%= f.input :city, label: 'Ciudad', collection: [ "Chihuahua", "DF", "Toluca", "Juarez"] %>
    <%= f.input :department, label: 'Departamento', :collection => [ "Direccion Tecnica", "Proyectos Especiales", "Sub Direccion de Estudios"] %>
    <%= f.input :email, label: 'Correo Electronico', placeholder: 'usuario@correo.com' %>
</div>
        <div class="span4 offset1">
    <%= f.input :init_date, label: 'Fecha problable de Inicio'%>
    <%= f.input :end_date, label: 'Fecha problable de Terminacion' %>
</div>
    </div>

    <div class="tab-pane fade" id="nombrar">
     <div class="span6 offset1">
<%= f.input :definition, label: 'Definicion del Problema', as: :text %>
</div>

    </div>

    <div class="tab-pane fade" id="lluvia">



 <div class="span6 offset1">

<%= f.simple_fields_for :idea do  |i| %>

    <%= i.input :content,  label: 'Idea', as: :text, required: false %>
    <%= i.link_to_remove "Quitar", :class => 'btn btn-danger' %>


<% end %>

 <%= f.link_to_add "Agregar Idea", :idea %>
</div>


    </div>



  </div>




    </div>
    <div class="tab-pane fade" id="arbolproblema">




<ul class="nav nav-tabs">
    <li><a href="#causas" data-toggle="tab">Causas</a></li>
    <li><a href="#efectos" data-toggle="tab">Efectos</a></li>
    <li><a href="#diagcauef" data-toggle="tab">Generar Diagrama</a></li>
  </ul>

<div class="tab-content">
    <div class="tab-pane" id="causas">

<div class="span6 offset1">      
<%= f.simple_fields_for :levelonecause do  |levelone| %>

    <%= levelone.input :content,  label: 'Causa', as: :text, required: false %>
    <%= levelone.link_to_remove "Quitar", :class => 'btn btn-danger' %>


<% end %>

 <%= f.link_to_add "Agregar Causa del nivel 1", :levelonecause %>
</div>
 </div>

    <div class="tab-pane" id="efectos">
<div class="span6 offset1">      
<%= f.simple_fields_for :leveltwocause do  |leveltwocause| %>

     <%= leveltwocause.input :content,  label: 'Efect', as: :text, required: false %>
    <%= leveltwocause.link_to_remove "Quitar", :class => 'btn btn-danger' %>
  <%= leveltwocause.label :levelonecause_id, "Efecto" %>
            <%= leveltwocause.collection_select(:levelonecause_id, Levelonecause.all, :id, :content) %>


<% end %>

 <%= f.link_to_add "Agregar Efecto del nivel 1", :leveltwocause %>
</div>

    </div>

    </div>
  </div>
</div>

 <%= f.button :submit, 'Guardar Datos', class: 'btn btn-primary pull-right' %>
<% end %>
</div>

修改

好的伙计们,我在我的模型中移除了槽,它的工作原理!有谁知道为什么? 这个新的Rails版本正在杀了我

1 个答案:

答案 0 :(得分:0)

尝试将:levelonecause_id添加到attr_accessible levelonecause模型列表以及包含它的其他模型中。