Im building a membership web application with Ruby on Rails. I set up my DB and relationships between tables, everything worked fine. I decided to use Active Admin to shortcut the creation of a dashboard as well as providing a good search feature.
The issue I'm currently having is in Active admin on my dropdown list and table the foreign keys are only showing Id numbers and reference addresses (sorry, I forgot what their actually called).
Pics of my issue:
How can go about fixing this and in which files.
I would greatly Appreciate any help.
Customize ActiveAdmin Index Filters > Change The String Representation
Above article explains the similar issue. you have to do something like this in admin page for membership.
filter :plan, collection: -> {
Plan.all.map { |plan| [plan.name, plan.id] }
}
I have used name attribute for plan as an example. you may have different attribute, use correct attribute to reflect the details.
Update
Based on #DevOps92 answer
Alternate way to achieve this is to set any the method :display_name, :full_name, :name, :title, :to_s in Plan model
Active Admin Doc
For association columns we make an educated guess on what to display by calling the following methods in the following order:
:display_name, :full_name, :name, :username, :login, :title, :email, :to_s
This can be customized in config/initializers/active_admin.rb.
I solved the issue by going to my models that was generated by rails when I created the table in my database via scaffold. App/models/plan.rd and used the method display_name. Within the display_name method I listed the column name from the plan table. so anyone facing a similar issue check your rails models.
My Solution
Related
For one of my models in ActiveAdmin, it is changing the URLs to use the name instead of ID.
For example: http://localhost:3000/admin/product/PH instead of http://localhost:3000/admin/product/1
I don't understand why it's doing that since all of the other models are working correctly (using ID).
This model has no models/product.rb file.
# app/admin/product.rb
ActiveAdmin.register Product do
permit_params :name,
:amount,
:description
end
I checked the documentation and didn't see anything that looks like it would make this happen.
Also, all of the other SO posts I've seen related to name and URL seem to be trying to do the opposite - changing the default route to use name (instead of ID).
Late reply, but I just met the same problem.
It was due to slug. I just remove slug from my model and it worked.
Hope it could help someone.
Some had posted similar question as mine. I tried the solution to most of them. Some say ActiveAdmin is using Metasearch and some say it's using ransack. I tried both methods.
My activeadmin version is 1.0.0.pre4 and rails 4.2.6
Follwing is my code for the user based filter:
ActiveAdmin.register Equipment do
filter :user
end
This gives me a user dropdown where I can select one of the user and it filters the result based on that user. Right now I have only 3 users so its fine. But in future it will grow to thousands. So instead I would like to have a contains field, where I can enter user name and filter.
I tried changing to following:
filter :user, as: :string, label: 'User'
and also
filter :items_user, as: :string
Didn't work. Can somebody please tell me what am I missing?
Try this,
filter :user_name, as: :string
where name is attribute of User Model.
Normally you would use something like attr_accessible :name, :email to allow for mass assignment for those specific fields but how do you do this in mongoid for an embedded document like :nick_names? I tried tagging it at the end of attr_accesible but it does not work.
Thanks!
I would use a combination of criteria and update all. For instance:
Foo.where(country: nil).update_all(country: "USA")
http://mongoid.org/docs/querying/modification.html
If you're trying to do it via a Rails update, then #apneadiving is correct in that you need to enable accept_nested_attributes_for in the parent model.
I'm currently using the mass assignment security baked into rails 3 to scope what level of users can update about their model. For example this code allows me to protect attributes based on the user level.
class Customer
attr_accessor :name, :credit_rating
attr_accessible :name
attr_accessible :name, :credit_rating, :as => :admin
end
I would like to be able to use this same idea for which attributes appear when I do a find. For example I would like to be able to say
Customer.all.as(:admin)
and get back the credit rating. Compare this to doing
Customer.all
and getting back all the attributes except the credit_rating
Is this something rails supports and I've missed?
attr_accessible is used to filter incoming attributes on mass assignment. This is a convenience method created so that a developer does not need to manually clean the incoming hash of params, something he does not control.
When displaying information a developer is in full control of what he/she desires to show, so there seems to be no reason to limit the read functionality.
However, rails allows you to "select" the attributes you desire in a query: see http://guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields
You could easily create a scope with the name admin that would limit the selected values.
If you do not desire to have the full models, but only the values, you could use the generated sql. e:g.
ActiveRecord::Base.connection.select_values(Customer.select('name').to_sql)
I'm a bit of a newbie at Rails and feel I'm missing a trick here. I'm trying to add a phone_number field to my Devise-generated User model, but I'm having an issue with saving it. I've done the rails generate devise:views, updated the edit.html.erb file to add in the :phone_number field, and created a migration to add the phone_number field to the model. It's not saving to the model because (as I understand it) I can't update the controller to include the new fields.
Do I need to create an app/controllers/users/registration_controller.rb defined with class Users::RegistrationsController < Devise::RegistrationsController and then monkey patch the update method? Or is there a more straightforward/elegant/easier way?
I realize there are a couple other questions related to this on the site, but one offers no useful answers, and the other simply details what I mention here. Is there anything more to it?
Thanks.
After you add the field to your database through a migration you will also need to add it to your list of accessible attributes in your User model. Your attr_accessible list should look something like the following depending on what devise modules you are using.
attr_accessible :email, :password, :password_confirmation, :phone_number