ruby-on-rails – 接受belongs_to关联上的嵌套属性
发布时间:2020-12-17 03:44:21 所属栏目:百科 来源:网络整理
导读:我有一个复杂的表格来安排事件.以下是缩写关联: class Event ActiveRecord::Base belongs_to :client accepts_nested_attributes_for :client,:reject_if = lambda { |a| a[:name].blank? }endclass Client ActiveRecord::Base has_many :events has_many :
我有一个复杂的表格来安排事件.以下是缩写关联:
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 客户端已存在,并从选择菜单中选择.观察者通过在控制器操作中设置客户端变量然后渲染部分来呈现嵌套属性表单. 这是我得到的错误: 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' 我遇到问题(我认为)与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 结束 解决方法
尝试在选项中添加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 %> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |