Question

I have this model "Schedule" with attributes :id, :augurid, #and so on. And I have another model "Augur" with attributes :id, :name, :email #and so on.

Now I want to display augur.name in the Schedule page in Rails_Admin, and my code is like following:

config.model "Schedule" do
    list do
      fields :id, :weekdays, :occupied, :starting, :duration
      field :augur do
        pretty_value do
          augur = Augur.find(:augurid)
          augur.name
        end
      end
    end
    edit do
      fields :id, :augurid, :weekdays, :occupied, :starting, :duration
    end
    show do
    end
  end

But this code is not working, I don't know why since I'm new to rails, and I've checked the documentation of rails_admin, and looks like there's no solution to this problem.

Can anyone help me?

And I think the problem is around augur = Augur.find(:augurid), because if I change the :augurid to 5, it will display the right augur name. But how do I do that for every record in schedule?

Was it helpful?

Solution 2

As @JKen13579 mentioned, :object_label_method :name can do it for you. But if you really want to access the "current object" in your rails_admin callbacks, there is a way:

list do
    field :augur do
        pretty_value do
            bindings[:object].name
        end
    end
end

This will show the name of the augur and only affect the list view.

OTHER TIPS

You should just be able to do add object_label_method :name to the rails_admin configuration for the Augur model:

config.model "Augur" do
  object_label_method :name

  ...

end

Is that what you're looking for?

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