我有一个复杂的表格来安排事件.以下是缩写关联:
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
Run Code Online (Sandbox Code Playgroud)
表单正在创建一个新事件,我有以下结构:
- 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
Run Code Online (Sandbox Code Playgroud)
客户端已存在,并从选择菜单中选择.观察者通过在控制器操作中设置客户端变量然后渲染部分来呈现嵌套属性表单.
这是我得到的错误:
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'
Run Code Online (Sandbox Code Playgroud)
我遇到问题(我认为)与Event和Client之间的belongs_to关联.当事件属于客户端时,我不知道Event是否可以接受客户端的嵌套属性.我总是以相反的方式完成它(客户端接受Event的嵌套属性).
有任何想法吗?如果您需要更多代码或背景,我可以详细说明.谢谢!
更新:根据请求添加了控制器代码.
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
Run Code Online (Sandbox Code Playgroud)
结束
尝试在选项中添加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 %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6554 次 |
| 最近记录: |