我对使用ruby编程相当新。我试图用AJAX创建一个Livesearch,但让我很头疼。基本思想是,当用户创建节点时,可以存储相同的内容:创建节点的用户的信息,父节点等。为此,我进行了实时搜索,其中用户输入父节点的ID并自动显示与该节点对应的用户数据。然后,在同一视图中,我想创建节点。到目前为止,我有以下代码,我没有做好工作。
我想要这样的事情:
http://www.subeimagenes.com/img/join-1369789.png
问题:
AJAX无法正常工作,我确定我忘记了重要的事情
不知道如何传递价值" parent_id" (u.id in _users.html.erb," live search"的结果)来创建节点。
nodes_controller.rb
def new
@node = Node.new
end
def search
@node = Node.new
@parent = Node.search(params[:search]).where(:ocuped => true)
if not @users.nil?
if @users.count == 1
@node_incomplete = @users.nodes.where(" sons < ? AND ocuped = ?",2,true).first
else
@node_incomplete = @users.first.nodes.where(" sons < ? AND ocuped = ?",2,true).first
end
@son_of_incompleted_node = @node_incomplete.children
end
respond_to do |format|
format.html
format.js
end
端
search.html.erb
<%= form_for @node, :id => "users_search" do |f| %>
<%= f.text_field :search, :value => params[:search], :autocomplete => 'off' %>
<div id='users'>
<%= render 'users' %>
</div>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :ocuped, :value => true %>
<%= f.text_field :custom_node_name %>
<%= f.check_box :terms_of_service,{}, true,false %>
<%= f.submit "Create", class: "button postfix" %>
<% end %>
_users.html.erb
<% if not @parent.nil? %>
<% @parent.each do |u| %>
<ul class="inline-list">
<li class="img-simple" style = "text-align: center; background-image: url('<%= u.user.profile_img %>')"></li>
<li style="display:table-cell; vertical-align: middle;"><h6><%= u.user.name %></h6><p><%= u.user.email %></p></li>
</ul>
<% end %>
node.rb
def self.search(search)
self.where('id = ? ', search.to_i)
end
的application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require foundation
//= require_tree .
$(function() {
$("#users_search input").keyup(function() {
$.get($("#users_search").attr("action"), $("#users_search").serialize(), null, "script");
return false;
});
});
nodes.js.cofee
jQuery ->
# Ajax search on keyup
$('#users_search input').keyup( ->
$.get($("#users_search").attr("action"), $("#users_search").serialize(), null, 'script')
false
)
视图/节点/ search.js.erb
$('#users').html('<%= escape_javascript(render("users")) %>');