ActiveAdmin: Separate form for some of the fields - ruby-on-rails

We are using ActiveAdmin currently as our main CMS.
For the User model, I want to add a 'Change Password' button which takes you to a form with two fields: 'password' and 'password confirmation'. The form should submit and update only those two fields for the given user. I want to do this all the while keeping the standard form for editing all other attributes.
Is this even possible with ActiveAdmin's DSL, or do I have to get a bit hacky? Their documentation hasn't been very helpful.

Did you check out ActiveAdmin: how to leave user password unchanged? - that should cover the standard form. The change password action can then be implemented with a member action.

Related

Submitting information with a Rails form that is not accessible by user

Looking for the "Rails convention" to do this:
I have a form for a model "user". The person filling out the form enters some information like "name" and "email", but some information is taken from the page due to other actions e.g. "upload_size".
Should I just create a hidden field in the user's form, and populate it with "upload_size"? Previously I had stored the info in JS, but I'm switching off of submitting the form via ajax.
Yes, just use a hidden field. That's what they're there for.

Retaining email address and some additional parameters with Devise

I am using Devise to manage users/registration. I am having a two=-fold issue:
If a mistake is made when filling the user registration form, for example one of the required fields is missing, when the form is re-displayed, the email field is missing (the other fields information is retained).
I have an additional field in the form that's not in the users table. This field is called code. When there's an error (described in #1 above), and the form is re-displayed, information for the code field is not retained either. If I look at the parameters in the log file, this field is a separate parameter (not part of the user parameter).
Suggestions?
To get the info to re-display in the form just add params[:field] to that field. An example would be <%= text_field_tag :email, params[:email] %>.
You may have already figured this about by now but thought I would answer it anyways. If you want help on the other part just post your code :)

Allowing users to choose between a public and private profile. Better way to display this in view?

I am making it so users can choose to allow their profiles to be public or only allowed to registered users.
Here are the steps that I took:
rails g migration AddIs_publicToUsers is_public:boolean
rake db:migrate
I have the :is_public to default=> true in my db
Then I added this into the user.rb model
attr_accessible :is_public
Now I'm trying to create the view but would it be better to do this using radioboxes instead of a checkbox?
<%= f.label :is_public, "Set Profile as Private" %>
<%= f.check_box :is_public, :checked => false %>
The problem with above is that after users check their profile as private and they come back to the page, it doesn't stay checked which means that users aren't able to make their profile public again.
Finally, how do I ensure that is_public is referring to non-signed in users vs. sign-in users? For example, if is_public is true, then all people including signed in and non-signed in can view the profile. However, if is_public is false, then only signed in users of the website can view.
Your question is really multiple questions in one.
Radio vs Checkbox
Given that this appears to be a mandatory and binary choice with a default value, radio buttons are going to be your best bet. As a tip, make sure that your labels are set correctly so the user can click on the label and doesn't have to find the radio button itself with their mouse (which is tiny).
Radio buttons not updating
If the radio buttons are not updating after the form is submitting, the user's input is getting lost somewhere. Check the params being submitted to see if the controller is receiving them, then make sure they are translated into the saved record. If the record is being updated, make sure your radio buttons are reflecting the database value in the form itself.
Signed-in users vs. non-signed-in users
My recommendation is to create a before_filter on the profiles controller, which checks the value of is_public, checks whether the user is signed in, then redirects non-authenticated users wherever you like.

Ruby on rails form validation for user profiles advice needed

I have one model that holds validation rules for my edit_profiles page. On the edit profile page I'm using jquery accordion to split user edit_profile into different sections for users to edit information. Each section is a separate form.
e.g.
Basic info (form 1)
Personal Stats (form 2)
Favourite things (form 3)
About me (form 4)
My problem is successfully filling out information on one form and clicking update is unsuccessful because other validation rules that have been set are firing in because other forms are failing validation because they have not yet be filled in.
I've tried to use the validation_group gem but this seems to have no affect.
I'd like to know if there is an easy way to do this?
Can't I just bunch up validation rules for each form and put them in separate methods and only make them come into play when the update button from a matching form has been clicked?
So if the update button on form 1 is clicked the form_one_validations method would be fire for example and the unrelated validation methods won't.
I would really really appreciate an example of how to do this.
This is the action responsible for y edit_profile view:
def edit_profile
#profile = Profile.find_by_user_id(current_user.id)
end
It is based inside my profiles controller.
Kind regards
I ended up using :allow_blank
This way the fields don't have to be filled in but all other important validation rules are still enforced if they need to be.
I can propose you such approach: you can add additional fields to your model, something like "basic_info_completed" which will be set up once after user has filled all corresponding information. And make all necessary validations conditional and perform them only when such field is set to true. So before user fills all fields of profile section, they are all can be edited without validation, but after profile fields are completed, validation is turned on for that part of profile.

changing form behavior on submit based on radio button selection

I'm using Rails 3.1.0.rc5. I want to have a form with a pair of radio buttons, enable and disable, and a field to enter an integer (expire_after_days, the number of days until ticket expiration), with a hidden field for the fixed parameter subdomain_name. I'd like to be able to use the same simple form to create, edit, or delete a record, depending on which radio button is checked.
So if enable is checked, with no record found for the subdomain_name, a record would be created on form submission.
If enable is checked, and a record is found, the existing record should be updated on form submission.
And if disable is checked, the record should be deleted on form submission.
Is this a reasonable thing to do? If so, what tips do you have for how to do it?
It's not ideal nor restful to have all 3 actions (create, update, destroy) cramped up in just one controller method, but if you wish to continue on this dirty route here's what you could do:
def my_dirty_method
if params[:enable].present?
if params[:subdomain_name].present?
# Edit subdomain
else
# Create subdomain
end
end
if params[:disable].present?
# Delete subdomain
end

Resources