gpt4 book ai didi

ruby-on-rails - 具有嵌套属性的Rails表单(accepts_nested_attributes_for)

转载 作者:行者123 更新时间:2023-12-04 06:03:12 24 4
gpt4 key购买 nike

我有一对多的关系:

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

我还需要添加或删除其他内容以使表单显示角色嵌套属性吗?

这是我的程序 Controller :
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

我只希望嵌套属性仅在编辑和显示操作中显示。

最佳答案

仅当实际有数据要显示时,才显示嵌套属性的形式。如果您的Programa实例具有一个或多个与之关联的角色。

在呈现表单之前,可以像添加 Controller 中的@programa.roles.build一样简单,以添加新角色。任何现有角色都将被呈现。

编辑:您还需要实际渲染表单,即。 <%= f.fields_for(请注意缺少=)。

关于ruby-on-rails - 具有嵌套属性的Rails表单(accepts_nested_attributes_for),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19274730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com