0

,我有以下的Rails 4.2应用的机型列表:的Rails 4.2的has_many通过就地编辑嵌套形式xeditable

  • 用户
  • 公司
  • 市场

的关联看起来如下:

User.rb

class User < ActiveRecord::Base 
end 

Company.rb

class Company < ActiveRecord::Base 

    has_many :company_marketplaces 
    has_many :marketplaces, through: :company_marketplaces 

    accepts_nested_attributes_for :marketplaces, update_only: true 
end 

Marketplace.rb

class Marketplace < ActiveRecord::Base 
    has_many :company_marketplaces 
    has_many :companies, through: :company_marketplaces 
end 

我想建立一个形式允许添加/删除交易市场到公司借助就地编辑库x-editable。我能够允许从现有列表中添加新市场,但由于没有提交数据,因此它在删除时失败。

这是视图的相关部分:

index.html.slim

 td 
      = link_to company.marketplaces.join(", "), "", data: { :"name" => "marketplaces_attributes" , :"xeditable" => "true", :"url" => admin_agents_company_path(company), :"pk" => company.id, :"model" => "company", :"type" => "checklist", :"placement" => "right", :"value" => company.marketplaces.join(", "), :"source" => "/marketplaces" }, class: "editable editable-click" 

而且companies_controller.rb#update方法如下所示:

companies_controller.rb

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 

我能找到的大部分资源都不会在现场进行编辑或使用fields_fornested_forms一起使用。

是否有处理加/的形式除去has_many:through:相关联的对象,而不依赖于fields_for溶液?

感谢

回答

-1

可以提供marketplaces_attributes在PARAMS。

实施例从:http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

class Member < ActiveRecord::Base 
    has_many :posts 
    accepts_nested_attributes_for :posts 
end 

params = { member: { 
    name: 'joe', posts_attributes: [ 
    { title: 'Kari, the awesome Ruby documentation browser!' }, 
    { title: 'The egalitarian assumption of the modern citizen' }, 
    { title: '', _destroy: '1' } # this will be ignored 
    ] 
}} 

member = Member.create(params[:member]) 
member.posts.length # => 2 
member.posts.first.title # => 'Kari, the awesome Ruby documentation browser!' 
member.posts.second.title # => 'The egalitarian assumption of the modern citizen' 

阅读更多的例子在该链接上方

相关问题