jquery token 输入插件不传递选择列表中的 id
我正在开发一个简单的 Rails 插件,用户可以选择一个包含数据库中可用学校列表的表单。我的问题是正在获取学校列表,但如果我选择数组中的其余学校,我会得到第一个项目的 id。我可能做错了什么?我的代码示例如下
我的用户模型
belongs_to :school
attr_reader :school_tokens
def school_tokens=(id)
self.school_id = id.split(",")
end
然后在我的学校模型中我
has_many :users
在我的表单中我有
<%= f.label :school, "School Name" %>
<%= f.text_field :school_tokens %>
我的application.js看起来
$(function(){
$('#user_school_tokens').tokenInput("/school_streets.json", {
crossDomain: false,
tokenLimit: 1
});
最后我的学校控制器是这样的
def index
@school_streets = SchoolStreet.where("name LIKE ?", "%#{params[:q]}%")
respond_to do |format|
format.html # index.html.erb
format.json { render json: @school_streets.map(&:attributes) }
end
end
注意:学校显示得很好,但是当我选择一所学校时,例如列表中的第二所学校,然后提交其保存的表格,其中仅包含列表中第一所学校的 ID。
感谢您的帮助。
I am working on a simple rails plugin where a user can select a form containing the list of school available in the database. My problem is that the list of schools is being fetched but if I select the rest of the schools in the array I get the first item's id. What could I be doing wrong? My code sample is as follows
my user model has
belongs_to :school
attr_reader :school_tokens
def school_tokens=(id)
self.school_id = id.split(",")
end
then in my school model I have
has_many :users
in my form I have
<%= f.label :school, "School Name" %>
<%= f.text_field :school_tokens %>
my application.js looks like
$(function(){
$('#user_school_tokens').tokenInput("/school_streets.json", {
crossDomain: false,
tokenLimit: 1
});
finally my schools controller is like this
def index
@school_streets = SchoolStreet.where("name LIKE ?", "%#{params[:q]}%")
respond_to do |format|
format.html # index.html.erb
format.json { render json: @school_streets.map(&:attributes) }
end
end
Note: the schools display very well, but when I select a school, for instance the second school in the list, and submit the form it saves with only the first school's id in the list.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
tokenLimit: 1
行表示您一次只能选择一所“学校”。删除此项以提交多个。The line
tokenLimit: 1
means you can only select one 'school' at a time. Remove this to submit multiple.