我遵循了本教程: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?"
我做错了什么?