使用自动填充时不搜索

时间:2012-08-14 10:06:32

标签: ruby-on-rails autocomplete

我遵循了本教程:http://railscasts.com/episodes/102-auto-complete-association

但它不起作用。

我做的是以下内容:

我使用

安装了该插件
rails plugin install git://github.com/rails/auto_complete.git

在我的message.rb中,我声明了user_name

 def user_name
    user.username if user
  end

  def user_name= (username)
    self.user = User.find_or_create_by_username(username) unless username.blank?
  end

在我的application.html.haml中添加了

= javascript_include_tag :defaults

在我的个人资料/ users / index.js.erb

<%= auto_complete_result @users, :username %>

我尝试了http://localhost:8080/profile/users.js

并输出预期的用户列表。

在我的users_controller.rb

    auto_complete_for :message, :user_name
  def index
    @users = User.find(:all, :conditions => ['username LIKE ?', "%#{params[:search]}%"])
    respond_to do |format|
      format.html # index.html.erb
      format.js # index.js.erb
    end
  end

当我耙路线时,我得到了视图所需的正确输出

 profile_users GET    /profile/users(.:format)                                {:action=>"index", :controller=>"profile/users"}

最后在我的个人资料/ messages / compose.html.haml

= form_for :message, :url => send_message_profile_messages_path do |message|
  %label{:for => "friend"} To:
  %br
  = text_field_with_auto_complete :message,:user_name, {:size => 15} , {:url =>profile_users_path(:format => :js), :method => :get, :param_name => 'search' }
  %br
  %label{:for => "message"} Body:
  %br
  = message.text_area :message
  %br
  = message.file_field :attachment
  %br
  = submit_tag "Send", :confirm => "Are you sure?"

我做错了什么?

0 个答案:

没有答案