ActiveAdmin - failed adding custom page page under a resource - ruby-on-rails

I'm trying to add a custom page under one of my resources (ActiveRecord) - on the ActiveAdmin site this was listed under the 'belongs_to' section
Something like this:
# the model
ActiveAdmin.register Desk do
...
end
# the custom page
ActiveAdmin.register_page "Chair" do
belongs_to :desk
content do
para "Hello World"
end
end
in my rack routes a new route is added:
/activeadmin/desks/:id/chair
but when I try to put the route on the browser or refer to it from the code i get the following:
undefined method `activeadmin_desks_chair_path' for >ActiveAdmin::Helpers::Routes:Module
Did you mean? activeadmin_desk_chair_path
activeadmin_desks_path
activeadmin_desk_chair_url
activeadmin_desk_path
Any suggestions?

Related

Custom model editing for Rails

Suppose I have a Book model in Rails, and this model as a resources has predefined routes e.g. /books/:id/edit.
Now, in my app I wish to show a list of books and add an edit button where everyone can edit the book but only one specific field. I wish to achieve this by rendering a different view, using a different route and a different controller.
How should I define the route so that this works, i.e. the id of the Book instance is passed as needed?
Sounds like you can keep
rails g model book field_one field_two
rails g controller books
# routes.rb
resources :books
patch 'common_update/:id', to: 'books#common_update'
if you specifically want to hit another controller then you can just create a different one (rails g contoller_name) and replace books in the routes with contoller_name (note that the generator takes a plural!).
# books_controller.rb
def common_update
#book = Book.find(params[:id])
if #book.update(common_params)
# do something
else
# do something else
end
end
def common_params
params.require(:book).permit(:field_one) # but not field_two !
end
As for views, you can perhaps conditionally render the form for the owner (the user who created the book) and the form for everyone else. If you want a separate view/show you can also just add the route (get 'common_show', to: 'controller_name#common_show') plus a view file in the correct folder :+1:

How to get the path for a collection in ActiveAdmin

I made custom index view for ActiveAdmin, and I would need to get the admin path for the given collection.
Here is what I have:
module ActiveAdmin
module Views
class IndexAsSpecial < ::ActiveAdmin::Component
def build(page_presenter, collection)
//...
// should be something like /admin/posts
path_to_collection = ???
//...
end
def self.index_name
"special"
end
end
I have searched and tried, but all I found is paths for a resource, not for the collection (e.g. resource.route_collection_path )
I also found this, but here I am still missing the right parameters, or maybe the class name of the active admin collection:
Rails.application.routes.url_helpers.polymorphic_path([:admin,:posts])
Just that I need to replace :posts somehow dynamically.
Any ideas?
I must say, that the documentation of ActiveAdmin is very vague here, but after debugging a bit more I found the answer to my question:
helpers.collection_path # will generate /admin/posts
or if you need a member action on the collection:
helpers.resource_path(:action_name) # will generate /admin/posts/action_name

ActiveAdmin - blank slate - customized message - Rails

How can i change the blank_slate message on ActiveAdmin. Each of my models would have a different blank_slate message.
Example:
Transport: No transports, do this and this
Car: Did you forget, no cars until this
You can monkey patch ActiveAdmin to load the message as desired:
require 'active_admin/helpers/collection'
module ActiveAdmin
module Views
module Pages
class Index < Base
protected
def render_blank_slate
# for example only, you can define your own I18n structure
# You can use active_admin_config.resource_label too if not mistaken
blank_slate_content = I18n.t("active_admin.blank_slate.content.#{active_admin_config.plural_resource_label}")
insert_tag(view_factory.blank_slate, blank_slate_content)
end
end
end
end
end
load this file to the initializer folder and put the messages in your language YAML file following the structure as defined in the blank_slate_content assignment line.
You can use (poorly documented but not so monkey-path) PagePresenter.blank_slate_link option responsible for rendering a link to new resource on an empty index page (see GitHub source). You may override it in your ActiveAdmin's resource index page using blank_slate_link keyword in the header of the index page definition. E.g. (app/admin/transport.rb):
ActiveAdmin.register Transport do
# Some cool initialization: scope, filters, ...
index blank_slate_link:
-> { "No transports, "+
link_to("do this and this", new_admin_transport_path) }
do
# ...other index page details go here...
end
# ...rest of pages: show, edit, ...
end
This way you get as sophisticated link as you want. And the I18n's active_admin.blank_slate.link is no longer used for generating link to a new resource.
But as you see render_blank_slate still uses the hard-coded active_admin.blank_slate.content string to render text right before the link (e.g., "There are no %{resource_name} yet."). If you want to get rid of this text you should override it in your YAML localization file. You may simply leave it empty (app/config/locales/en.yml):
en:
active_admin:
blank_slate:
content: ""
Or, in your case I'd suggest:
en:
active_admin:
blank_slate:
content: "%{resource_name}: "
which gives you the desired "Transport: No transports, do this and this" message.

add view with user:id

I'm trying to add a view to the Users/views, which will incorporate the users-id. The view I'm looking to add is 'test' and I'd like each User to have their own test page. For example, the path for User edit is as follows: http://localhost:3000/users/1/edit. I have no problem creating a test page, but can't get it to associate with user:id. I'd like the User test path to be as follows: http://localhost:3000/users/1/test. Unsure how to set this up.
If you want a route called http://localhost:3000/users/1/test, you should create a member on your resources users.
So in your routes you'll have to add something like this:
resources :users do
get :test, on: :member
end
(For reference, also see the rails guides: http://guides.rubyonrails.org/routing.html)
Note: with the on: :member option. You get the resource id in the params[:id]. If you don't specify it like this, you'll find it the params[:user_id].
When you've done that, you should add the action to your controller and create the corresponding view.
class UsersController < ApplicationController
def test
# create a corresponding view app/views/users/test.html.erb
end
end

activeadmin, remove empty message

In ActiveAdmin, when there are no items for the model (in my example User), it shows a default 'There are no Users yet. Create one'.
How can I remove this message?
Is there the possibility of customizinig it on per-page basis, that is having a particular message for a particular ActiveAdmin page?
This is a MonkeyPatch:
Create a new file in lib folder and copy:
module ActiveAdmin
module Views
# Build a Blank Slate
class BlankSlate < ActiveAdmin::Component
builder_method :blank_slate
def default_class_name
'blank_slate_container'
end
def build(content)
super(span(content.html_safe, class: "blank_slate"))
end
end
end
end
Customize the content variable in build method to change the default message.
For now you cannot do it by ActiveAdmin settings. Look issues in
repository.
You can render any html or erb.html in your resources
You may use poorly documented (but not so monkey-path) blank_slate_link index page option. It lets you override this message in any of your ActiveAdmin's resource index page.
See https://stackoverflow.com/a/72529909/1744707 for details.

Resources