Active admin In Rails 4 - ruby-on-rails

I have Rails 4 app, which I want to manage with Active admin. I have 2 models,
Item
Category
Item has 2 fields, name and category_id. The Category model has a field called name. The models are related with has_many :items and belongs_to :Category.
When I try to access the admin panel in Active Admin, after configuring some aspect in Active Admin, in the model Item I have a desplegable menu with the id reference of the category like this #Category:=0x675654. What I want is to get the name of the category. How can I to do this? I don't have access to edit this view.

How about something like:
ActiveAdmin.register Item do
form do |f|
f.inputs "Item" do
f.input :category, as: :select, collection: Category.all.collect {|c| [c.name, c.id] }
end
end
end

If what you want is to show the name in the index page, you need to customize it with something like:
column 'Name' do |item|
name = Category.find(item.category_id).name
end

To simply display the name :
ActiveAdmin.register Item do
menu parent: 'My Menu'
index do
id_column
column 'Category' do |item|
item.category.name
end
end
end
If you'd like to get a link to the resource on top of that name, use auto_link :
ActiveAdmin.register Item do
menu parent: 'My Menu'
index do
id_column
column 'Category' do |item|
auto_link(item.category, item.category.name)
end
end
end
The same thing applies for Item views.

Related

Wrong display of user names in ActiveAdmin forms

I created a FreeMeal tab in my DB; this tab has a foreign key on the User tab. When I use the form in Active Admin to create a new entry of FreeMeal, I get a list of all my users for the user input; but users aren't displayed the right way, like so:
edit: the form is automatically populated as I have the foreign key on users. I simply have the following code for this form in app/admin/free_meals.rb:
ActiveAdmin.register FreeMeal do
form do |f|
f.inputs "FreeMeal" do
f.input :user
f.input :reason
end
f.actions
end
end
I would like to have this list with my users' id, first_name and last_name.
How can I do that ?
Try this:
f.input :user, as: :select, collection: User.all.map{ |u| ["#{u.id}, #{u.first_name} #{u.last_name}", u.id]}, multiple: false

How do I call the name of a user when they belong to a model

I have two models, Chasing and User, a chasing belongs_to :user and a user has_many :chasings.
I created a migration for linking the two models together:
class AddUsersToChasings < ActiveRecord::Migration
def change
add_reference :chasings, :user, index: true, foreign_key: true
end
end
I have a controller for creating new users which I then want to be able to assign to chasings. I currently have this code in my chasings form for selecting the user:
<%= f.select :user_id, options_for_select(User.all.map {|c| [c.name, c.id]}), { :include_blank => "Please select user"}, {:class => "form-control"} %>
This seems to do the trick, after calling Chasing.first in rails console I can see the chasing now has user_id relevant to the user I picked. I can also run Chasing.first.user.name to give me the name of the user who is associated with the chasing. I'm wanting to show this name in my index view, the code I currently have for this is:
ChasingsController:
def index
#chasing = Chasing.all
end
Index view:
<% #chasing.each do |chasing| %>
<%= chasing.user %>
<% end %>
This shows a random string (seems to change every time I update a chasing - #<User:0xf5b0ba8> for example). when I change this to chasing.user.name I get 'undefined method `name' for nil:NilClass'.
Is there a way I can call the name for my view?
EDIT:
As per NickM's comment below I had chasings without users assigned to them causing active record to throw the error.
Looks like you have some Chasing objects in your database without user_ids. You can test by doing <%= chasing.user.name if chasing.user %>

Pass a parameter to the new action in Active Admin

I have two related models, Bunny has_many BunnyData (which belongs_to Bunny). From the show page of a particular Bunny (in Active Admin), I want to create a link to make a related BunnyData. I've tried a few different ways, with no success, and am currently trying this:
sidebar :data, :only => :show do
link_to 'New Data', new_admin_bunny_datum(:bunny_id => bunny.id)
end
The link being generated ends up as something like:
.../admin/bunny_data/new?bunny_id=5
But when you go to that page, the dropdown for Bunny is set to the blank default as opposed to showing the name of Bunny with ID 5.
Thanks in advance.
Rails namespaces form fields to the data model, in this case BunnyData. For the form to be pre-filled, any fields provided must also include the namespace. As an example:
ActiveAdmin.register Post do
form do |f|
f.inputs "Post Details" do
f.input :user
f.input :title
f.input :content
end
f.actions
end
end
The fields can be pre-filled by passing a hash to the path helper.
link_to 'New Post', new_admin_post_path(:post => { :user_id => user.id })
Which would generate the following path and set the form field.
/admin/posts/new?post[user_id]=5
In the case of BunnyData, it might be slightly different due to the singular and plural forms of datum. But that can be verified by inspecting the generated HTML to find the name attribute of the inputs.

ActiveAdmin show, edit, and delete actions in custom models

I have a rails 3 application which uses the ActiveAdmin gem.
If I do not customizes my models, 3 actions are enabled in each line of my model : show, edit, delete
But if I customizes my model, the actions disappear.
Model not customized showing the actions (users.rb) :
ActiveAdmin.register User, as: 'Users_full' do
menu :parent => 'Users'
end
Custom model not showing actions (companies.rb) :
ActiveAdmin.register Company do
index do
selectable_column
column :name
column :url
end
csv do
column :name
column :url
end
end
Is there a way to get actions in customized models ? I have already tried to add : actions :all, config.batch_actions = true and action_item to my companies.rb file but nothing change.
Add the actions, like:
index do
selectable_column
column :name
column :url
actions
end
You're defining the index page's content, and that content includes the actions-if you omit them, they won't show up.

ruby on rails how to use FormOptionHelpers to create dynamic drop down list

I have checked some tutorials but I got confused by the parameters in this method
collection_select (object, attribute, collection, value_method, text_method, options = {}, html_options ={})
I have a map model includes: :area, :system, :file
and I want to read :area from database to a drop down list, and let user choose one
I already did #map = Map.all in the view
what the method should be?
especially the parameter "attribute". In a lot tutorials, people put "id" here. But I don't know what "id" is, and in my situation I don't need any other value, just the "area".
Im not exactly sure what you are asking here but if you are trying to make a dropdown selection for use in an html form will this example help you at all?
<% nations = {'United States of America' => 'USA', 'Canada' => 'Canada', 'Mexico' => 'Mexico', 'United Kingdom'=> 'UK'} %>
<% list = nations.sort %>
<%= f.select :country, list, %>
Here nations is a hash of countries then list becomes the sorted copy of that hash. An html dropdown is then created as a part of the form "f". ":country" is the part of the model that the data is connected to while list is the options to populate the dropdown with
It's not clear from your question what the model is that's being populated with the area.
Typically, collection_select is used between related models.
eg.
class Category < ActiveRecord::Base
has_many :products
end
class Product < ActiveRecord::Base
belongs_to :category
end
When selecting the 'category' for a product, your view would have something like:
<%= f.collection_select(:category_id, :id, Category.all, :name, include_blank: true) %>
What this does is specify the Product.category_id as the attribute being populated with the value of Category.id. The values come from the Category.all collection, and with Category.name being the item displayed in the select. The last (optional) parameter says to include a blank entry.
Something like the following is probably what you need:
<%= f.collection_select(:map_id, :id, #map, :area) %>
However, if the model you're trying to populate has an area attribute (instead of an ID linking to the map), you might need to use:
<%= f.collection_select(:area, :area, #map, :area) %>
This specifies that the area attribute of the receiving table will be populated with Map's area attribute, which is also being used as the "description" in the select.

Resources