adding a text area field to devise edit form - ruby-on-rails

I have an attribute called discount_info. I would like to create another field in the devise edit form, so user can edit this information. I would like to have the field be a text_area. I'm not sure how to do this in rails.
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :address %><br />
<%= f.email_field :address, autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>

<%= f.text_area :discount_info, :class => "", :placeholder => "if you want a placeholder" %>

Just add the attribute in the form and permit it in the configure_permitted_parameters method.
<div class="field">
<%= f.label :discount_info %><br />
<%= f.text_area :discount_info %>
</div>
And in the controller
def configure_permitted_parameters
#other code
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:other_attributes, :discount_info) }
end

Related

Custom field tag in devise registration view

I want to add custom field tag in devise registration view but that field doesn't exist in users table. I just want user select some value in that field and i get information about selected value in params at the time of sign up. Can anyone suggest me how to do this?
Devise_registration_view:
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :type, 'Role' %>
<%= f.select :type, %w{Admin Teacher Student Guardian}, :prompt => 'Select', required: true, placeholder: 'Roles' %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
You can use attr_accessor for User model which will help you to get and set value for attribute which is not there in table, Please refer this link. attr_accessor_details
updating answer for adding custom validation for attribute created by attr_accessor
attr_accessor :test_attribute
validate :check_test_attribute
def check_test_attribute
errors.add(:test_attribute, "is missing") if test_attribute.blank?
end

Devise: Edit registration form not showing values when used in non Devise controller

I've added the resource helper methods to my settings controller so that I can update the current_user from within the controller. I've taken the form_for from registrations/edit and copied it into my settings controller view. I'm able to successfully update the user, but the existing values aren't showing up in the form as they would if I open users/registrations/edit. It's the same code displaying each form. What am I missing?
settings_controller.rb
helper_method :resource_name, :resource, :devise_mapping, :resource_class
def resource_name
:user
end
def resource
#resource ||= User.new
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end
def resource_class
User
end
devise/registrations/edit.html.erb and general.html.erb
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>

Devise multiple step form for registration

I want the user to be able to register in two steps as I have many fields. Ideally first step would be to accept email and password. As user enter it, they can proceed to fill the next step.
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :first_name %>
<%= f.text_field :first_name, autofocus: true %>
</div>
<div class="field">
<%= f.label :last_name %>
<%= f.text_field :last_name %></br>
</div>
<div class="field">
<%= f.label :city %>
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :address %>
<%= f.text_area :address %>
</div>
<div class="field">
<%= f.label :gender %>
<span class="option">Male</span><%= f.radio_button :gender, "m" %>
<span class="option">Female</span><%= f.radio_button :gender, "f" %></br>
</div>
<div class="field">
<%= f.label :mobile_no %></br>
<%= f.telephone_field :mobile_no %>
</div>
<div class="field">
<%= f.label :website %>
<%= f.url_field :website %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.email_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<% if #validatable %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :skills %>
<%= f.text_field :skills %>
</div>
<div class="field">
<%= f.label :passion %>
<%= f.text_field :passion %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_area :description %>
</div>
<% end %>
I tried the approach where user enters email and password and after that they are redirected to edit page where they can update other fields.
class RegistrationsController < Devise::RegistrationsController
def after_sign_up_path_for(resource)
redirect_to edit_user_path(resource)
end
end
routes.rb
devise_for :users, :pro, :amateur, controllers: { registrations: "registrations" }
However, the redirect doesn't seem to work. I also tried after_inactive_sign_up_path_for as I am using confirmable, still not working.
I can't seem to figure out why this could be also I would like to know if there are any other approach without using any gem?
You could use javascript if you're just interested in user interface. Or if it's important that you're saving all the info as you're going, you can have the form split into partials and override the devise registration controller to render those separate partials.
You will also need to add steps that link with the partials in your user model. I think if you used a gem, this would probably be main part that it will do for you.
this is a great railscast on how to have a multistep form without any gems. the only difference is that you'll need to override the create method in devise

Accept Old Password To Edit Settings

I have a user model with a settings page at /users/1/edit
Currently you can change the email and change your password (w/ confirmation).
I would like to have a field called "current_password" where the user has to type their current password before they can change any information.
Here is my form now:
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
Obviously if I just add the field in, it gives me an error because my user model doesn't have a "current_password" attribute.
How can I do this? Is there a better way, possibly?
You can either add it to the model and database or this to your model:
add attr_reader :current_password
Then you can do a validation of that in your model.

Problem with Ruby Gem Paperclip

I am having trouble using the Ruby Gem paperclip. I followed the instructions in the ReadMe but I cannot seem to get it to actually load my images. Here is my edit form:
<% form_for :user, #user, :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :expertise %><br />
<%= f.text_area :expertise, :class => "expertise" %>
</div>
<div class="field">
<%= f.label :occupation %><br />
<%= f.text_field :occupation %>
</div>
<div class="field">
<%= f.label :city %><br />
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :state %><br />
<%= f.text_field :state %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br />
<%= f.password_field :password_confirmation %>
</div>
<%end%>
Yet when I try to save I keep getting this error: No route matches "/users/4/edit"
What is the problem
The error is telling you that there's no /users/4/edit route. What does your config/routes.rb look like? If there's a line like:
resources :users
Then, try changing that first line to:
form_for #user
Instead of:
form_for :user, #user
Also, I don't see a file_field in there anywhere so I don't think this question is about Paperclip?

Resources