belongs_to on activerecord rendering as not found

这一生的挚爱 提交于 2019-12-11 06:42:26

问题


I must be doing something stupid here. I am using rails 3.2.19 with activeadmin 0.6.0. Basically I'm trying to do a navigate down through a belongs_to association via a side bar. I have loaded my database with text fixtures and can drill down through the association at the rails console, i.e:

2.1.1 :004 > Employee.first.blogposts.first
Employee Load (0.1ms)  SELECT "employees".* FROM "employees" LIMIT 1
Blogpost Load (0.1ms)  SELECT "blogposts".* FROM "blogposts" WHERE "blogposts"."employee_id" = 615722309 LIMIT 1
=> #<Blogpost id: 298486374, title: "Mine too", body: "Can we try markdown?", employee_id: 615722309, created_at: "2014-07-25 03:27:14", updated_at: "2014-07-25 03:27:14"> 
2.1.1 :005 > 

My model associations are nothing fancy. Basically as simple as they can be:

class Blogpost < ActiveRecord::Base
  belongs_to :employee
end

class Employee < ActiveRecord::Base
   has_many :blogposts
end

And the same with my active admin resources. I built these based on the documentation.

ActiveAdmin.register Employee do

  sidebar "Details", only: [:show, :edit] do
    ul do
      li link_to("Blogposts", admin_employee_blogposts_path(employee))
    end
  end
end

ActiveAdmin.register Blogpost do
  belongs_to :employee
end

I can also see the route specified when I do rake routes

admin_employee_blogposts GET        /admin/employees/:employee_id/blogposts(.:format)              admin/blogposts#index

The link on the employee page (the one defined at the Employee resource) renders without error, however when I click on it I receive:

NoMethodError in Admin::BlogpostsController#index undefined method `find' for nil:NilClass Parameters: {"employee_id"=>"615722309"}

The URL specified in the browser is:

http://localhost:3000/admin/employees/615722309/blogposts

Does anybody know how to troubleshoot this? I am certain that the particular employee ID in question has blog posts, as I demonstrated by using the rails console. Any help anybody could provide would be greatly appreciated.

Thank you,

Dan Sullivan


回答1:


Try using this:

ActiveAdmin.register Blogpost do
  belongs_to :employee, parent_class: Employee
end


来源:https://stackoverflow.com/questions/24958503/belongs-to-on-activerecord-rendering-as-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!