【问题标题】:Rails form with nested attributes (accepts_nested_attributes_for)带有嵌套属性的 Rails 表单 (accepts_nested_attributes_for)
【发布时间】:2013-10-09 14:22:57
【问题描述】:

我有这种一对多的关系:

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

它在 Rails 控制台中工作:

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

但我不能让它在 app/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 %>

还有什么我必须添加或删除才能使我的表单显示角色嵌套属性的吗?

这是我的程序控制器:

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

我只想在编辑中显示嵌套属性并仅显示操作。

【问题讨论】:

  • 你的控制器动作是什么样的?

标签: ruby-on-rails nested-attributes has-many


【解决方案1】:

嵌套属性的表单只有在实际有数据要显示时才会显示,即。如果您的 Programa 实例有一个或多个与之关联的角色。

这可以像@programa.roles.build 在您的控制器中一样简单,在呈现表单之前,添加一个新角色。将呈现任何现有角色。

edit:您还需要实际呈现表单,即。 &lt;%= f.fields_for(注意缺少=)。

【讨论】:

  • 角色的创建是由其他形式的。我实际上在角色表中有数据及其相应的 programa_id。
  • 不敢相信,正式语言!谢谢!
  • 谢谢,我也忘记了=。当我做一百次时,事情会变得模糊。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-07
  • 2010-11-16
  • 1970-01-01
相关资源
最近更新 更多