Rails 帮助预填充数据 - 令牌输入字段

发布于 2024-11-10 16:12:53 字数 2113 浏览 4 评论 0原文

我正在尝试将数据预先填充到令牌输入字段中。

但我的编辑视图中没有预先填充任何内容。

我关注了这个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏拉图鍀咏恒 2024-11-17 16:12:53

尝试在编辑视图中使用 input_html 包装 data-pre:

:input_html => {"data-pre" => @konkurrancer.tags.map(&:attributes).to_json }

* 您正在使用 simple_form,而 Railscasts 则没有。

Try wrapping your data-pre with input_html in your edit view:

:input_html => {"data-pre" => @konkurrancer.tags.map(&:attributes).to_json }

* You're using simple_form while the railscasts isn't.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文