我设置了一个控制器警报,以在索引或显示路由返回到零时向我发出警报,即尝试路由到它们时,即我单击link_to链接到涉及“ Thor”的英雄并收到此错误。
我最终要完成的工作是尝试将这些路线嵌套在一起,以便所有英雄和最终恶棍都将被关联并嵌套在报告路线中,并且用户将最终能够输入新的英雄(如果还没有的话)。但是,它似乎并未分配hero_id或villain_id。但是,它确实分配了提交报告的用户的user_id。
我怀疑这与我的关联被关闭或未正确同步有关,但是我可能是错的。
英雄控制器
def index
if params[:report_id]
@report = Report.find_by(id: params[:report_id])
if @report.nil?
redirect_to reports_path, alert: "Report not found"
else
@heros = @reports.heros
end
else
@heros = Hero.all
end
end
def show
if params[:report_id]
@report = Report.find_by(id: params[:report_id])
@hero = @report.heros.find_by(id: params[:id])
if @hero.nil?
flash[:notice] = "Hero not found"
redirect_to report_hero_path(@report)
end
else
@hero = Hero.find(params[:id])
end
end
def create
@hero = Hero.build(hero_params)
if @hero.save
flash[:notice] = "Hero was successfully created"
redirect_to @hero
else
render 'new'
end
end
def hero_params
params.require(:hero).permit(:hero_name)
end
报表控制器参数
def report_params
#byebug
params.require(:report).permit(:subject, :description, :hero_ids, :villain_ids)
end
报告模型
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
end
英雄模型
class Hero < ApplicationRecord
validates :hero_name, presence: true, length: { minimum: 3, maximum: 25 }
validates_uniqueness_of :hero_name
has_many :report_heros
has_many :reports, through: :report_heros, dependent: :destroy
end
路线
Rails.application.routes.draw do
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' },
skip: [:mailer, :unlocks, :passwords, :confirmations]
resources :users, except: :new
resources :reports do
resources :heros, only: [:index, :new, :show]
end
resources :heros
resources :villains
get 'about', to: 'pages#about'
root 'pages#home'
end
报告索引
<div class="container">
<h1 class="row justify-content-center">Report Index</h1>
<table class="table table-bordered bg-light">
<thead bgcolor="#959595">
<tr>
<th scope="col">Subject</th>
<th scope="col">Description</th>
<th scope="col">Created By</th>
<th scope="col">Hero Involved</th>
<th scope="col">Villain Involved</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
<% @reports.each do |report|%>
<tr>
<td><%= report.subject %></td>
<td><%= report.description %></td>
<td><%= link_to report.user.email, report_path(report) %></td>
<% report.heros.each do |hero|%>
<td><%= link_to hero.hero_name, report_heros_path(:hero_id, :report_id) %></td>
<% end %>
<% report.villains.each do |villain|%>
<td><%= villain.villain_name %></td>
<% end %>
<% if report.user == current_user %>
<td><%= link_to "Edit Report", edit_report_path(report) %></td>
<td><%= 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>