I'm looking to generate a list of all users signed up as well as a created date and an IP address.
The most important feature is the ability to delete users from this list.
Active_admin seems to be the best choice for this task, but I have found very few results for this scenario. I find it hard to believe because I would assume that this is a common feature.
Any suggestions or links to tutorials?
Thanks in advance!
EDIT:
Ok, so I went and created a user resource through active admin. I.e /app/admin/user.rb
I then went and took some of active_admins demo code and trimmed it down for testing.
ActiveAdmin.register User, :as => "User" do
filter :username
filter :email
filter :created_at
index do
id_column
column :username
column :email
column :created_at
default_actions
end
end
But I get the error:
NoMethodError in Admin::UsersController#index
undefined method `per' for []:ActiveRecord::Relation
Any ideas?
EDIT 2: I just tried:
ActiveAdmin.register User do
def index
#users = User.find(:all)
end
end
and I get the same exact error.
EDIT3:
Found the issue. After some digging, will_paginate gem was causing the conflict!
Everything works now!
With Active Admin, you can display data from every model you have. When for example you have a model user (your front-end users), you just create /app/admin/users.rb and put your stuff in there. Finished.
You can have a look at the code of activeadmin.info on github.
Related
I am using ActiveAdmin and have an Account model and a User model. Each account will have a has_many relationship with users, and I'd like to be able to list the user names on the account table.
I've seen quite a few SO questions that address the same question and this is how far I've managed to get:
index do
column :id
column :account_status
column :users do |account|
account.users.each do |user|
auto_link user
end
end
column :address do |account|
account.address
end
column :created_at
column :updated_at
actions
end
Note: i've also tried replacing "auto_link user" with "user.name"
This kind of works. It does get the data, but instead of showing attribute values I seem to just get a memory address reference:
I should say here that I am quite new to both Ruby and Rails, so it is likely that I am just missing the point. Considering what I have, is there something I've missed out?
account.users.each will return an array of User objects. That's why you got something like "a memory address reference". To return a list of user.name, you should do something like this:
column :users do |account|
account.users.pluck(:name).join(', ')
end
I use join here to convert an array of names to a string. I'm not sure without join, what will be displayed. You can try it by yourself :)
Last time I got the problem with active_admin. In tables where I have 5000+ rows of data it's work very slowly. How I can optimize it? Maybe somebody know some async load plugins for this module?
There are a couple things you can do.
By default, Active Admin loads associations as drop-down filters on the index page. If those filters aren't being used, it helps to remove them because they instantiate every record of that model to build the drop-down.
ActiveAdmin.register Post do
remove_filter :categories
end
If your index page has columns that depend on associated records, it helps to eager-load them.
ActiveAdmin.register Post do
controller do
def scoped_collection
super.includes :author, :publisher
end
end
end
This doesn't really apply since you only have 5000 records, but if you get to the point where even a DB COUNT of the table takes a long time, you might want to disable the count in the bottom right of the index page. (this feature was added in 0.6.1)
ActiveAdmin.register Post do
index pagination_total: false
end
Two questions:
1) How can I make a column in the 'list' for a model consist of data from the record's association? In other words, I have a user model and a user has_many posts. I want to simply have a "post count" column in the list. I tried doing:
field :posts do
formatted_value do
value.count
end
end
but that results in a divide by zero error. I even tried doing:
field :posts do
formatted_value do
bindings[:object].posts.count
end
end
but got the same results.
2) How can I filter the listing to a particular scope? For example, I want to make the users post count be a link that is clickable which will show all posts for the given user.
The best I could figure out how to do this was to do:
# note that I created a method post_count to temporarily solve problem #1
field :post_count do
formatted_value do
bindings[:view].link_to value, "/admin/posts?query=#{bindings[:object].id}"
end
end
Which doesn't work very well. Is there a way to instruct rails-admin to do a .where(:user_id => xxx) on the model?
The other thing I wasn't crazy about was having to manually put in 'admin/posts'.. I was trying to see if I could do rails_admin_list_path(:model_name => "posts"). but that didn't seem to work.
You'd probably get a better response on the rails_admin mailing list - http://groups.google.com/group/rails_admin/
For your first question, this should do the trick:
field :posts, :virtual do
formatted_value do
bindings[:object].posts.count
end
end
For your second question, rails_admin now has a filter system - see the "add filter" dropdown at http://demo.railsadmin.org/admin/players . Tapping into that would be a much better method.
rails_admin_list_path(:model_name => "posts") should work, you might have to include Rails.application.routes.url_helpers or similar.
Try adding this to your rails_admin.rb
RailsAdmin.config {|c| c.label_methods << :field_name}
worked for me
Hey everybody I have a scenario here I am really trying to figure out. I am trying to build a custom blog in Rails which requires users to be signed in to leave a comment. Basically I have 3 models - Post, User, and Comment. My problem is mainly with the comment model.
I am trying to have it so that a Comment belongs_to a User and also belongs_to Post, which have many comments. I also have made the post_id and the user_id inaccessible as I do not want them to be tampered with (I want the comment to be automatically associated with the id of the post on which it was left, and I want the user to be automatically determined via session). My problem is that I am not really sure how I can create a valid comment. If I try to add a comment doing #blog.comments.build(#attr) I am ignoring the inacessible User, and of course if I try to build it through the User I am ignoring the blog.
My guess is there is a much better way to do this and I might just be approaching it wrong. Any ideas?
I assume you have this in the Comment model:
attr_accessible :content
and when you try to build a comment this happens:
#post = Post.first
#post.comments.build(:user=>current_user)
# => WARNING: Can't mass-assign protected attributes: user
So that won't work.
If you want to protect the user_id and post_id from being overwritten on an update, you could do this:
attr_accessible :content, :user_id, :post_id
attr_readonly :user_id, :post_id
#post = Post.first
#post.comments.build(:user=>current_user)
#post.save!
# sets user_id when creating
#post.user_id = 37
#post.save!
# saves, but user_id is not changed. No warning is logged.
#post.update_attributes(:user_id=>37)
# same as above
#post.update_attribute(:user_id,37)
# raises ActiveRecord::ActiveRecordError: user_id is marked as readonly
But this seems like overkill, since presumably your application would not submit a form with a user_id for an existing comment, and someone would have to code up their own form and post it to change the ID.
You could always just set the relation manually:
comment = #post.build(#attr)
comment.user = #user
comment.save
At least, I'm pretty sure that would work.
I am using acts_as_commentable and am curious if anyone has any good ideas on how to allow for anonymous and registered users to post comments? Meaning, if a registered user is authenticated, I want the comment to be marked with their name, etc. But I also want an anonymous user to be able to comment and have a name and email address recorded. I am using Devise for authentication.
I have an idea on how to make this work but it feels a little hacky to me. Wondering if anyone has any thoughts.
I don't know your plugin, but if you use this one (https://github.com/jackdempsey/acts_as_commentable), it seems very basic...
The Comment model has a relation to a user which is not mandatory.
So in your new comment form, I would just add two text_field_tags if the user is not logged (text_field_tag :first_name, text_field_tag :last_name).
And I'd just write the create action for comments like this :
def create
#comment = Comment.new(:commentable => #your_object, :user => current_user, :first_name => params[:first_name], :last_name => params[:last_name])
...
end
if the user is not logged, current_user will be nil and that won't cause any problem.
You can write an helper method to display the name for a comment depending it has a user or not like this...
# Displays the user's login if any or else the first name and last name
def displayed_name(comment)
comment.user ? comment.user.login : "#{comment.first_name} #{comment.last_name}"
end