2016-11-24 52 views
0

感謝您能否提供幫助。我正在使用Rails 4.2.6並試圖爲虛擬健身房的虛擬網站進行搜索。Rails - 搜索表單返回錯誤

在每一頁上我有代碼搜索表單:

<%= form_tag "/client_workouts/find" do %> 
    <%= text_field_tag :search_string %> 
    <%= submit_tag "Search" %> 
    <% end %> 

我的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(這是'def find'我編輯過的,以使搜索表單工作):

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 

回答

1

您的查詢應該像

ClientWorkout.where("client_name LIKE ? OR trainer LIKE ?", "%#{params[:search_string]}%", "%#{params[:search_string]}%") 

此外,FindActiveRecord它可以接受id一個方法或idsobject查找記錄。它還接受idscollection以從表中返回記錄。您編寫查詢的方式可能不正確。

我建議看看這個頁面:http://guides.rubyonrails.org/active_record_querying.html如果你剛剛開始學習Rails ,那麼我強烈推薦閱讀一些類似Crafting Rails和其他書籍的書籍。所有最好的

+0

它的工作,謝謝!我有點困惑,因爲我從OReilly Head First Rails書中獲得了我的代碼,但是感謝您提供關於新書的建議。 :) – crcerror

+0

我很高興它的工作。不確定爲什麼它在書中寫成這樣,但確實要檢查Crafting Rails書籍,你會學到非常棒的東西。 – Abhinay