gpt4 book ai didi

ruby-on-rails - 接受belongs_to 关联上的嵌套属性

转载 作者:行者123 更新时间:2023-12-03 16:00:39 25 4
gpt4 key购买 nike

我有一个用于安排事件的复杂表格。以下是缩写的关联:

class Event < ActiveRecord::Base
belongs_to :client
accepts_nested_attributes_for :client, :reject_if => lambda { |a| a[:name].blank? }
end

class Client < ActiveRecord::Base
has_many :events
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }
end

该表单正在创建一个新事件,我具有以下结构:
- form_for @event do |event_form|
%select=collection_select(client_options_for_select, :options, :group_name, :id, :name, @event.client_id)

- event_form.fields_for :client do |client|
= client.text_field :name

- client.fields_for :questions do |question|
= question.text_field :content

客户端已经存在并且是从选择菜单中选择的。观察者通过在 Controller 操作中设置客户端变量然后呈现部分来呈现嵌套的属性表单。

这是我得到的错误:
ActionView::TemplateError (wrong number of arguments (0 for 1)) on line #1 of app/views/proceedings/_questions.html.haml:
1: - event_form.fields_for :client do |client|

app/views/proceedings/_questions.html.haml:1:in `form'
app/views/proceedings/_questions.html.haml:1:in `_run_haml_app47views47events47_client_questions46html46haml_locals_client_questions_object'
haml (3.0.21) rails/./lib/haml/helpers/action_view_mods.rb:13:in `render'
app/controllers/proceedings_controller.rb:261:in `__instance_exec0'
app/controllers/proceedings_controller.rb:260:in `corp_client_questions'
app/controllers/proceedings_controller.rb:258:in `corp_client_questions'

我在事件和客户端之间的belongs_to 关联上遇到了问题(我认为)。不知道当事件属于客户端时,事件是否可以接受客户端的嵌套属性。我总是反过来做(客户端接受事件的嵌套属性)。

有任何想法吗?如果您需要更多代码或背景,我可以详细说明。谢谢!

更新:根据要求添加了 Controller 代码。
def client_questions
if params[:client_id].blank?
render_no_client_questions
elsif @client = Client.find(params[:client_id]) and @client.is_unspecified?
render_no_client_questions
else
respond_to do |format|
format.js {
render :update do |page|
page[:client_questions].replace_html :partial => 'client_questions', :layout => false
end
}
end
end

结尾

最佳答案

尝试在选项中添加 fields_for 对象的实例......通常在创建新的顶级表单对象时,符号是不够的......尝试以下操作,但是可以接受belongs_to上的嵌套属性。

<%= event_form.fields_for :client, @client do |client| %>
<%= client.text_field :name %>
<%= client.fields_for :questions, Question.new do |question| %>
<%= question.text_field :content %>
<% end %>
<% end %>

关于ruby-on-rails - 接受belongs_to 关联上的嵌套属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3855781/

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