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.
Related
I always get routing errors in this Ruby file on Rails to project. It shows me routing error as you can see in below screenshot.
All gems are installed properly it shows me the home page but do not show me the check file or show file page.
Here is my Routes.rb code:
Rails.application.routes.draw do
get 'home/submission3'
post 'home/checkFile'
get 'home/showFiles'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
Here is an image of the error shown:
Here is my controller code:
# C:\Users\attari\RubymineProjects\submission3\app\controllers\home_controller.rb
require 'docx'
require 'find'
class HomeController < ApplicationController
def submission3
end
def checkFile
word = params[:folder][:word]
path = params[:folder][:path]
#files = checkWord(word, path)
puts "FILES ARE #{#files}"
redirect_to home_showFiles_path(#files)
# puts "WORD IS #{word} PATH IS #{path}"
end
def save
#submissions = submission3.new
end
def checkWord(userInput, folderInput)
count = 0
if (Dir.exist?(folderInput))
docx_file_paths = []
filtered_files = []
Find.find(folderInput) do |path|
docx_file_paths << path if path =~ /.*\.docx$/
end
(0..docx_file_paths.length - 1).each { |i|
d = Docx::Document.open(docx_file_paths[i])
d.each_paragraph do |p|
string_ = p.to_s()
if (string_.include? userInput)
puts docx_file_paths[i]
filtered_files.append(docx_file_paths[i])
count = count + 1
puts count
else
puts "No word Matched"
end
end
puts count
}
return filtered_files
else
puts "No Directory exist"
end
end
def create
#submission3 = submission3.new(params[:word])
if #submission3.save
redirect_to new_submission_path
end
end
private
def book_params
params.require(:book).permit(:title)
end
end
How can I fix this routes.rb error?
If you look at the error message, you can see that the filename is being appended directly to the route name (/home/showFiles.C:%5CUsers...). You need to add an id to the route so that Rails knows to expect something there. Your route should look something like this:
get 'home/show_files/:file_name'
[Note that you need to define :file_name in the context where this will be accessed (your controller).]
Even better would be to use a model as the basis for your route. So, if you have (for example) a File model, you would use:
resources :files
and Rails will generate a whole pile of routes for you automatically (or you can limit the routes by doing something like resources :files, only: [:show, :index, :create, :delete]).
Basically, I think you need to read up more on how Rails, and routes specifically, are supposed to work. RailsGuides is probably a good place to start. If you work with Rails and its conventions, you'll find your life a lot easier! Rails prefers convention over configuration!
Also, on a side issue, you're not following Rails convention in naming your methods and variables. CamelCase is used in object names (like class HomeController) but not method names or variable names - Rails uses snake_case for these. Again, if you stick to Rails convention (show_files not showFiles, for example), you'll find things go a lot smoother. Following the Rails conventions means Rails will seamlessly stitch things together for you; fail to follow them and Rails will be a complete nightmare.
Based on the error, it looks like you are trying to access /submission3 route even though you have defined /home/submission3 route. Updating the route should fix your routing error.
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
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?
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.
I have a controller called 'reports'. In it, I want to display pre-made reports through partials. As it stands, I don't want to make a fully-fledged sql query/report generator so I figure the easiest way to do this is that every time I want to make a new report, I do so only in a partial file. I then upload that file and all the routes/methods are generated on the fly. That means that there is a method in the controller that looks through the views directory for partials and then returns a url-safe string to be used in the routes and in the controller models.
The question I have is "would this produce a bottleneck in speed"? Such that every time a url is requested, the directory is searched.
Controller:
class ReportsController < ApplicationController
def initialize
super()
ReportsController::reports(true).each do |report|
self.class.send(:define_method, report.gsub('-', '_').to_sym) do
render "#{name_to_safe_url(report).gsub('-', '_')}.html.erb"
end
end
end
# Currently I'm just using a static array to give the reports.
# This will change such that it will look through the views directory
# for partials. It will read the file name and return it as part of the list
def self.reports(return_url = false)
list = [
"Revenue/Sale",
"Item Quantities",
"Number of Sales/Day",
"Compare Revenue to Past Dates"
]
return_url ? list.map{|i| name_to_safe_url(i)} : list
end
def self.name_to_safe_url(name)
name.gsub(/\//, ' per ').squeeze(' ').gsub(/[^a-z]/i, '-').squeeze('-').chomp('-').downcase
end
end
Please repeat the Skinny Controller Fat Model mantra before writing code like this.