Multiple attributes in activeadmin associated dropdown - ruby-on-rails

I am using Active Admin where I have two models User and Post. The relationship is User has many Post and Post belongs to User. The attributes of User are name, id, phone-number, email and address. In new form of Post, associated dropdown of User is only showing name of each user but I want to show name,phone-number and address of every user. I am new to RoR. So any help will be highly appreciated.

To modify the associated dropdown you should edit your form association's input in the ActiveAdmin form. ActiveAdmin uses formtastic to generate forms for your models. When you set your User model in your Post's form as f.input :user ActiveAdmin set the input type to :select.
So, you can use f.input :user, as: :select and it'll work in the same way.
Formtastic Select let us edit the label rendered in each option through the member_label property. First add the property to your input in the ActiveAdmin form
f.input :user, member_label: :full_label, then declare the full_label method in your User model returning the joined string with the user attributes.
def full_label
"#{name} - #{phone-number} - #{address}"
end

Related

How do I hide fields thats rails admin shows when I create a new model (user)?

When I'm going to create a new user, rails admin shows me many fields like created_at, sign_in_at, etc, etc etc.
You need to use create instead of list in the config block.
Enter the following code in config/initiazlizers/rails_admin.rb.
See https://github.com/sferik/rails_admin/wiki/Fields for further details regarding the customization of fields.
config.model 'User' do
create do
field :name
field :email
# etc....
end
end

Active Admin Model Index Page (Model List View) define order of displayed fields

Is there an easy way to change the order of displayed field of a Model in the Active Admin index view for a model? I have far more fields then Active Admin can display but i want to be able to define the most relevant fields for the user in this view.
The relevant fields in my case (see screenshot) are hidden (..) by Active Admin and only can be seen then i change to the show view of a model instance.
You should be able to define the fields to display in the index view inside the ActiveAdmin model configuration, i.e.
ActiveAdmin.register Place do
config.sort_order = 'name_asc'
index do
column :name
column :address
column :place_type
column :published
default_actions
end
end
You can read more about ActiveAdmin options here: http://www.activeadmin.info/docs/3-index-pages.html

How do I perform multiple modifications and save a has_many relationship

class Company < ActiveRecord::Base
has_many :users
end
The user hits on Company->Users href link and assuming it shows 3 users for a certain company.
I would want to set user.age (an integer) for all the users and press save. How do I do this in my controller / view code?
We usually have an Edit link against each User to modify its details, but I would like to set age information for all users.
Use a form_tag to submit to an arbitrary controller action set up specifically for editing multiple users (not one of your user scaffold methods). Then, add one fields_for tag per user inside of the form you created, each with a field for the age attribute. When submitted, you can simply iterate over the user params in the controller to manipulate your users.

How do you use a form select input for a polymorphic belongs_to association in Rails?

I've got an Article model that has a polymorphic belongs_to association (resource). I've been trying to let it set the association from the article form via two select boxes, one with the type and the other with id of the association as shown below.
= form_for #article do
= f.select :resource_type, ...
= f.grouped_collection_select :resource_id, ...
My problem is that it doesn't select the correct model in the grouped_collection_select if the association id exists in two of the associated models.
There must be a better way to do this but I just can't seem to see it right now.
One way to handle it is to allow toggling between resources with a javascript function. You have a link that is bound to a javascript function that swaps out one resource select box for another.
There's a railscast that describes how to do this. Ignore the nested form and add/remove business and focus on the essentials (javascript-bound link, helper methods, etc): http://railscasts.com/episodes/197-nested-model-form-part-2.

Rails 3 : Mass-assignment with ActiveAdmin and has_one

I am developing a rails application in which I have two models User and Client.
User is backed by devise and is responsible for authentication and has_one Client which holds the client details for a given user. This relation is always present as I ensure that a Client model is created whenever I create a User.
For the administration area I am using ActiveAdmin. Now, when I try to create a User through the administration interface I use a form like this:
form do |f|
f.inputs :username, :email, :password
f.inputs :name => "Client", :for => :client do |client|
client.inputs :name, :address, ...
end
end
The problem is that either the User nor the Client are saved and the page is reloaded with validation errors. I have checked rails console and there's a WARNING: Can't mass-assign protected attributes: client_attributes message every time I try to create a User.
I have searched over this issue and found that in order to allow for mass-assignment one had to define attr_accessible for each of the fields allowed for the assignment. So, I had put this directive in Client model for each of the fields mentioned above and the message keeps appearing, preventing the models to be properly saved.
Does anyone have a clue on this?
The problem is not in your Client model, but in your User model - because this is the primary model you are trying to create. All you need to do is to add client_attributes to the list of attr_accessible attributes in your User model, just as the error message in the log files says, e.g.:
class User < ActiveRecord::Base
attr_accessible :client_attributes
end
I imagine you already have a list of accessible attributes in the User class. So just add client_attributes to the end of that list.
The changes you made to your Client model (i.e. adding a list of attributes to attr_accessible) is not needed for this to work. If you want, you can also go ahead and undo that.

Resources