お手伝いいただければ幸いです。私は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
それは、ありがとう!私はOReilly Head First Railsの本からコードを取得しているので少し混乱しますが、新しい本のアドバイスをいただきありがとうございます。 :) – crcerror
私はそれがうまく働いた。なぜこの本のように書かれているのですが、確かにCrafting Railsの書籍をチェックして、本当にすばらしいものを学ぶでしょう。 – Abhinay