Question

I have a model Order. In the index, I want the current_user to see the index with only their orders. Order belongs_to :admin_user. AdminUser has_many :orders. I am using activeadmin in my app, if that makes a difference. I am getting this error:

Couldn't find Order without an ID

The line giving the error is the second line in my order controller.(redacted unnecessary info)

index do 
    @order = Order.where(admin_user_id: current_admin_user.id, order_id: resource.id)
     column "ID" do |order|
      link_to order.id, admin_order_path(order)
     end
    column "Proof" do |order|
     image_tag order.proof_url(:proof).to_s
        end
        column "Name" do |order|
      link_to order.name, admin_order_path(order)
    end
    column(:customer, :sortable => :customer_id)
        column "Category", :order_category
        column "Status", :order_status
        column "Priority", :order_priority 
    column "Due Date", :end_date
    default_actions
  end

here is my order model requested by @jamesw

class Order < ActiveRecord::Base
  attr_accessible :color_back, :color_front, :color_sleeve, :end_date, :name, :start_date, :whiteboard, :customer_id, :order_category_id, :order_type_id, :order_status_id, :order_priority_id, :print_location_id, :artwork, :proof, :line_items_attributes, :assignee_id, :admin_user_id

  mount_uploader :artwork, ArtworkUploader
  mount_uploader :proof, ProofUploader


  has_many :line_items
  belongs_to :assignee, :class_name => "AdminUser"
  belongs_to :customer
  belongs_to :order_category
  belongs_to :order_type
  belongs_to :order_status
  belongs_to :order_priority
  belongs_to :print_location
  belongs_to :admin_user
  accepts_nested_attributes_for :line_items, :allow_destroy => true

  scope :owned_by, lambda { |user| includes(:assignee).where("admin_users.id = ?", user.id) }



  def default_values
    if new_record?
      self.start_date ||= Date.today
      self.number ||= (Order.maximum(:number) + 1 rescue 1)
    end
  end
end
Was it helpful?

Solution 3

I fixed the issue by taking the line in question out and using scope_to :current_user. I am wondering though, how to add a conditional statement to still allow the admin to view this? here is a look at the controller now.

scope_to current_user

index do 
     column "ID" do |order|
      link_to order.id, admin_order_path(order)
     end
    column "Proof" do |order|
     image_tag order.proof_url(:proof).to_s
        end
        column "Name" do |order|
      link_to order.name, admin_order_path(order)
    end
    column(:customer, :sortable => :customer_id)
        column "Category", :order_category
        column "Status", :order_status
        column "Priority", :order_priority 
    column "Due Date", :end_date
    default_actions
  end

OTHER TIPS

It looks like you're trying to filter the Orders table by order_id. Unless you've built your DB in a non-standard manner, the ID field of the orders table would typically be id (not order_id).

That issue aside, I doubt you want to be passing in an order id for the index action since that would only return a single record, and by it's nature the index action should list many records (in your case, all records for the current_admin_user).

If neither of those issues solve your problem, try commenting out the lines 1 by 1.

Try to add this controller method

ActiveAdmin.register Order do
  controller do
    def scoped_collection
     Order.where(:admin_user => current_admin_user.id)
    end
  end
end

see more here: Two pages for the same resource - ActiveAdmin

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top