好的,这是关键时刻。我有一个我正在研究的项目,我无法让它工作。目前我需要创建一个搜索框,它更像是一个允许搜索的简单文本字段。问题是每当我点击搜索按钮时,我都会收到错误“找不到id = search的学生”
我不明白的是:为什么ID被分配了“搜索”的值?我不知道代码在哪里传递了这些信息。我只更改了三个文件:
class StudentsController < ApplicationController
def search
@student = Student.where("name LIKE ?", "%#{params[:q]}%")
render :index
end
# GET /students
# GET /students.json
def index
@students = Student.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @students }
end
end
# GET /students/1
# GET /students/1.json
def show
@student = Student.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @student }
end
end
# GET /students/new
# GET /students/new.json
def new
@student = Student.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @student }
end
end
# GET /students/1/edit
def edit
@student = Student.find(params[:id])
end
# POST /students
# POST /students.json
def create
@student = Student.new(params[:student])
@student.courses << Course.find(1)
respond_to do |format|
if @student.save
format.html { redirect_to @student, notice: 'Student was successfully created.' }
format.json { render json: @student, status: :created, location: @student }
else
format.html { render action: "new" }
format.json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
# PUT /students/1
# PUT /students/1.json
def update
@student = Student.find(params[:id])
@student.courses << Course.find(params[:course][:id])
respond_to do |format|
if @student.update_attributes(params[:student])
format.html { redirect_to @student, notice: 'Student was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
# DELETE /students/1
# DELETE /students/1.json
def destroy
@student = Student.find(params[:id])
@student.destroy
respond_to do |format|
format.html { redirect_to students_url }
format.json { head :no_content }
end
end
end
Assign3::Application.routes.draw do
resources :courses
resources :students
resources :courses_students
resources :students do
collection do
get 'search'
end
end
end
<h1>Listing students</h1>
<%= form_tag(search_students_url, method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
<table>
<tr>
<th>Student num</th>
<th>First name</th>
<th>Last name</th>
<th>School</th>
<th>Years attended</th>
<th>Birthday</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @students.each do |student| %>
<tr>
<td><%= student.student_num %></td>
<td><%= student.first_name %></td>
<td><%= student.last_name %></td>
<td><%= student.school %></td>
<td><%= student.years_attended %></td>
<td><%= student.birthday %></td>
<td><%= link_to 'Show', student %></td>
<td><%= link_to 'Edit', edit_student_path(student) %></td>
<td><%= link_to 'Destroy', student, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Student', new_student_path %>
任何指针都非常感谢,许多人提前感谢。
答案 0 :(得分:2)
替换:
Assign3::Application.routes.draw do
resources :courses
resources :students
resources :courses_students
resources :students do
collection do
get 'search'
end
end
end
使用:
Assign3::Application.routes.draw do
resources :courses
resources :courses_students
resources :students do
collection do
get 'search'
end
end
end
在路由中,规则首先匹配,首次投放,并且您有2 resources :students