通过链接传递参数在生产中不工作
我正在开发一个Web应用程序来动态地嵌入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作为参数,以便我可以在组#索引中访问该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 %>
当我点击类别之一,我想重定向到组#索引页,但我想只显示属于该类别的组。
所以,我写了我的组控制器索引行为如下。
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日志”时,错误是
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
到目前为止,组#索引页是静态的。尽管我在heroku中遇到了错误,但在开发过程中表现良好。
我在做什么错?
看起来它在find_group失败(可能是before_action的一部分?)。您提供的控制器代码不包含对该部分的任何引用。
感谢兄弟:)它正在工作 –
这是一个怎样的答案? – MTarantini
用途:
@group = Group.find_by(id: params[:id])
“查找”方法将抛出一个错误,如果它没有找到与该ID的记录,而“find_by”将不会引发错误。
更好的解决方案是删除before_action列表中的操作索引。我不明白你为什么需要索引页上的@group。
看起来你还没有在生产中创建的组数据 - 填充prod数据库并重试 –
即使我没有开发数据,我也没有收到任何错误。由于组#索引模板仍为静态页面 –
开发模式比生产更容易(例如,查看'whiny_nils'设置)。如果您在生产模式下('RAILS_ENV = production rails s')在本地运行您的Rails服务器,则应该遇到同样的问题 –