-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rails 4.1 enum support #1993
Comments
Hope this gist helps someone - just put in in config/initializers and RailsAdmin will recognize ActiveRecord enums https://gist.github.com/dmilisic/38fcd407044ace7514df |
This just got us out of a bind with Rails Admin and ActiveRecord enums. Thank you so much for the gist! |
I put rails_admin_active_record_enum.rb file in the config/initializers but it's not creating the necessary method handlers. Any help? I am using Rails 4.1.1 with Ruby 2.1.2. |
Did you restart your server? You need to restart in order for the file to be picked up by Rails Admin |
Also, for context on what my model looks like:
Did not need to any any special configuration to the role field in my rails_admin config file once I had the gist linked to above saved in |
Yes, I did restart the server. The part that's not working is this: It's not actually extending the built-in Rails |
Worked for me |
The gist works for display, but when I edit and save, I get an |
@jegodwin @TimSullivan To fix you problem you need to install gem 'rails_admin', '0.6.2', git: 'git://github.com/sferik/rails_admin.git', branch: 'master', ref: '02ba1dab32d' This fixes the error |
Thanks for the gist @dmilisic |
@kenchung Same problem here. Display is fine but creating/saving is not. (edit: Rails 4.1.5 + RailsAdmin 0.6.3, saving triggers |
extend Enumerize
enumerize :ssl_tls, in: [ [ 'not_used', '0' ], [ 'ssl', '1' ], [ 'tls', '2' ] ], default: 'not_used' This works |
Rails 4.1.5 + RailsAdmin 0.6.3
|
@dmilisic Thanks! Your gist helps for me. It's working. |
Hi @dmilisic, ActiveRecord::StatementInvalid in RailsAdmin::Main#index .rvm/gems/ruby-2.1.1/gems/rails_admin-0.6.2/app/views/rails_admin/main/index.html.haml where line #128 raised: PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "user" Extracted source (around line #128): %th.other.right.shrink= "..."
any pointers? |
@mulaiko I just came across the same issue. Seems that rails_admin tries to search enum fields like strings. The workaround could be setting searchable option to
|
@jegodwin sorry for the delay... Are you using rails 4.1? |
If you have custom config for model with enum in rails_admin initializer, rename it to z_rails_admin or whatever to load rails_admin_active_record_enum.rb first. It solve "1 is not a valid" staff. |
Well guys, don't know if anyone here still having issues with Rails Admin and Rails 4 Enum, but anyway here's what I've done to work it out without the need of adding field :role, :enum do
searchable false
enum do
User.roles.map { |k,_| [k.titleize, k] }
end
pretty_value do
bindings[:object].send(:role).titleize
end
def form_value
bindings[:object].role
end
end It partially works, I couldn't make enum fields searchable though, it triggers a invalid SQL statement as mentioned above by @mulaiko. Anyway... has anyone come up with a workaround for this? |
It's not a workaround, but just something to prevent the error since Rails Admin can't filter enum fields:
It will prevent Rails Admin from filtering on that field. |
Thanks for the Gist, worked for me as well!!!! |
Is there a fix for enum filtering? I use version |
I wonder if this will be implemented in Rails admin anytime soon |
To add localization (default attribute path translation) support, change getter method definition to define_method("#{ name }_enum") {
self.class.send(name.to_s.pluralize).to_a.map do |k, v|
translated = I18n.t(k, scope: [:activerecord, :attributes, self.class.to_s.downcase, name.to_s.pluralize])
processed_key = translated.include?('translation missing') ? k : translated
[processed_key, v]
end
} |
searching still seems broken even with the monkey patch PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "hunter" LINE 1: ...".* FROM "orders" WHERE ((orders.shift_window IN ('hunter'))... ^ : SELECT "orders".* FROM "orders" WHERE ((orders.shift_window IN ('hunter')) OR .... shift_window is an enum |
@kenchung @fstephany did you get any luck? Same problem here. |
no :( |
@dgobaud you could disble search for that filed (in config/initializers/rails_admin.rb):
|
@dmilisic thanks that is what I'm currently doing but I would like search to work ideally |
Done 🌟 |
awesome thanks! |
this is still happening in 0.7.0 |
never mind it was the old monkey patch causing the problem. removed it and now search seems to work |
To get selector's item, Typus uses plualized name's method. https://github.com/typus/typus/wiki/customization-selectors But, when Rails4.1 released. Method name confilected ActiveRecord::Enum's method name. so, I research other admin framework gems, and I think best way to implement enum selector. 1. rails_admin rails_admin select new method name is like "{column name}_enum" railsadminteam/rails_admin#1993 2. active admin active admin select add to parameter in "collection" option. http://stackoverflow.com/questions/23414880/how-to-properly-configure-rails-4-1-enums-in-activeadmin and, ActiveRecord::Enum have't I18n. so liker a below gems were released to support this function. * https://github.com/brainspec/enumerize * https://github.com/zmbacker/enum_help so, We should avoid ActieRecord::Enum i18n gem's implement and use convenient. I'll follow rails_admin's way.
This is still happening in 1.1.1. Any fix? |
+1 |
@onlyyouandty I also did experience the error in 1.1.1 but solve it by changing the data type of the enum from integer to string. Hope that helps |
@dotdidi Enum can only be integer values. If you change it to string, the values won't get stored when you try to fill the form because enum calls in integer like [0, 1, 2]. Have you tried it? |
@onlyyouandty not necesarilty in rails admin class ReunionRecord
def graduating_year_enum
(1950...Date.today.year)
end
def class_enum
['Regular', 'International', 'Express']
rails_admin do
list do
field :name do
filterable true
end
field :graduating_year do
filterable true
end
field :class do
filterable true
end I put the graduating_year and class as :string and the enum works without any errors |
You are actually right, based on rails/rails#12747, enum could be string like you said. I will have to figure out why it is not saving as string type in my own case. |
It still doesn't work. Having class A < ApplicationRecord
enum status: %w(a b c)
rails_admin do
list do
field :status, :enum do
enum {%w(a b c)}
filterable true
end
end
end
def status_enum
%w(a b c)
end
end (substitute
leads to such a query SELECT "as".* FROM "as" WHERE ((as.status IN ('c'))) ORDER BY as.id desc LIMIT $3 OFFSET $4 [["LIMIT", 60], ["OFFSET", 0]] that fails because Interestingly, enums work as expected in the edit section with no tweaking: select boxes with word keys and numeric values even though they're treated as integer fields. |
I'd say filtering on an enum is one of the strongest things you can do. Disabling filtering on an enum or "status" field is the worst thing I can do in my interface. Is there a known workaround by now for having both edit support and filter support for enums? |
My godawful hacky solution for v0.8.1: rails_admin.rb
|
Rails 4.1 introduced enum attributes which don't behave well with RailsAdmin.
RailsAdmin (v 0.6.2) threats enum attributes as integers by default. This can be "fixed" by adding method to the model, ie.
There is another issue - creating or updating record raises:
Obviously the controller got
'1'
as a string value which doesn't match any of the states ('active'
,'archived'
), nor state as integer (0
,1
).The quick fix might look like:
It works, but isn't nice. Especially when you have multiple enum attributes.
Looking forward to support for
ActiveRecord::Enum
out of the box.The text was updated successfully, but these errors were encountered: