【发布时间】:2015-01-07 16:12:55
【问题描述】:
我有这个模型:
class CompanyCrawler < ActiveRecord::Base
....
serialize :entry_pages, Array
def entry_page_objects
entry_pages.map { |url| EntryPage.new(url) }
end
def entry_page_objects_attributes=(attributes)
# ...
end
....
end
这个表格来渲染模型:
.....
%p
%p
= crawler_form.label 'Entry pages'
= crawler_form.text_area :entry_pages_text, size: '80x6'
%ul.entry-pages
= crawler_form.fields_for :entry_page_objects do |entry_page_field|
%li=entry_page_field.text_field :url, size: 80
%a{href: '#', class: 'add-button'} Add Entry Page
我遇到的问题是表单错误地呈现了entry_page_object 输入名称(例如company_crawler[entry_page_objects_attributes][0][url] 而不是company_crawler[entry_page_objects][0][url])。我真的不知道该怎么做,我已经阅读了文档,并且示例说只需定义 attr_attributes=(attributes) 和 persisted? 我就可以将 fields_for 用于集合,只要它们是用 accept_nested_fields 定义的关联.
我已经看到了不同的解决方案,例如将 String 'entry_page_objects[]' 给 fields_for 但我想与 Rails 命名约定保持一致,我知道我可以使用 form_tag 而不是 form_for 但我想fields_for 按预期工作。
【问题讨论】:
标签: ruby ruby-on-rails-3 haml