【发布时间】:2020-12-24 05:04:11
【问题描述】:
我刚刚在我的模型中创建了一个搜索方法。但是,它似乎部分起作用。本质上,如果有人要在搜索字段中输入英雄的名字,它应该从索引中提取与英雄相关的所有“报告”以及所有相关数据。
现在,我搜索“雷神”,假设我在与他关联的 4 份报告的列表中有 3 份报告。其他人与其他英雄相关联。它通过动作,然后将我带到索引页面,但不会在页面/表格上显示任何错误或报告。只是一个空的屏幕。像这样。
报表模型
class Report < ApplicationRecord
validates :subject, presence: true, length: { minimum: 6, maximum: 100 }
validates :description, presence: true, length: { minimum: 10, maximum: 300 }
belongs_to :user
has_many :report_heros
has_many :heros, through: :report_heros
has_many :report_villains
has_many :villains, through: :report_villains
def self.search(search)
if search
hero = Hero.find_by(hero_name: search)
if hero
self.where(hero_id: hero.id)
else
Report.all
end
else
Report.all
end
end
end
报告索引
<div class="container">
<h1 class="row justify-content-center">Reports</h1>
<h3> Search for a Hero related Reports</h3>
<%= form_tag reports_path, :method => "get" do %>
<%= text_field_tag(:search, params[:search])%>
<%= submit_tag("Search", :name => nil) %>
<% end %>
<table class="table table-bordered bg-light">
<thead bgcolor="#959595">
<tr>
<th scope="col" class="text-center">Ticket Number</th>
<th scope="col" class="text-center">Subject</th>
<th scope="col" class="text-center">Created By</th>
<th scope="col" class="text-center">Hero Involved</th>
<th scope="col" class="text-center">Villain Involved</th>
<th scope="col" class="text-center">Edit</th>
<th scope="col" class="text-center">Delete</th>
</tr>
</thead>
<tbody>
<% @reports.each do |report|%>
<tr>
<% if report.user == current_user || current_user.admin? %>
<td class="text-center"><%= link_to report.id, report_path(report) %></td>
<td><%= report.subject %></td>
<td><%= link_to report.user.email, report_path(report) %></td>
<% report.heros.each do |hero|%>
<td class="text-center"><%= link_to hero.hero_name, report_hero_path(report.id, hero.id) %></td>
<% end %>
<% report.villains.each do |villain|%>
<td class="text-center"><%= link_to villain.villain_name, report_villain_path(report.id, villain.id) %></td>
<% end %>
<td class="text-center"><%= link_to "Edit Report", edit_report_path(report) %></td>
<td class="text-center"><%= link_to "Delete Report", report_path(report), method: :delete, data: {confirm: "Are you sure?"} %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'New Report', new_report_path, class: "btn btn-primary" %>
</div>
报表控制器
class ReportsController < ApplicationController
before_action :require_login
before_action :authenticate_user!, except: [:index, :show]
before_action :set_report, only: [:show, :edit, :update, :destroy]
before_action :require_same_user, only: [:edit, :update, :destroy]
def index
@reports = Report.all
@reports = Report.search(params[:search])
end
def show
end
def new
@report = current_user.reports.new
end
def create
@report = current_user.reports.build(report_params)
if @report.save
flash[:notice] = "Report was created successfully."
redirect_to @report
else
render 'new'
end
end
def update
if @report.update(report_params)
flash[:notice] = "Report updated successfully"
redirect_to @report
else
render 'edit'
end
end
def edit
end
def destroy
@report.destroy
redirect_to reports_path
end
private
def set_report
@report = Report.find(params[:id])
end
def report_params
params.require(:report).permit(:subject, :description, :hero_ids, :villain_ids, :search)
end
def require_login
unless user_signed_in?
flash[:error] = "You must be logged in to access this section"
redirect_to root_path
end
end
def require_same_user
if current_user != @report.user && !current_user.admin?
flash[:alert] = "You can only edit or delete your own article"
redirect_to @report
end
end
end
这是正在查询的内容-
Started GET "/reports?search=Thor" for ::1 at 2020-09-04 20:58:13 -0500
Processing by ReportsController#index as HTML
Parameters: {"search"=>"Thor"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/reports_controller.rb:57:in `require_login'
Hero Load (0.1ms) SELECT "heros".* FROM "heros" WHERE "heros"."hero_name" = ? LIMIT ? [["hero_name", "Thor"], ["LIMIT", 1]]
↳ app/models/report.rb:15:in `search'
Rendering reports/index.html.erb within layouts/application
Report Load (0.2ms) SELECT "reports".* FROM "reports" WHERE "reports"."hero_id" = ? [["hero_id", 1]]
↳ app/views/reports/index.html.erb:22
Rendered reports/index.html.erb within layouts/application (Duration: 1.1ms | Allocations: 793)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_navigation.html.erb (Duration: 0.5ms | Allocations: 435)
Rendered layouts/_messages.html.erb (Duration: 0.1ms | Allocations: 17)
Rendered layouts/_footer.html.erb (Duration: 0.0ms | Allocations: 5)
Completed 200 OK in 30ms (Views: 8.7ms | ActiveRecord: 0.4ms | Allocations: 7637)
【问题讨论】:
标签: ruby-on-rails search