感谢您的帮助。我正在使用Rails 4.2.6并尝试为健身房制作想象网站的搜索表单。
在每个页面上,我都有一个代码为
的搜索表单 <%= form_tag "/client_workouts/find" do %>
<%= text_field_tag :search_string %>
<%= submit_tag "Search" %>
<% end %>
my routes.rb:
Rails.application.routes.draw do
resources :client_workouts
post 'client_workouts/find' => 'client_workouts#find'
end
我的schema.rb:
ActiveRecord::Schema.define(version: 20161103044039) do
create_table "client_workouts", force: :cascade do |t|
t.string "client_name"
t.string "trainer"
t.integer "duration_mins"
t.date "date_of_workout"
t.decimal "paid_amount"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
和我的脚手架client_workouts_controller.rb(&#39; def find&#39;我编辑了以使搜索表单工作):
class ClientWorkoutsController < ApplicationController
before_action :set_client_workout, only: [:show, :edit, :update, :destroy]
def find
@client_workouts = ClientWorkout.find(:all,
:conditions=>["client_name = ? OR trainer = ?", params[:search_string], params[:search_string]])
end
# GET /client_workouts
# GET /client_workouts.json
def index
@client_workouts = ClientWorkout.all
end
# GET /client_workouts/1
# GET /client_workouts/1.json
def show
end
# GET /client_workouts/new
def new
@client_workout = ClientWorkout.new
end
# GET /client_workouts/1/edit
def edit
end
# POST /client_workouts
# POST /client_workouts.json
def create
@client_workout = ClientWorkout.new(client_workout_params)
respond_to do |format|
if @client_workout.save
format.html { redirect_to @client_workout, notice: 'Client workout was successfully created.' }
format.json { render :show, status: :created, location: @client_workout }
else
format.html { render :new }
format.json { render json: @client_workout.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /client_workouts/1
# PATCH/PUT /client_workouts/1.json
def update
respond_to do |format|
if @client_workout.update(client_workout_params)
format.html { redirect_to @client_workout, notice: 'Client workout was successfully updated.' }
format.json { render :show, status: :ok, location: @client_workout }
else
format.html { render :edit }
format.json { render json: @client_workout.errors, status: :unprocessable_entity }
end
end
end
# DELETE /client_workouts/1
# DELETE /client_workouts/1.json
def destroy
@client_workout.destroy
respond_to do |format|
format.html { redirect_to client_workouts_url, notice: 'Client workout was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_client_workout
@client_workout = ClientWorkout.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def client_workout_params
params.require(:client_workout).permit(:client_name, :trainer, :duration_mins, :date_of_workout, :paid_amount)
end
end
但是当我试图通过表格找到某些东西时,我得到了:
Couldn't find all ClientWorkouts with 'id':
(all, {:conditions=>["client_name = ? OR trainer = ?", "lol", "lol"]})
(found 0 results, but was looking for 2)
Extracted source (around line #5):
def find
@client_workouts = ClientWorkout.find(:all,
:conditions=>["client_name = ? OR trainer = ?",
params[:search_string], params[:search_string]])
end
答案 0 :(得分:1)
您的查询应该是
ClientWorkout.where("client_name LIKE ? OR trainer LIKE ?", "%#{params[:search_string]}%", "%#{params[:search_string]}%")
此外,Find
是来自ActiveRecord
的方法,它接受id
或ids
或object
来查找记录。它还接受collection
ids
来从表中返回记录。你编写查询的方式可能不对。
我建议查看此页面:http://guides.rubyonrails.org/active_record_querying.html如果您刚开始学习Rails 那么我强烈建议你阅读Crafting Rails和其他书籍。一切顺利