Ruby on rails 3 用Coffeescript更新表中的额外列有很多,:through
我在这里有一个相当简单的设置:一个Ruby on rails 3 用Coffeescript更新表中的额外列有很多,:through,ruby-on-rails-3,coffeescript,jquery-ui-sortable,has-many-through,Ruby On Rails 3,Coffeescript,Jquery Ui Sortable,Has Many Through,我在这里有一个相当简单的设置:一个Doc模型,一个出版物模型和一个文章模型 doc.rb class Doc < ActiveRecord::Base attr_accessible :article_ids,:created_at, :updated_at, :title has_many :publications, dependent: :destroy has_many :articles, :through => :publications, :order =&
Doc
模型,一个出版物
模型和一个文章
模型
doc.rb
class Doc < ActiveRecord::Base
attr_accessible :article_ids,:created_at, :updated_at, :title
has_many :publications, dependent: :destroy
has_many :articles, :through => :publications, :order => 'publications.position'
accepts_nested_attributes_for :articles, allow_destroy: false
accepts_nested_attributes_for :publications, allow_destroy: true
end
class Publication < ActiveRecord::Base
attr_accessible :doc_id, :article_id, :position
belongs_to :doc
belongs_to :article
acts_as_list
end
class Article < ActiveRecord::Base
attr_accessible :body, :issue, :name, :page, :image, :article_print, :video, :id
has_many :publications
has_many :docs, :through => :publications
end
def update
@doc = Doc.find(params[:id])
@doc.publications.destroy_all
respond_to do |format|
if @doc.update_attributes(params[:doc])
format.html { redirect_to share_url(@doc) }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @doc.errors, status: :unprocessable_entity }
end
end
end
此咖啡脚本在拖动时保存顺序:
jQuery ->
$('#sort_articles').sortable(
axis: 'y'
handle: '.handle'
update: ->
$.post($(this).data('update-url'), $(this).sortable('serialize'))
);
这是docs\u controller.rb中的排序方法:
def sort
Article.all.each_with_index do |id, index|
Publication.update_all({position: index + 1}, {id: id})
end
render nothing: true
end
我遵循了Rails cast的可排序列表,在我更新Doc
记录之前一切都很好,因为更新时不会保存重新排序。我得出的结论是,这是因为我的可排序字段位于关联表上(即,出版物
),但由于应用程序的工作方式,它必须是
我在这里做了一些研究,发现了一个问题,这个问题的答案很接近,但因为我有一个咖啡脚本动作,首先要保存记录,所以它不起作用
任何帮助都会很好,我真的被卡住了。因为我为此奋斗了很长时间,所以我把我愚蠢的简单解决方案放在这里。希望它能帮助其他人 只需在保存更改之前删除
update
上的旧联接表记录即可,即:
docs\u controller.rb
class Doc < ActiveRecord::Base
attr_accessible :article_ids,:created_at, :updated_at, :title
has_many :publications, dependent: :destroy
has_many :articles, :through => :publications, :order => 'publications.position'
accepts_nested_attributes_for :articles, allow_destroy: false
accepts_nested_attributes_for :publications, allow_destroy: true
end
class Publication < ActiveRecord::Base
attr_accessible :doc_id, :article_id, :position
belongs_to :doc
belongs_to :article
acts_as_list
end
class Article < ActiveRecord::Base
attr_accessible :body, :issue, :name, :page, :image, :article_print, :video, :id
has_many :publications
has_many :docs, :through => :publications
end
def update
@doc = Doc.find(params[:id])
@doc.publications.destroy_all
respond_to do |format|
if @doc.update_attributes(params[:doc])
format.html { redirect_to share_url(@doc) }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @doc.errors, status: :unprocessable_entity }
end
end
end
轰