我正試圖在Rails 4應用程序中創建一個搜索欄。我是我的用戶數據庫爲用戶的'名稱'和'電子郵件'列 - 我希望用戶能夠通過名稱或ID搜索其他用戶。Rails 4搜索欄
目前我得到這個:
ActiveRecord::RecordNotFound in UsersController#index
Couldn't find all Users with 'id': (all, {:conditions=>["name LIKE ?", "%[email protected]%"]}) (found 0 results, but was looking for 2)
有誰知道我做錯了嗎?我已經看過railscasts和一些論壇等,但目前無法通過這一點。
index.html.erb:
<% form_tag users_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
模型/ user.rb:
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
users_controller.rb:
def index
@users = User.search(params[:search])
end
routes.rb中:
get 'search' => 'users#index'
這對我有效,謝謝@ jamesuriah! – sss333 2014-10-20 01:13:35