Search code examples
ruby-on-railsruby-on-rails-4herokuparametersproduction-environment

Passing parameter through link not working in production


I am developing a web application to embedd youtube videos dynamically.

Following are my models:

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 file

  root 'welcome#index'
  resources :categories
  resources :groups
  resources :videos

Homepage contains list of all categories which are links. I am passing id as parameter through the links, so that I can access that ID in groups#index.

<%= @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 %>

When I click on one of the category, I want to redirect to groups#index page but I want to show the groups only belongs to that category.

So, I wrote my groups controller index action like the following.

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

And when I clicking on the category link, It is working fine even if there are no groups created for that category. But when I open in production, It is showing the error if there are no groups for that category. When "heroku logs", the error is

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

So far, the groups#index page is static. Eventhough I am getting error in heroku but working well in development.

What am I doing wrong?


Solution

  • Looks like it is failing at find_group (probably called as part of before_action ?). The controller code you provided doesn't include any reference to that part.