rolify+activeAdmin add role by form - activeadmin

i m using rolify+activeadmin gems.
I have 2 resource: Staff and User (default devise table).
Staff is a model that map an only read table, so i can't write in staffs table.
i'm trying with active admin to add a role for a user using has_one and belongs_to associations:
class User < ActiveRecord::Base
rolify
belongs_to :staff
end
class Staff < ActiveRecord::Base
has_one :user
end
in the app/admin/staff.rb class i have this:
form do |f|
f.inputs "Add role" do |staff|
f.input :roles, :as => :select, :collection => Role.global
end
f.actions
end
So i want to add a role for a user using Staff admin resource.
when i click on submit form button i have this error:
NoMethodError in Admin/staffs#edit
Showing app/views/active_admin/resource/edit.html.arb where line #1 raised:
undefined method `roles' for #<Staff:0x00000005c6af70>
Extracted source (around line #1):
1: insert_tag renderer_for(:edit)

Roles is part of User model, not Staff model. Add your form to app/admin/user.rb instead, and then you will be able to assign a role to a user. Also, in the user's form, you can assign the staff record. Here is an example form:
# app/admin/user.rb
form do |f|
f.inputs 'Name' do
f.input :name
end
f.inputs 'Add role'
f.input :roles, :as => :select, :collection => Role.global
end
f.inputs 'Staff' do
f.input :staff
end
f.actions
end
You can also add a delegate to staff to be able to read the roles natively in the Staff model.
# app/models/staff.rb
class Staff < ActiveRecord::Base
attr_accessible :name, :user_id
has_one :user
delegate :roles, :to => :user
end

Related

ActiveAdmin: Child object claims to not have method parent_object_id

I have solved this issue before but the solution currently escapes me.
I have two Models FlsCenter and Airport. FlsCenters has_many airports.
In active admin I am creating a newe Airport object. however when I press New Airport in the dashboard, I get the following error:
undefined method `fls_center_id' for #<Airport:0x007fbf5457bf00>
Here are the model definitions for both models:
class Airport < ActiveRecord::Base
belongs_to :fls_center
end
class FlsCenter < ActiveRecord::Base
has_many :housing_options
has_many :airports
has_many :programs
end
Here is my Schema.rb
create_table "airports", force: true do |t|
t.string "name"
t.integer "pick_up_cost"
t.datetime "created_at"
t.datetime "updated_at"
end
notice that there is not field fls_center_id. Doesn't rails take care of that via belongs_to has_many? How come my schema does not reflect my models?
I ran rake db:migrate. Also for reference I have included fls_center_id as a permitted param to active admin.
Rails won't create database columns for you just from setting up the model. You can add another migration to add this new field. Try:
rails g migration add_fls_center_to_airports fls_center:reference:index
And then run rake db:migrate.

make column of associated resource in index table non-linked

Okay, using ActiveAdmin (0.6.3) and am able to use the following code to get a caller's location attribute to appear in the table of callers. The name of each location appears as a link to the "show" action for the location. The "show" action and result is not useful for my application. I want to remove the link but keep the text. Help? Thanks :)
ActiveAdmin.register Caller do
index do
column 'Location', :location, :sortable => 'locations.name'
end
controller do
def scoped_collection
resource_class.includes(:location)
end
end
end
class Caller < ActiveRecord::Base
attr_accessible :active, :assignedname, :callingnumber, :description, :location_id, :lookupcount, :lastlookuptime
belongs_to :location
end
class Location < ActiveRecord::Base
attr_accessible :active, :description, :name, :callers_attributes
has_many :callers, dependent: :destroy
end
Try this.
index do
column 'Location', :sortable => 'locations.name' do |caller|
caller.location.name
end
end

ActiveAdmin won't save has many and belongs to many field

I have 2 models. Category and Post. They are connected using a has_many_and_belongs_to_many relationship. I checked in the rails console and the relationship works.
I created checkboxes in activeadmin to set the post categories using this form field:
f.input :categories, as: :check_boxes, collection: Category.all
The problem is when I try to save it because every other field data (title, body, meta infos etc.) is saved, but the category stays the same even if I unchecked it, or checked another too.
I am using strong parameters like this:
post_params = params.require(:post).permit(:title,:body,:meta_keywords,:meta_description,:excerpt,:image,:categories)
Please give me some suggestions to make active admin save the categories too!
Best Wishes,
Matt
Try this in AA:
controller do
def permitted_params
params.permit post: [:title, :body, :meta_keywords, :meta_description, :excerpt, :image, category_ids: []]
end
end
Put something like this in /app/admin/post.rb:
ActiveAdmin.register Post do
permit_params :title, :body, :meta_keywords, :meta_description, :excerpt, :image, category_ids: [:id]
end
If you are using accepts_nested_attributes_for then it would look like this:
ActiveAdmin.register Post do
permit_params :title, :body, :meta_keywords, :meta_description, :excerpt, :image, categories_attributes: [:id]
end
I've tested, this might works for you and others as well
# This is to show you the form field section
form do |f|
f.inputs "Basic Information" do
f.input :categories, :multiple => true, as: :check_boxes, :collection => Category.all
end
f.actions
end
# This is the place to write the controller and you don't need to add any path in routes.rb
controller do
def update
post = Post.find(params[:id])
post.categories.delete_all
categories = params[:post][:category_ids]
categories.shift
categories.each do |category_id|
post.categories << Category.find(category_id.to_i)
end
redirect_to resource_path(post)
end
end
Remember to permit the attributes if you're using strong parameters as well (see zarazan answer above :D)
References taken from http://rails.hasbrains.org/questions/369

