通过 link 传递参数在生产中不起作用

Passing parameter through link not working in production

我正在开发一个网络应用程序来动态嵌入 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

主页包含 link 的所有类别的列表。我将 id 作为参数传递给 links,这样我就可以在 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 %>

当我点击其中一个类别时,我想重定向到群组#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

当我点击类别 link 时,即使没有为该类别创建组,它也能正常工作。但是当我在生产环境中打开时,如果该类别没有组,则会显示错误。当"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" =  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 中遇到错误,但在开发中运行良好。

我做错了什么?

使用:

@group = Group.find_by(id: params[:id])

'find' 方法如果找不到具有该 ID 的记录将抛出错误,而 'find_by' 不会抛出错误。

更好的解决方案是删除 before_action 操作列表中的 :index。我不明白您为什么需要在索引页上使用@group。

看起来它在 find_group 处失败了(可能被称为 before_action 的一部分?)。您提供的控制器代码不包含对该部分的任何引用。