Getting error while rendering form in active admin dashboard - rails? - ruby-on-rails

I am getting error while rendering the form from partial in active admin dashboard.My form partial look like:
app/views/admin/dashboard/_form.html.arb
insert_tag active_admin_form_for :bill do |f|
inputs do
input :from_date
input :to_date
input :expiry_date
end
actions
end
And my dashboard code looks like :
ActiveAdmin.register_page "Dashboard" do
menu priority: 1, label: proc{ I18n.t("active_admin.dashboard") }
content title: proc{ I18n.t("active_admin.dashboard") } do
columns do
column do
panel "Generate Bills" do
render partial: 'form' #rendering form partial
end
end
end
end
I am getting following error while clicking on dashboard menu link :
help me out
thnxx!

Related

Render custom edit partial in a 'Custom Page' in ActiveAdmin

I'm using ActiveAdmin for a project. I have and I have a partial with a custom form. I want to be able to render this custom form when the user clicks in a button so this is what I did:
ActiveAdmin.register_page "Messages" do
menu priority: 5
welcome_message = Template.first
page_action :update, method: :put do
#update code
end
page_action :edit, :method => :get do
render partial:'custom_form', locals: { settings: welcome_message }
end
action_item do
link_to "Edit", admin_welcome_messages_edit_path, :method => :get
end
content do
render text: "HI"
end
end
This works, but the problem is that my form gets rendered without the layout and styles of ActiveAdmin, it just shows my custom_form as a clean html.
If I render my custom_form in the content do ... end it does work, but I need that to show something different.
Any help?? I don't know what else to try, I have reached the first 3 pages of google without success!!
kind of an old question but I was having the same problem and this solved it for me.
render partial: "name_of_your_partial", layout: "active_admin"
wasn't working for me either so I just changed my partial into a template.
render: "name_of_your_template", layout: "active_admin"
and now the activeadmin layout is being displayed.
I did not find an answer for this but I found a nice workaround that looks pretty good.
In the action_itemI have a link to admin_welcome_messages_path that is the main view and if I'm editing add a param there to show the form instead of the body.
Hope it helps somebody!
ActiveAdmin.register_page "Messages" do
menu priority: 5
welcome_message = Template.first
page_action :update, method: :put do
#update code
end
action_item do
link_to "Edit", admin_welcome_messages_path(edit: true), :method => :get
end
content do
if params["edit"] == "true"
render partial:'form', locals: { settings: welcome_message }
else
render partial:'body'
end
end
end
Add layout option to the render call.
render partial:'custom_form', layout: 'active_admin', locals: { settings: welcome_message }
Determine which layout to use.
If we're rendering a standard Active Admin action, we want layout(false)
because these actions are subclasses of the Base page (which implements
all the required layout code)
If we're rendering a custom action, we'll use the active_admin layout so
that users can render any template inside Active Admin.
https://github.com/activeadmin/activeadmin/commit/ce0927661c5991cc857306363c402ef9e283cc42
# this would automatically render the custom_edit.html.erb view
page_action :custom_edit do
end
action_item do
link_to "Custom Edit", admin_welcome_messages_custom_edit_path
end
I realise that this an old question but the following worked for me for my Ruby on Rails application:
Inside your your_custom_page.rb file do the following
content do
render 'partial_name'
end
and place the _partial_name.erb in view/admin/your_custom_page/
Your partial should now render on that active admin page.

ActiveAdmin Custom page with index table

I have created a custom page with ActiveAdmin as follows:
ActiveAdmin.register_page "message_list" do
controller do
def index
#collection = client().account.messages.list.sort_by{ |message| Date.rfc2822(message.date_sent) }.reverse
render :layout => 'active_admin'
end
end
end
I have created an index.html.erb file with a table that I want to display on this page. This however is not optimal. How do I use the active admin standard table layout that also comes with pagination and display it with my table info? I know that ActiveAdmin PageDSL Class does not include #index and therefore I can't simply do:
index do
selectable_column
id_column
column :to
column :from
default_actions
end
In addition to achieving the ActiveAdmin table layout on a custom page, how do I change the Title of the page itself? As of now it is called "Index".
An easier method would be to define an ActiveAdmin resource for your message class, Message, and limit the actions to only allow :index.
ActiveAdmin.register Message do
actions :index
index do
selectable_column
id_column
column :to
column :from
default_actions
end
controller do
def scoped_collection
super.where(account_id: account.id).order(:date_sent)
# Or provide a custom collection similar to the current implementation:
# client().account.messages.list.sort_by{ |message| Date.rfc2822(message.date_sent) }.reverse
end
end
end
It is also possible to rename the resource if necessary by providing an :as option to the #register method:
ActiveAdmin.register Message, as: "Account Message" do
# ...
end
While the accepted answer works well if you can use an ActiveAdmin resource instead of a custom page, it is possible to get an index-style table on a custom page via Arbre:
<%=
Arbre::Context.new({}, self) do
table_for(client().account.messages, sortable: true, class: 'index_table') do
column :id
column :created_at
end
end
%>

Customise layout (components ) for activeadmin

