6

我正在尝试让令牌字段在我的应用程序上工作。

我完全按照这个视频:http ://railscasts.com/episodes/258-token-fields

项目模型

class Project < ActiveRecord::Base
  attr_accessible  :edited_first_name, :edited_last_name, :first_name, :last_name, :business_div, :client, :customer_benifits, :edited_date, :end_date, :entry_date,  :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech , :technols, :technol_tokens


has_many :projecttechnols
has_many :technols, :through => :projecttechnols

attr_reader :technol_tokens

  def technol_tokens=(ids)
    self.technol_ids = ids.split(",")
  end


accepts_nested_attributes_for(:technols)

技术控制器:

def index
    @technols = Technol.where("tech like ?", "%#{params[:q]}%")

    respond_to do |format|
      format.html # index.html.erb
    format.json { render :json => @technols.map(&:attributes) }
    end
  end

布局/application.html.erb

<!DOCTYPE html>
<html>
<head>

<div id="user_nav">
  <% if user_signed_in? %>
    Hi <%= current_user.firstname %>, Signed in as  <%= current_user.email %>.  Not you?

<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
  <% else %>
    <%= link_to "Sign up", new_user_registration_path %> or
    <%= link_to "Sign in", new_user_session_path %>
  <% end %>
</div>
<% if current_user.try(:admin?) %>
 <div class="admin_button">
<%= button_to "ADMIN", users_path, :class => "button", :method => "get" %>

</div>




<% end %>
  <title>ExceptionApp</title>



<!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js" type="text/javascript"></script>
  <![endif]-->
  <%= stylesheet_link_tag    "application", "token-input-facebook" %>
  <%= csrf_meta_tags %>
<link href="application.css" rel="stylesheet" />



<meta name="viewport" content="width=device-width; initial-scale=1.0">


<link rel="SHORTCUT ICON"
       href="/assets/minilogo.png">



<%= link_to(image_tag("/assets/logo.png"), projects_path, :class => "logow") %>

</head>
<body>

<%= yield %>


<style>
.debug {


position:absolute; right:0px; bottom:0px;

}
</style>
<div class ="debug">
<%= debug(params) %>
<div>


<%= content_tag(:div, flash[:error], :id => "flash_error") if flash[:error] %>
<%= content_tag(:div, flash[:alert], :id => "flash_alert") if flash[:alert] %>
<!-- Javascripts
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <%= javascript_include_tag :defaults, "application", "jquery.tokeninput" %>

  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>


</body>
</html>

项目/new.html.erb

<%= stylesheet_link_tag "new" %>


<h1>Create New Project</h1>

<%= stylesheet_link_tag "form" %>


<%= form_for(@project) do |f| %>



<p>
    <%= f.label :technol_tokens, "technol" %><br />
    <%= f.text_field :technol_tokens, "data-pre" => @project.technols.map(&:attributes).to_json %>
  </p>



</BODY>
</HTML>


<div class="create_button">
<div class="actions">
    <%= f.submit "Save New Project", :class => "button",  :confirm => "Are you sure you want to save the new project?" %>
  </div>
</div>




</div> <%#= small div %>





<% end %>



<div class="back_button2">
<%= button_to "Back", projects_path , :class => "button", :method => "get" %>
</div>

application.js编辑:

$(function() {
  $("#project_technol_tokens").tokenInput("/technols.json", {
    crossDomain: false,
    prePopulate: $("#project_technol_tokens").data("pre"),
    theme: "facebook"
  });
});



$(function() {
  $("#project_start_date").datepicker({dateFormat: 'dd-mm-yy'});
});

$(function() {
  $("#project_end_date").datepicker({dateFormat: 'dd-mm-yy'});
});



jQuery(function(){

jQuery('#start_date_A').datepicker({dateFormat: "dd-mm-yy"});
});

jQuery(function(){

jQuery('#start_date_B').datepicker({dateFormat: "dd-mm-yy"});
});

在我的新页面上,我有一个可以工作的 jquery datepicker 文本框。我不明白为什么同一页面上的令牌字段不起作用。

我已经按照视频大约 3 次来确保它仍然无法正常工作。唯一的区别是我将我的 javascripts 和样式表放在 assets 文件夹中,在视频中它放在 public 文件夹中。任何人都可以帮忙吗?谢谢。

4

1 回答 1

0

不是一个可能的错误。请尝试以下方法:

  1. 检查您的 application.js 文件。您是否包含了 jquery.tokeninput.js 文件?
  2. 确保application.js中jquery后需要token-input js文件
  3. 检查以确保包含 CSS 文件
  4. 在浏览器中,您是否在控制台中收到任何 js 错误?如果是这样,请修改问题。

编辑

另外,我刚刚注意到,在您的 new.html.erb 中,在 html 结束标记之后有一个 div !

</BODY>
</HTML>

请删除/将它们放置在适当的位置。

于 2012-11-19T10:06:44.313 回答