I am using active-admin(AA, 1.0.0) in a rails 4.2 app. I am displaying a list of online users. I have to display a list of users with scopes for each type of user and filters as per the User model fields. Also, I need to display a list of online users on another page.
User(dropdown)
=> Accounts
=> Online Users
To display the users list, i have
ActiveAdmin.register User, as: 'User' do
menu :parent => "Users", :label => "Accounts"
...
end
To display the online users list, i have
ActiveAdmin.register User, as: 'Online User' do
menu parent: "Users", label: "Online Users", url: '/admin/online_users'
actions :index
filter :id
filter :name
filter :email
...
end
Now,the filters are getting displayed properly on the "Online Users" page, but when the filter form is submitted, its submitted to "/admin/users" rather than to "/admin/online_users". Can I pass the url to the filter form? If not, what is the right solution to this problem.
Thanks.
You can redefine the collection_path method which is used by the filter form builder like this:
controller do
def collection_path
admin_online_users_path
end
end
Related
I have a model that is called "controller". I checked the rails reserved terms list and it does not show "controller" as a word not to be used.
The model works fine but when I use the model in a view, the view does not submit the controller attribute back to the controller.
Any idea if "controller" is in fact a reserved word?
Yes controller is reserved in RoR. Even you can use it like this in your views:
#to get controller name:
<%= controller.controller_name %>
#to get action name, it is the method:
<%= controller.action_name %>
I'm ussing activeadmin (1.0.0.pre2).
In my model, I have fields, filled with tags. I translate this tags in the app using the method "translate_name" in form field values (like this .. i18n for select boxes)
In activeadmin I'm using custom form to choose only some fields.
This is my activeadmin code...
index do
column :user
column :user_surname
column :service
column :patient_type
column :description
actions
end
form do |f|
if f.object.errors.size >= 1
f.inputs "Errors" do
f.object.errors.full_messages.join('|')
end
end
f.semantic_errors # shows errors on :base
f.inputs :user
f.inputs :service
f.inputs :patient_type
f.inputs :postal_code
f.inputs :availability
f.inputs :date
f.inputs :estimated_period
f.inputs :description
f.actions # adds the 'Submit' and 'Cancel' buttons
end
service y patient_type are nested resources exactly like this ActiveAdmin customizing the form for belongs_to
activeadmin don't traslate this tags.
Any idea to solve this?
Thanks.
In Formtastic you can define a to_label method on your models which will be
used when rendering the select field options. Also, ActiveAdmin will call a
variety of methods when searching for a name for your object, like
display_name. See Index as a Table docs.
Adding to_label and display_name methods to your Service class should
properly translate the values highlighted above:
class Service < ActiveRecord::Base
def translated_name
I18n.t(name)
end
# method used when creating Formtastic select options
def to_label
translated_name
end
# method used for displaying model names in ActiveAdmin
def display_name
translated_name
end
end
ActiveAdmin checks objects for the following methods to show a name. Most often
the name method is used.
display_name
full_name
name
username
login
title
email
to_s
Ok, I searched all over the web and found no answer.
I am looking for a way to display a name of a 'category' in the show view of a post (I have to mention I'm rookie in Rails).
I have....
a model "Post"
class Post < ActiveRecord::Base
has_many :categories
belongs_to :user
end
a model "Category"
class Category < ActiveRecord::Base
belongs_to :post
end
and the "show" view of the "post" has a line like this
<%= #post.category.name %>
The error message as screen shot:
NoMethodError in Posts#Show - undefined method `category' for #
The "show" action in "Posts" controller:
def show
#post = Post.find(params[:id])
end
I'm building this app along a little outdated training video on udemy. In this video there's in the category model a line with "attr_accessible"
class Category < ActiveRecord::Base
attr_accessible :name <---------------------- this
has_many :posts
end
...but this does no longer exists since Rails 4.0. Is there another way to retrieve the category name of the post?
Thank you in advance :-)
I got the answer. I found out, that every way, to get the data out of the table categories in the view for products won't work. I than thougt, showing the category in the category show view is simple working. With this idea in mind I took the same code, this one:
app/views/categories/show.html.erb
<p>
<strong>Name:</strong>
<%= #category.category_name %> <--------this line
</p>
...from the category show view and put it into the post show view. I than got again an error but different:
--> NoMethodError in Posts#show
Ok, this says the instance variable "#category" isn't available for the post show view. To change that was easy. I copied the object from the categories controller's show action into the posts controller's the show action. Like this:
class PostsController < ApllicationController
.
.
.
def show
#post = Post.find(params[:id])
#category = Category.find(params[:id])
end
And: it works!!!!
But now, is there something wrong to do it like this?
Cheers.
The method category not exists because the Post model has many "categories" not one "category". The Post should have the method "categories". Then if you want to show a first "category" of post in the view:
<%= #post.categories.first.name %>
If you want to show all "categories" of post, then you iterate the collection:
<% #post.categories.each do |category| %>
<%= category.name %>
<% end %>
I tried again and found the real error in my code. the foreign key was not set by Rails. I had to do manually. The table which has a "belongs_to" (in my case the posts table) needs a foreign key added (like category_id).
First creating the migration file:
rails g migration add_foreign_key_to_posts_table
Second adding the migration code to the migration file:
class AddForeignKeyToPostsTable < ActiveRecord::Migration
def change
add_foreign_key :posts, :categories
end
end
Third raking the db migration with:
rake db:migrate
Fourth adding the foreign key for other resources by following steps one to three. Now everything works fine.
Is there a way to tell whether I am in create or edit mode from inside the permit_params block of active admin? I'd like to merge created_by and updated_by when I'd creating a new record. When I'm editing an existing record I only want to merge updated_by.
Here's my code:
ActiveAdmin.register Group do
menu parent: "Groups", priority: 0
permit_params do
permitted = [:name, :description, :group_type_id, :owner_user_id]
permitted.merge!(updated_by: current_admin_user)
permitted
end
I'd like to say something like:
permitted.merge!(created_by: current_admin_user) if new_record?
new_record? is an active admin method but it only seems to work in forms.
Any ideas?
You can merge it in the create and update action, too:
controller do
def create
params[:group].merge!(created_by: current_admin_user)
create!
end
def update
params[:group].merge!(updated_by: current_admin_user)
update!
end
end
Using Rails 4.0, ActiveAdmin, Rolify and CanCan, Adding and removing user roles via the activeadmin panel does not save (commit to database).
The my ActiveAdmin User and User Model look okay as I can list all the roles that apply to a user using check_boxes. Although when adding any roles or removing any roles via the checkboxes the changes do not get applied.
I get a notification that the user was updated successfully but as I look through the database or render the page, the roles have not been updated.
How can I get the roles to update when the form is saved?
Edit:
Using Devise also.
The solution here is to allow the ActiveAdmin controller to update the role ids related to the user.
ActiveAdmin.register User do
permit_params :email, :password, :password_confirmation, role_ids: []
Here's a form that shows a check box for each global role.
form do |f|
f.inputs "User Details" do
f.input :email
f.input :password
f.input :password_confirmation
f.input :roles, as: :check_boxes
end
f.actions
end
While we're at it, we might as well make it possible to update the user without entering their password:
# Allow form to be submitted without a password
controller do
def update
if params[:user][:password].blank?
params[:user].delete "password"
params[:user].delete "password_confirmation"
end
super
end
end
Put all of this in the app/admin/user.rb.
I just ran into this issue. The problem was with validation of presence (which I think is occurring in the database ":null => :false"). In order for the update to save, I had to fill out all fields (including password/password confirmation).
I had to add some controller code to make this work, I hope this helps:
Remember to permit the attributes you're accepting in active_admin as such (this will create the permitted_params method I'll be referring to in a bit.
Note that since we're not saving role_ids directly to the user, it doesn't even have to be a permitted param.
permit_params :email, :password, :etc
Create a private method we could call from the create and update controllers. This method will just iterate over non-empty ids, find the associated roles, and add them to user.roles.
The create and update methods simply find call the add_roles method prior to continuing execution. ActiveAdmin makes the the existing user available through resource, but not through create. We have to find it ourselves in create!
controller do
def create
#user = User.new(permitted_params[:user])
add_roles(#user)
create!
end
def update
add_roles(resource)
update!
end
private
def add_roles(resource)
resource.roles = []
params[:user][:role_ids].each { |r| resource.roles.push(Role.find(r)) unless r.blank? }
end
end
In the activeadmin form, you can add:
form do |f|
# other inputs
f.input :roles, as: :select, multiple: true, collection: Role.all
end
And in the show page, you can display the roles as such:
show do
attributes_table do
# other rows
row :roles do |r|
r.roles.map { |role| role.name }.join(", ")
end
end
Hope this helps!