Activeadmin layout is not just a file, its a collection of components.
How can I override some of the components like, logo, navigation with activeadmin.
The activeadmin layout components are very easy to customize. What you need to do: Just define a module that opens ActiveAdmin::View.
you can have a custom_activeadmin_components.rb in initializers or admin directory where you defined all of your activeadmin resources. I prefer to put it in the directory where your activeadmin resources are. Then override any module you want:
here is a sample:
module ActiveAdmin
module Views
class Header < Component
def build(namespace, menu)
super(:id => "header")
#namespace = namespace
#menu = menu
#utility_menu = #namespace.fetch_menu(:utility_navigation)
build_site_title
build_global_navigation
build_utility_navigation
#you can add any other component here in header section
end
def build_site_title
render "admin/parts/logo"
end
def build_global_navigation
render "admin/parts/main_nav"
end
def build_utility_navigation
render 'admin/parts/language_options'
insert_tag view_factory.global_navigation, #utility_menu, :id => "utility_nav", :class => 'header-item tabs'
render 'admin/parts/branch_in_header'
end
end
module Pages
class Base
def build_page_content
build_flash_messages
div :id => :wizard_progress_bar do
render 'admin/parts/wizard_progress_bar'
end
div :id => "active_admin_content", :class => (skip_sidebar? ? "without_sidebar" : "with_sidebar") do
build_main_content_wrapper
build_sidebar unless skip_sidebar?
end
end
end
end
end
end
You can customise active admin page in admin/your_model.rb file.
Sample code for active admin is as follows.
ActiveAdmin.register User do
menu :label => "Our User", :priority => 3 #rename menu & set priority#
#for index page#
index :title => 'Our User' do #set page title#
# index :download_links => false do
selectable_column
column :title
column :category do |e| #want to change name of category
e.categoryone
end
column :address
default_actions#this will add default action i.e. show|edit|delete#
end
#end index#
#for controller#
controller do
actions :all, :except => [:edit, :new] # you can decide which all methods to be shown in show page.
end
#end controller#
#show start page#
show do |user|
h3 "User Details"
attributes_table do
row :title
row :description
row :address, :label=>"User Address" #overide address label
row :country
row :approval
end
h3 "Activity Photoes"
attributes_table do
user.uploads.each do |img| #call associated model. Here i want to display uploaded image of upload.rb file
row(" ") do
link_to image_tag(img.to_jq_upload['small_url']), admin_upload_path(img)#here i have added upload module to admin thats y i can directly give path to the upload record of admin module#
end
end
end
active_admin_comments # to show comment block of active admin
end
#show end#
#filer(search) start righthand side panel#
filter :title
filter :category
filter :address
#end filter#
#for menu on show page#
action_item only:[:show] do # this add "Approve User" button on the right hand side of show menu only as we have specified only show method
if User.find(params[:id]).approval == true
link_to "Approve User", approv_user_path(:id => params[:id])#call custom method of normal rails controller#
end
end
#end menu#
#start add side bar on page#
sidebar "Project Details" do
ul do
li link_to("Profile", new_project_path)
end
end
#end side bar#
end

How to display ActiveAdmin menu tabs in the dashboard?

I have too many models in my rails app. And they don't fit in the top menu bar in active admin.
I would like to know how to display the top menu tabs, in a vertical menu via the dashboard?
Thanks in advance.
Eg:
Model 1
Model 2
Model 3
...
Dashboard.rb
ActiveAdmin.register_page "Dashboard" do
menu :priority => 1, :label => proc{ I18n.t("active_admin.dashboard") }
content :title => proc{ I18n.t("active_admin.dashboard") } do
div :class => "blank_slate_container", :id => "dashboard_default_message" do
span :class => "blank_slate" do
span I18n.t("active_admin.dashboard_welcome.welcome")
small I18n.t("active_admin.dashboard_welcome.call_to_action")
end
end
# Here is an example of a simple dashboard with columns and panels.
#
# columns do
# column do
# panel "Recent Posts" do
# ul do
# Post.recent(5).map do |post|
# li link_to(post.title, admin_post_path(post))
# end
# end
# end
# end
# column do
# panel "Info" do
# para "Welcome to ActiveAdmin."
# end
# end
# end
end # content
end
You can use the :parent option to the menu method to assign each model into a menu drop down in each model's admin file.
So:
ActiveAdmin.register User do
menu :parent => "User Data"
end
If you want to create a list of links on the dashboard page, your code would look something like this:
content :title => proc{ I18n.t("active_admin.dashboard") } do
columns do
column do
panel "Models" do
ul do
li link_to("Users", admin_users_path)
li link_to("Admin Users", admin_admin_users_path)
end
end
end
end

Keep form fields filled after an error (RoR)

After validation, I got an error and I got returned back to :action => :new.
Some field on form already filled, so I want to keep them filled even after error message too.
How it can be done?
Your View (new.html.erb) something like following
<%= error_message_for :user %>
<% form_for :user, :action=>"create" do|f|%>
<%= f.text_field :login %>
<% end %>
Controller Code (create method)
def create
#user=User.new(params[:user])
if #user.save
redirect_to :action=>'index'
else
render :action=>'new' #you should render to fill fields after error message
end
end
Since in my case the form was in the view of another controller I did use flash to store my data and then check if there is data in flash present. If yes take this for default values for your input fields, if not just show whatever you want to show then.
So snippets from my code
flash[:date] = start_date
# in the view where to form resides
start_day = flash[:date].nil? nil : flash[:date].day
# ...
<%= select day start_day ... %>
Hope that helps some of you ;-).

Resources