【发布时间】:2016-03-24 18:18:21
【问题描述】:
我正在开发一个网络应用程序来动态嵌入 youtube 视频。
以下是我的模型:
class Category < ActiveRecord::Base
has_many :groups
end
class Group < ActiveRecord::Base
belongs_to :category
has_many :videos
end
class Video < ActiveRecord::Base
belongs_to :group
end
routes.rb 文件
root 'welcome#index'
resources :categories
resources :groups
resources :videos
主页包含所有链接类别的列表。我通过链接将 id 作为参数传递,以便我可以在 groups#index 中访问该 ID。
<%= @categories.each do |category| %>
<%= link_to groups_path(id: category.id) do %>
<div class="course">
<h2><%= category.name %></h2>
<p><%= category.description %></p>
</div>
<% end %>
<% end %>
当我点击某个类别时,我想重定向到 groups#index 页面,但我想显示只属于该类别的组。
所以,我编写了我的组控制器索引操作,如下所示。
class GroupsController < ApplicationController
before_action :find_group, only: [:index, :edit, :update]
def index
@category = Category.find(params[:id])
@groups = @category.groups
end
def new
@group = Group.new
end
def create
@group = Group.new group_params
if @group.save
flash[:notice] = "Group #{@group} was successfully added."
redirect_to groups_path
else
flash[:notice] = "Oops.. unable to create the group. Please check for errors."
redirect_to groups_path
end
end
def edit
end
def update
if @group.update update_params
flash[:notice] = "Group #{@group} was successfully updated."
redirect_to groups_path
else
flash[:notice] = "Oops.. unable to update the group. Please check for errors."
redirect_to groups_path
end
end
private
def group_params
params.require(:group).permit(:title, :description, :category_id)
end
def find_group
@group = Group.find(params[:id])
end
end
当我单击类别链接时,即使没有为该类别创建组,它也可以正常工作。但是当我在生产中打开时,如果该类别没有组,则会显示错误。当“heroku logs”时,错误是
2016-03-24T17:25:05.102549+00:00 app[web.1]: Started GET "/groups?id=2" for 117.213.140.66 at 2016-03-24 17:25:05 +0000
2016-03-24T17:25:05.107691+00:00 app[web.1]: Processing by GroupsController#index as HTML
2016-03-24T17:25:05.107750+00:00 app[web.1]: Parameters: {"id"=>"2"}
2016-03-24T17:25:05.115174+00:00 app[web.1]: Group Load (6.1ms) SELECT "groups".* FROM "groups" WHERE "groups"."id" = $1 LIMIT 1 [["id", 2]]
2016-03-24T17:25:05.115830+00:00 app[web.1]: Completed 404 Not Found in 8ms (ActiveRecord: 6.1ms)
2016-03-24T17:25:05.118387+00:00 app[web.1]:
2016-03-24T17:25:05.118409+00:00 app[web.1]: ActiveRecord::RecordNotFound (Couldn't find Group with 'id'=2):
2016-03-24T17:25:05.118410+00:00 app[web.1]: app/controllers/groups_controller.rb:45:in `find_group'
2016-03-24T17:25:05.118411+00:00 app[web.1]:
2016-03-24T17:25:05.118411+00:00 app[web.1]:
2016-03-24T17:25:05.128576+00:00 heroku[router]: at=info method=GET path="/groups?id=2" host=akashpinnaka.herokuapp.com request_id=eb1cd798-fcf4-4aaa-87b3-9fd20d5b00d8 fwd="117.213.140.66" dyno=web.1 connect=0ms service=36ms status=404 bytes=1758
2016-03-24T17:25:05.963007+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=akashpinnaka.herokuapp.com request_id=b040c32d-fad6-4a64-95ef-94d057a5a34b fwd="117.213.140.66" dyno=web.1 connect=0ms service=4ms status=304 bytes=62
到目前为止,groups#index 页面是静态的。尽管我在 heroku 中遇到错误,但在开发中运行良好。
我做错了什么?
【问题讨论】:
-
您似乎还没有生产中创建的组的数据 - 填充产品数据库并重试
-
即使我没有开发中的数据,我也没有收到任何错误。因为 groups#index 模板仍然是静态页面
-
开发模式比生产模式宽容得多(例如,查看
whiny_nils设置)。如果您以生产模式 (RAILS_ENV=production rails s) 在本地运行您的 rails 服务器,您应该会遇到同样的问题 -
是的,我该如何克服这个问题?谢谢:)
-
提供
groups_controller.rb的完整代码。看起来它在find_group失败(可能被称为before_action的一部分?)。您提供的控制器代码不包含对该部分的任何引用。
标签: ruby-on-rails ruby-on-rails-4 heroku params production-environment