我想在搜索关键字时获得 json 格式的数据,所以我使用 LIKE 子句并像这样查询
"select * from employees where fname like ? or mname like ? or lname like ? or username like ? or id like ?", str, str, str, str, str
但我想使用 Rails 对其进行编码。我的控制器中有这段代码
def showemployees
str = params[:str]
render json: @employee = Employee.where(Employee.employees[:fname].matches("%#{str}%")) or
(Employee.employees[:mname].matches("%#{str}%")) or
(Employee.employees[:lname].matches("%#{str}%")) or
(Employee.employees[:id].matches("%#{str}%"))
end
这段代码在我的 config/routes.rb 中
get 'employees/showemployees'
root :to => 'employees#new'
resources :employees
post 'employees/update_info'
当我输入这个时http://localhost:3000/employees/showemployees?str=samplename http://localhost:3000/employees/showemployees?str=samplename,应该会出现 json 格式的记录,但我收到此错误消息
undefined method `employees' for #<Class:0x8e38900>
app/controllers/employees_controller.rb:6:in `showemployees'
其中第 6 行有此代码
render json: @employee = Employee.where(Employee.employees[:fname].matches("%#{str}%")) or