- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在努力添加使用 best_in_place 更新联系人语言的功能(一个 jQuery 就地编辑器 gem)。
我有一个联系人对象、一个语言对象以及联系人和语言之间的has_and_belongs_to_many :languages 关系。因此,has_and_belongs_to_many 暗示了联系人和语言表之间的连接表。快速总结一下,我有这样的东西:
contacts contacts_languages languages
+------+--------+ +------------+-------------+ +------+------------+
| id | name | | contact_id | language_id | | id | name |
+------+--------+ +------------+-------------+ +------+------------+
| 1 | John | | 1 | 2 | | 1 | EN |
| 2 | Mike | | 1 | 3 | | 2 | FR |
| 3 | Dick | | 2 | 1 | | 3 | DE |
+------+--------+ | 3 | 1 | | 4 | ES |
| 3 | 5 | | 5 | ZH |
| 3 | 6 | | 6 | JP |
+------------+-------------+ +------+------------+
现在,为了显示和编辑联系语言,我使用了一个表格,其中我有:
- @contact.languages.uniq.each_with_index do |lang, index|
= best_in_place lang, :name, type: :select, collection: @all_languages.map { |name| [name.id, name.name] }, classes: "#{'empty' if lang.blank?}"
请注意,我在这里使用的是 slim 模板引擎。
@contact 在 Controller 中设置为 Contact.find(params[:id])
@contact.languages 返回 #language 对象的 ActiveRecord@all_languages 在 Controller 中设置为 Language.order("name ASC")
另外,请注意 @contact.language_ids
会打印 @contact
的 language_id
数组,我正在使用 @contact .update_attributes(language_ids: [1, 2, 3])
设置联系语言。
上面的当前源代码可以显示语言,但在更新它们时不起作用,因为 lang
被 best_in_place 用作对象。因此,我最终不会像这样更新 *contacts_languages* 表,而是更新 languages 表:
languages
+------+------------+
| id | name |
+------+------------+
| 1 | 1 |
| 2 | FR |
| 3 | DE |
| 4 | ES |
| 5 | ZH |
| 6 | JP |
+------+------------+
其中 name 已替换为应该设置到 contacts_languages 表的语言的 ID。
您知道如何更新 *contacts_languages* 表而不是编辑后的语言吗?如果可能,一切都使用 best_in_place。
谢谢!
contact.rb
# == Schema Information
#
# Table name: contacts
#
# id :integer not null, primary key
# first_name :string(255)
# last_name :string(255)
# title :string(255)
# age :float
# gender :integer
# active :integer
# info :text
# created_at :datetime not null
# updated_at :datetime not null
# user_id :integer
# slug :string(255)
# account_id :integer
# image :string(255)
# uid :integer
# tracking_friend_token :string(255)
# tracking_friend_id :string(255)
# user_token :string(255)
# job_title :string(255)
# dob :date
# marital_status :integer
#
class Contact < ActiveRecord::Base
include PublicActivity::Model
tracked except: :destroy,
owner: Proc.new{ |controller, model| controller.current_user },
recipient: ->(controller, model) { model && model }
include PgSearch
pg_search_scope :search, against: [:first_name, :last_name, :uid],
associated_against: { email_addresses: :email }
after_create :set_defaults
attr_accessible :first_name, :last_name, :title, :age, :gender, :active, :info, :user_id, :type_list, :industry_list, :tag_list, :category_list, :phone_numbers_attributes, :email_addresses_attributes, :social_networks_attributes, :addresses_attributes, :websites_attributes, :instant_messengers_attributes, :company_ids, :language_ids, :user_ids, :slug, :account_id, :image, :uid, :tracking_friend_token, :tracking_friend_id, :job_title, :dob, :marital_status, :children_attributes, :important_dates_attributes, :user_token
#Will Paginate default
self.per_page = 100
acts_as_tenant(:account)
acts_as_taggable
acts_as_taggable_on :type, :industries, :categories
has_many :addresses
has_many :alerts
has_many :attachments
has_many :call_requests
has_many :children
has_many :comments
has_many :contact_companies
has_many :email_addresses
has_many :important_dates
has_many :instant_messengers
has_many :phone_numbers
has_many :pushes
has_many :social_networks
has_many :websites
has_many :companies, through: :contact_companies
has_and_belongs_to_many :languages
has_and_belongs_to_many :users
accepts_nested_attributes_for :addresses, :reject_if => proc { |attributes| attributes['address_line_1'].blank? }, :allow_destroy => true
accepts_nested_attributes_for :attachments, :reject_if => proc { |a| a['file'].blank? }, :allow_destroy => true
accepts_nested_attributes_for :children, :reject_if => proc { |a| a['first_name'].blank? }, :allow_destroy => true
accepts_nested_attributes_for :comments, :reject_if => proc { |a| a['comment'].blank? }, :allow_destroy => true
accepts_nested_attributes_for :email_addresses, :reject_if => proc { |a| a['email'].blank? }, :allow_destroy => true
accepts_nested_attributes_for :important_dates
accepts_nested_attributes_for :instant_messengers, :reject_if => proc { |a| a['name'].blank? }, :allow_destroy => true
accepts_nested_attributes_for :languages, :reject_if => proc { |a| a['iso'].blank? }, :allow_destroy => true
accepts_nested_attributes_for :phone_numbers, :reject_if => proc { |a| a['number'].blank? }, :allow_destroy => true
accepts_nested_attributes_for :social_networks, :reject_if => proc { |a| a['name'].blank? }, :allow_destroy => true
accepts_nested_attributes_for :websites, :reject_if => proc { |a| a['url'].blank? }, :allow_destroy => true
validates :first_name, :presence => true
validates :last_name, :presence => true
validates :active, :presence => true
def reference_id
"ID-USR-000000#{self.id}"
end
def fullname
"#{first_name.titleize} #{last_name.titleize}"
end
def set_defaults
type_list = "Customer" unless self.type_list.present?
language_ids = 1 unless self.language_ids.present?
self.update_attributes(language_ids: language_ids, type_list: type_list)
end
#Postgres fulltext search
def self.text_search(query)
if query.present?
search(query)
else
scoped
end
end
end
语言.rb
# == Schema Information
#
# Table name: languages
#
# id :integer not null, primary key
# name :string(255)
# iso :string(255)
# contact_id :integer
# company_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class Language < ActiveRecord::Base
attr_accessible :name, :iso
has_and_belongs_to_many :contacts
validates :name, presence: true, uniqueness: true
validates :iso, presence: true, uniqueness: true
default_scope order("name ASC")
end
最佳答案
我认为以下内容可能是您的最佳选择。我尝试以更“Rails”的方式进行,但 BIP 不允许这样做。
查看
<% @contact.languages.uniq.each_with_index do |lang, index| %>
<%= best_in_place lang, :id,
type: :select,
path: "/contacts/bip/#{@contact.id}/#{lang.id}",
collection: @all_languages.map { |i| [i.id, i.name] } %>
<% end %>
contacts_controller
def bip
@contact = Contact.find(params[:c_id])
ContactsLanguage.where({contact_id: params[:c_id],language_id: params[:l_id]}).delete_all
ContactsLanguage.create! contact_id: params[:c_id], language_id: params[:language][:id]
respond_to do |format|
format.json { render json: @contact }
end
end
路线
match '/contacts/bip/:c_id/:l_id' => 'contacts#bip'
关于ruby-on-rails - Rails 4 - best_in_place gem 和多对多关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21184592/
我无法让它工作。我已按照 here 描述的安装过程进行操作。该网站上的其他解决方案没有帮助。我在开发者工具上不断收到此错误 static_pages.self-8c8a5c3….js?body=1:7
我正在使用 best_in_place gem,因此我可以在我的索引 View 中编辑多个学生。 但问题是可用性很差。我必须单击、键入信息、输入/单击并再次单击以编辑其他信息。 这是我可以按 Tab
我正在使用 best_in_place 对数据表进行页内编辑 在 ruby-on-rails 应用程序中。就地编辑工作,但我有一个 失败的极端案例。行中的一对项目 (device_name, ge
我正在使用best_in_place gem 并根据说明,为了使用 jquery.purr 实现优雅的错误处理,我所要做的就是将其添加到我的 application.js 中: //= require
我正在尝试使用 best_in_place gem,但它似乎不起作用。我遵循了 Railscast 教程,但现在运气好。当我说它不起作用时,我的意思是没有选项可以编辑屏幕上的任何内容。以默认rails
在显示页面中,我显示周期性事件发生的日期列表,以及对所有日期计算的统计信息,例如连续日期之间的最大、最小和平均间隔。 我使用 best_in_place gem 来允许就地编辑日期。但是,每次更改日期
我正在使用 best_in_place gem在我的 Rails 应用程序中允许进行内联编辑。我在尝试呈现一些文本 html 安全时遇到了问题。 没有指定文本应该是 html_safe,这是它在页面上
我查看了整个 Google 和 StackOverflow,并查看了 best_in_place javascript 代码,但无济于事。 我正在使用 best_in_place 通过文本区域编辑 P
我目前正在尝试使用 best_in_place gem 以便在 HTML 表中进行内联编辑。我正在购物车的显示 View 中显示购物车。在购物车的显示 View 中,我可以添加 lineItems。创
我无法将 gem best_in_place 添加到我的表中。 class_datatable.rb: def data class.map do |record| [
希望有一个简单的答案;我正在使用 gem best_in_place而且效果很好。我试图弄清楚如何使用以下方法创建下拉菜单: :type => :select, :collection => [] 我
Rails 3.2.11 gem 文件: gem 'jquery-rails' gem "best_in_place" 应用程序.js //= require jquery //= require j
在 Rails 项目中,我使用 best_in_place 日期选择器,但在日期格式方面遇到一些问题。 由best_in_place生成的html: 16 Aug 正如您从data-original-
我正在尝试使用 type => date 的 best_in_place gem,但我在格式方面遇到了一些问题。我的 Rails 日期格式如下: Time::DATE_FORMATS.merge!({
我从 Bernat 的博客 Rails 迷你教程中获取了评论代码 我尝试使用他的 best_in_place gem 添加就地编辑功能。在大多数情况下,它工作正常 我的代码部署在 http://fal
编辑 #2 在我看来我应该能够使用 javascript 来 JSON 解析任何 respond_with_bip(@buildingTour)返回,因为那是我需要的更新对象(我需要访问 @build
我正在使用 best_in_place gem在几个文本区域上,有时(看似随机)文本区域“跳跃”并在我单击保存时触发模糊事件,提示“放弃您的更改”警告。 代码如下: Administrator
我尝试使用以下方法执行此操作: "30", :rows => "50", :inner_class => 'occupation-edit',
关于如何在 Best In place 中使用 display_with 选项的文档并不多,但我试图让 Best_in_Place 以精简形式(mm/dd/yyyy)显示日期。 我的数据库(sqlse
我想我遗漏了一些东西,因为似乎无法通过 CSS 区分 best_in_place 值和占位符(data-nil="enter your number")。我只想将占位符的样式设置为与实际值不同,这样它
我是一名优秀的程序员,十分优秀!