Active admin shows html code - ruby-on-rails

I'm using ejholmes Active_admin Editor and it's working fine but in my active admin index I see the html code with tags and not the correct output. I do use .html_safe in my views but here's my active admin's posts.rb
...
index.do
column :id
column "Titolo", :title
column "Corpo news", :body
column "Creato il", :created_at
column "Modificato il", :updated_at
column :link
column "Link Immagine", :image_url
bool_column :attivo
default_actions
end
...
I want to have my "Corpo news" column rendered.
Thanks for the help!

I guess you need to use the method .to_html as you can see by the source
So in the view you need to call
body.to_html

Related

Display the total for a single columns values in Activeadmin

I am trying to display a box at the bottom of my ActiveAdmin index page where it will total all of the values of the collection and display this total value. I am trying to sum the value of the column :number_books and I am currently using this code as suggested when someone answered a similar question a few years ago.
This does not appear to be having any impact on my index page as nothing is visibly changing on the index page for :orders.
Any help is greatly appreciated!
ActiveAdmin.register Order do
index do
column :email
column :customer_name
column :number_books
column :street_address
column :state
column :zip_code
column :total
default_actions
div class: "panel" do
h3 "Total amount: #{collection.pluck(:number_books).reduce(:+)}"
end
end
end
I found out I had to define this new index page I was creating as the default, otherwise activeadmin uses the default index page and ignores my custom one. I also had to change default_actions to simply actions and this now works great!
the working code is:
ActiveAdmin.register Order do
index default: true do
column :email
column :customer_name
column :number_books
column :street_address
column :state
column :zip_code
column :total
actions
div class: "panel" do
h3 "Total amount: #{collection.pluck(:number_books).reduce(:+)}"
end
end
end

Active Admin Errors

I am using active admin for the first time, and I don't know much about it. I'm trying to create a table (in admin dashboard view) that shows certain details of the model object (which are created via form submission.)
I simply want to be able to view recent additions to the model object from the admin dashboard.
I've read the documentation on Active Admin, but it seems like most of it is using examples that don't entirely meet what I'm trying to do. (Or it could just be that I am a newbie at this)
I've looked at a few forums and found some examples, but even they use multiple variations on the table_for method.
I am getting this error:
NoMethodError in Admin::Dashboard#index Showing
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/activeadmin-1.4.3/app/views/active_admin/page/index.html.arb
where line #2 raised:
undefined method `first_name' for # Did you
mean? sti_name
Any insight or advice would be appreciated. I've posted my code from dashboard.rb below:
ActiveAdmin.register_page "Dashboard" do content :title => proc{ I18n.t("active_admin.dashboard") } do columns do column do
panel "New Teacher Applicants" do
table_for Volunteer do |t|
t.column("Name") { |volunteer| volunteer.first_name }
t.column("Surname") { |volunteer| volunteer.last_name }
t.column("Email") { |volunteer| volunteer.email }
t.column("Gender") { |volunteer| volunteer.gender }
end
end end end
end end
I think this is what you are looking for:
panel 'New Teacher Applicants' do
table_for Volunteer.order("created_at desc").take(5) do
column "Name", :first_name
column "Surname", :last_name
column "Email", :email
column "Gender", :gender
end
end
The argument to table_for should be a collection. Maybe take a closer look: https://activeadmin.info/12-arbre-components.html#table-for

Displaying currency symbols in form dropdown menu

I have a form in a commerce application where users can add an item listing.
In this create#item form, I'd like the user to be able to select (from a dropdown menu) what currency their pricing is in.
I've created a Currency model, views and a controller so the admin can add currency types. (I want the admin to be able to limit currency types).
Here is the currency migration file:
class CreateCurrencies < ActiveRecord::Migration
def change
create_table :currencies do |t|
t.string :name
t.string :symbol
t.timestamps null: false
end
end
end
(The "symbol" being a string that holds the currency HTML code)
I connected Currency and Item with a belongs_to/has_many relationship in the db. I then implemented a dropdown menu in the create#item form where users can select the currency.
My question is, how can I display the currency symbol in the dropdown menu?
Here's what I tried.
<%= f.collection_select :currency, Currency.order(:name),:id, "#{:symbol}".html_safe %>
The problem is, this doesn't display the currency symbols as I would have hoped; it just displays the string that was entered (the currency HTML code).
For example, with the code as it is, if an Admin entered the currency HTML code for $ (&#36), the dropdown shows "&#36" isntead of the expected "$")
Thanks in advance!!
Quick answer is: use raw method to unescape the html code.
I've just reproduced your code on my machine and noted a strange behavior.
For some reason raw doesn't work with collection_select and I can't figure why. Consider using select helper and 'manually' iterate your collection. Here is two identical variants:
= form_for "test" do |f|
%p collection_select variant
= f.collection_select :currency, User.all, :id, raw(:symbol.to_s)
%p select variant (works!)
= f.select("currency", User.all.collect {|u| [ raw(u.symbol), u.id ] })
You can use the HTMLEntities gem. I would recommend setting it up as a helper method which you can use in the view.
helper file
def currency_symbol(currency_code)
HTMLEntities.new.decode(currency_code)
end

Acts As Taggable On with Rails Admin

I have installed the gem acts-as-taggable-on, everything is working well with my model, but I would like to use Rails Admin and this is where I have a problem...
In the form new or edit, I would like to display the field Tags with 2 columns, the first one with the list of all the tags already recorded and the second with tags I want to use. For now, I just have a simple field, where I can enter tags with comma.
I have added the code below in rails_admin.rb:
config.model Recipe do
edit do
field :title
field :url
field :prep_time
field :cook_time
field :serve
field :picture
field :publisher
field :ingredient_lines
field :ingredients
field :tag_list do
html_attributes do
{:style => "width:90%"}
end
end
end
end
Just to give you a idea of what I want, here are a screenshot.
Screenchot link
Thank you for your help :)
Problem solved. I have created my own tag model, no everything is working well.

Override index columns activeadmin

So I would like to have a shortened table with batch actions.
ActiveAdmin.register User do
batch_action :acitve do |selection|
User.find(selection).each do |user|
user.active! true
end
end
filter :email
index do
column :id
column :first_name
column :last_name
column :email
column :sign_in_count
default_actions
end
end
However batch action box is greyd out. It's understanbale because nothing is selected. However when I use default index settings (no columns specification), the checkbox stays there. How can I have a default checkbox with custom columns?
according to this (Customizing Table Columns part) you need to add
index do
selectable_column #batch actions checkboxes column
column ...
...
end
to render checkboxes

Resources