Ruby on rails 为什么我会在这件事上被提名?

Ruby on rails 为什么我会在这件事上被提名?,ruby-on-rails,ruby,activerecord,rails-routing,Ruby On Rails,Ruby,Activerecord,Rails Routing,我的config/routes.rb中有以下内容: 按_区域过滤的_操作如下所示: #filtered_by_region method def filtered_by_region @region = Region.where(title: params[:region]).first @category = Category.where(title: params[:category]).first @teams = Team.where(region_id: @region.i

我的config/routes.rb中有以下内容:

按_区域过滤的_操作如下所示:

#filtered_by_region method
def filtered_by_region
  @region = Region.where(title: params[:region]).first
  @category = Category.where(title: params[:category]).first
  @teams = Team.where(region_id: @region.id, category_id: @category.id)
end
我有一个按_region.html.erb过滤的视图,如下所示:

Region: <%= @region.title %>
Category: <%= @category.title %>

<% @teams.each do |team|%>
  <%=team.title %>
<% end %>
class Region < ActiveRecord::Base
  has_many :teams
  attr_accessible :title
end
class Category < ActiveRecord::Base
  has_many :teams
  attr_accessible :title
end
category.rb模型如下:

Region: <%= @region.title %>
Category: <%= @category.title %>

<% @teams.each do |team|%>
  <%=team.title %>
<% end %>
class Region < ActiveRecord::Base
  has_many :teams
  attr_accessible :title
end
class Category < ActiveRecord::Base
  has_many :teams
  attr_accessible :title
end
我还有相应的区域、团队和类别表,已经填充了数据

当我输入如下所示的url时:

http://localhost:3000/football/south_west
我收到以下消息的错误: nil的未定义方法“title”:NilClass我意识到@region和@category都返回nil,但我不明白为什么。在类别和地区表中,我确实有一个带有足球标题的类别和一个带有西南标题的地区。

如果您使用的是Rails 4或Rails 3,为什么不使用:

def filtered_by_region
   @category = Category.find_by_title(params[:category])
   @region = Region.find_by_title(params[:title])

   if defined?(@category) && defined?(@region)
       @teams = Team.where(region_id: region.id, category_id: category.id)
   else
       redirect_to root_path
   end
end

我认为可能的问题是您的查询未找到任何记录,或者您将尝试以记录的形式访问集合,而不管使用的是什么。首先

请指定错误产生的文件和行号。根据您发布的内容,我们无法帮助您。你真正的问题是为什么找不到我的地区/类别?你还没有发布任何能帮助我们告诉你的信息。打开rails控制台并确保您可以找到记录。@meagar当我运行rails控制台时,我可以同时获得区域和类别。例如,当我这样做时:category=category.wheretitle:“football”。首先:我得到category Load 31.2ms SELECT categories.*从categories WHERE categories.title=“football”按标题限制排序1=>@meager我不确定这是否有帮助,但当我检查服务器日志时,没有执行db查询。没有应获取@region、@category或@teams的查询。应用程序只是呈现视图。我已经尝试了find_by,但它仍然不起作用,我意识到问题不是查询没有找到任何记录,而是查询根本没有被触发。当我检查服务器日志时,应用程序只是呈现视图,而没有尝试查找@category、@region或@teamsVery。你确定它会影响控制器的动作吗?您的路线可能有问题吗?非常感谢,我犯了一个粗心的错误,没有关闭“按区域过滤”方法上方的方法。因此,应用程序无法找到操作,因此在不点击操作的情况下呈现视图。你的建议很有帮助。