Rails 帮助预填充数据 - 令牌输入字段
我正在尝试将数据预先填充到令牌输入字段中。
但我的编辑视图中没有预先填充任何内容。
我关注了这个railscast:http://railscasts.com/episodes/258-token-fields
我的控制器:
class Admin::TagsController < Admin::AdminController
layout 'admin'
def index
@title = 'asdsadas'
@kategoris = Tag.where("name like ?", "%#{params[:q]}%")
respond_to do |format|
format.html
format.json { render :json => @kategoris.map(&:attributes) }
end
end
end
我的 application.js:
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
$(function() {
$("#konkurrancer_tag_tokens").tokenInput("http://localhost:3000/admin/tags.json", {
crossDomain: false,
prePopulate: $("#konkurrancer_tag_tokens").data("pre"),
theme: "facebook"
});
});
我的编辑视图:
<h1>Editing kategori</h1>
<%= simple_form_for(@konkurrancer, :url => {:action => 'update', :id => @konkurrancer.id }) do |f| %>
<%= f.input :tag_tokens, :label => 'Tags', "data-pre" => @konkurrancer.tags.map(&:attributes).to_json %>
<%= f.button :submit, :value => 'Edit konkurrence' %>
<% end %>
令牌字段输出:
<div class="input string optional">
<label for="konkurrancer_tag_tokens" class="string optional"> Tags</label>
<ul class="token-input-list-facebook"><li class="token-input-input-token-facebook">
<input type="text" autocomplete="off" style="outline: medium none; width: 30px;">
<tester style="position: absolute; top: -9999px; left: -9999px; width: auto; font-size: 13.3333px; font-family: MS Shell Dlg; font-weight: 400; letter-spacing: normal; white-space: nowrap;"></tester></li></ul>
<input type="text" size="50" name="konkurrancer[tag_tokens]" id="konkurrancer_tag_tokens" class="string optional" style="display: none;"></div>
I am trying to pre-populate data to a token input field.
But nothing gets pre-populated in my edit view.
I have followed this railscast: http://railscasts.com/episodes/258-token-fields
My controller:
class Admin::TagsController < Admin::AdminController
layout 'admin'
def index
@title = 'asdsadas'
@kategoris = Tag.where("name like ?", "%#{params[:q]}%")
respond_to do |format|
format.html
format.json { render :json => @kategoris.map(&:attributes) }
end
end
end
My application.js:
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
$(function() {
$("#konkurrancer_tag_tokens").tokenInput("http://localhost:3000/admin/tags.json", {
crossDomain: false,
prePopulate: $("#konkurrancer_tag_tokens").data("pre"),
theme: "facebook"
});
});
My edit view:
<h1>Editing kategori</h1>
<%= simple_form_for(@konkurrancer, :url => {:action => 'update', :id => @konkurrancer.id }) do |f| %>
<%= f.input :tag_tokens, :label => 'Tags', "data-pre" => @konkurrancer.tags.map(&:attributes).to_json %>
<%= f.button :submit, :value => 'Edit konkurrence' %>
<% end %>
Token field output:
<div class="input string optional">
<label for="konkurrancer_tag_tokens" class="string optional"> Tags</label>
<ul class="token-input-list-facebook"><li class="token-input-input-token-facebook">
<input type="text" autocomplete="off" style="outline: medium none; width: 30px;">
<tester style="position: absolute; top: -9999px; left: -9999px; width: auto; font-size: 13.3333px; font-family: MS Shell Dlg; font-weight: 400; letter-spacing: normal; white-space: nowrap;"></tester></li></ul>
<input type="text" size="50" name="konkurrancer[tag_tokens]" id="konkurrancer_tag_tokens" class="string optional" style="display: none;"></div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在编辑视图中使用 input_html 包装 data-pre:
* 您正在使用 simple_form,而 Railscasts 则没有。
Try wrapping your data-pre with input_html in your edit view:
* You're using simple_form while the railscasts isn't.