rails_admin searchable association

I am using rails_admin together with globalize3 and cannot get searchable associations to work. Here are the models (Person has_one/belongs_to Name has_many/belongs_to NameTranslation):
class Person < ActiveRecord::Base
has_one :name, inverse_of: :person
end
class Name < ActiveRecord::Base
belongs_to :person, inverse_of: :name
translates :first_name, :last_name
has_many :name_translations, inverse_of: :name, dependent: :destroy
end
class NameTranslation < ActiveRecord::Base
belongs_to :name, inverse_of: :name_translations
end
The NameTranslation model is coming from globalize3, it contains the same attributes as name (first_name and last_name) plus locale and name_id,.
In config/initializers/rails_admin.rb I have
config.model Person do
list do
field :name do
searchable name_translations: :last_name
end
end
end
Then, in the GUI, when I add a filter on name, I get:
SQLite3::SQLException: no such column: name_translations.last_name: SELECT "people".* FROM "people" WHERE (((name_translations.last_name LIKE '%freud%'))) ORDER BY people.id desc LIMIT 20 OFFSET 0
Obviously, rails_admin is looking for a column named name_translations.last_name in people instead of joining/including names and name_translations - why?
What I need rails_admin to do is this, working in irb:
>> Person.joins( name: :name_translations ).where('name_translations.last_name like "test"')
which generates the following SQL:
SELECT "people".* FROM "people" INNER JOIN "names" ON "names"."person_id" = "people"."id" INNER JOIN "name_translations" ON "name_translations"."name_id" = "names"."id" WHERE (name_translations.last_name like "test")
Can this be done in rails_admin? Thanks for your help...
From this thread, I followed Nick Roosevelt's suggestion and it worked for my case
class Room < ActiveRecord:Base
has_many :time_slots
end
class TimeSlot < ActiveRecord::Base
belongs_to :room
rails_admin do
list do
field :day do
searchable true
end
# field :room do
# searchable room: :name
# end
field :room do
searchable [{Room => :name}]
queryable true
end
end
end
end
I tried searchable room: :name and it was not working, but searchable [{Room => :name}] seem to make it work.
I had a similar problem with a has one relationship.
The way I solved it was to set a default_scope on the model and join it with the associated table (it is was the only way I could get rails admin to join these two tables).
I also had to set queryable true on the associated field.
Imagine that you had to search only inside the name association, then here's how it would work:
class Person < ActiveRecord::Base
has_one :name, inverse_of: :person
default_scope { eager_load(:name) }
end
config.model Person do
list do
field :name do
queryable true
searchable [:column1, :column2, ..]
end
end
end
However, you need to search through the has many association and I don't know whether that approach would still work, but here's a guess:
class Person < ActiveRecord::Base
has_one :name, inverse_of: :person
has_many :name_translations, through: :name
default_scope { eager_load(:name_translations) }
end
config.model Person do
list do
field :name_translations do
queryable true
searchable :last_name
end
end
end

RailsCast 197: nested forms Unknown Attribute

I have two tables:
Quote and Part. I am using the rails 3.1 ruby 1.9.2. My code is as follows:
class Quote < ActiveRecord::Base
attr_accessible :name, :customer_id, :part_id, :date, :quote_id
has_many :customers
has_many :cycles
has_many :parts, #:dependent => :destroy
accepts_nested_attributes_for :parts
end
class Part < ActiveRecord::Base
belongs_to :quote
end
QuotesController
def new
#quote = Quote.new
#customers=Customer.find(:all)
#cycles = Cycle.find(:all)
1.times {#quote.parts.build}
end
_form
<% f.fields_for :parts do |builder| %>
<%= render "cycle_fields", :f => builder%>
I am getting an unknown attribute: quote_id when I try to render the form.
Run this migration
rails g migration AddIdToPart quote_id:integer
rake db:migrate

Resources