Default selected value in select field generated via association

616 views
Skip to first unread message

gan...@esignature.com.np

unread,
Jul 31, 2013, 7:17:22 AM7/31/13
to rails...@googlegroups.com
Hi all,
I have a select field generated via association in the rails_admin view . However when trying to create new record . I want that particular select box to be populated with some default selection instead of first coming up blank. I tried searching for it in the documentation and in goggle mailing list but haven't been able to find any solution or any hint for past two days. Any help or hints would be highly appreciated.

With regards,
Ganesh Kunwar.

Juan Felipe Rincon

unread,
Aug 1, 2013, 5:40:30 PM8/1/13
to rails...@googlegroups.com
Try this if you want it global:

config.model Player do
  configure :sport, :enum do

    enum do
      ["Football","Lacrosse","Rugby"]
    end
  end
end

And this if you want different values for edit/create

config.model Player do
  create do
    field :name

    field :sport, :enum do
      enum do
        ["Football","Lacrosse","Rugby"]
      end 
    end
  end

  edit do
    field :name

    field :sport, :enum do
      enum do
        ["Football","Lacrosse","Rugby","Cricket","Hockey","Baseball"]
      end
    end
  end
end

The return value of the enum block can also be a hash with label: 'value' pairs

- jfr

Ganesh Kuwar

unread,
Aug 2, 2013, 12:38:24 AM8/2/13
to rails...@googlegroups.com
Thanks for your reply, Actually my problem is: 
I have two models associated with has_many relation. Like this:

class Students
  field :name
  has_many :games
end

class Game
  field :name
  belongs_to :student
end

Now in my Student edit form of rails_admin I want to include Game as select drop-down. Everything is going well, but I want to display one default game on select field. How is it possible?
Ganesh Kunwar

Ganesh Kuwar

unread,
Aug 2, 2013, 12:42:12 AM8/2/13
to rails...@googlegroups.com
Thanks, Juan But i already have a values being populated from the association. What i want is when i load the new/edit page i want on especific value selected in the dropdown. (i.e Lets say "FootBall") always needs to be the default selected value in the dropdown.


On Friday, August 2, 2013 3:25:30 AM UTC+5:45, Juan Felipe Rincon wrote:

Mark Silverberg

unread,
Oct 21, 2013, 10:54:34 AM10/21/13
to rails...@googlegroups.com
This is exactly what I'm looking for as well. Any luck?
Reply all
Reply to author
Forward
0 new messages