Ruby on rails 4 Rails 4.2有许多通过xeditable进行就地编辑的嵌套表单
我在Ruby on rails 4 Rails 4.2有许多通过xeditable进行就地编辑的嵌套表单,ruby-on-rails-4,form-submit,frontend,nested-forms,x-editable,Ruby On Rails 4,Form Submit,Frontend,Nested Forms,X Editable,我在Rails 4.2应用程序中有以下型号列表: 使用者 公司 市场 这些关联如下所示: User.rb class User < ActiveRecord::Base end class Company < ActiveRecord::Base has_many :company_marketplaces has_many :marketplaces, through: :company_marketplaces accepts_nested_attribut
Rails 4.2
应用程序中有以下型号列表:
- 使用者
- 公司
- 市场
class User < ActiveRecord::Base
end
class Company < ActiveRecord::Base
has_many :company_marketplaces
has_many :marketplaces, through: :company_marketplaces
accepts_nested_attributes_for :marketplaces, update_only: true
end
def update
respond_to do |format|
if @company.update(company_params)
format.html { redirect_to @company, notice: 'Company was successfully updated.' }
format.json
format.js
else
format.html { render :edit }
format.json { render json: @company.errors, status: :unprocessable_entity }
format.js
end
end
end
和companys_controller.rb#update
方法如下所示:
公司\u controller.rb
class User < ActiveRecord::Base
end
class Company < ActiveRecord::Base
has_many :company_marketplaces
has_many :marketplaces, through: :company_marketplaces
accepts_nested_attributes_for :marketplaces, update_only: true
end
def update
respond_to do |format|
if @company.update(company_params)
format.html { redirect_to @company, notice: 'Company was successfully updated.' }
format.json
format.js
else
format.html { render :edit }
format.json { render json: @company.errors, status: :unprocessable_entity }
format.js
end
end
end
我能找到的大多数资源都不处理就地编辑或使用字段来处理嵌套表单
是否有一种解决方案可以处理添加/删除的多个:
到:
表单中的关联对象,而不依赖字段
谢谢您可以在params中提供marketplaces\u属性
示例来自:
类成员2
member.posts.first.title#=>“Kari,很棒的Ruby文档浏览器!”
member.posts.second.title#=>“现代公民的平等主义假设”
阅读以上链接中的更多示例