When I go to the edit_user_registration_path provided by Devise I have fields such as email and username pre-filled.
I want to create custom user profile page with the similar form for edit user information containing additional fields of dependent model. And I want fields be pre-filled.
How should I do it properly?
The default form provided by Devise:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :username %><br />
<%= f.text_field :username %></div>
<div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, :autocomplete => "off" %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password %></div>
<div><%= f.submit "Update" %></div>
<% end %>
Use nested attributes
In your devise user model, if you have other dependent models, you can add a relationship between those models, like has_many and belongs_to. The do this:
Devise user model:
accepts_nested_attributes_for :name_of_other_model
Then in your form you can use fields_for. fields_for docs
Here's also a great railscast of this here
Related
rails g devise:views command generated that view
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :username %><br />
<%= f.text_field :username, :autofocus => true %></div>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>
My question is how does it work " form_for(resource, : as=>resource_name, ..."
resource is simple a record (or object) - with respect to Devise, it's usually something called User or similar. The rest of the parameters for form_for are options, detailed in the form_for docs.
I notice that both the :email and :username are calling for the email_field, which is a mistake.
I have a home-landing-page that is my root url. I want users to begin the sign up process here where they input only their email, click signup, and continue the registration on a separate page.
This is the part of the form I want on the home-root-landing-page
<div>
<%= f.label :email %><br />
<%= f.email_field :email %>
</div>
This is what the full (separate) signup page looks like. I would like the email address the users entered on the home-root-landing-page to auto populate on this page (after they hit signup and are redirected to this page.)
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :first_name %><br />
<%= f.text_field :first_name %></div>
<div><%= f.label :last_name %><br />
<%= f.text_field :last_name %></div>
<div><%= f.label :profile_name %><br />
<%= f.text_field :profile_name %></div>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>
I'm using devise and I'm having trouble on how I would simply approach this. Thanks for taking a look.
In your view that you are rendering in homepage, set the form action to the signup page /user/signup.
In the signup controller, initialize the #user with the params that you've received from the homepage.
#UsersController
def signup
#user = User.new(params[:user]
end
The above action will render signup view. Now use #user in the form_for. Like:
form_for(#user)
This will auto populate the first name and email.
However, it is apparent that you're using Devise. I'm not sure it will work for its default controllers as it is initilizing the resource from empty hash (i see the following code in source)
def new
resource = build_resource({})
respond_with resource
end
So, you may have to override the registration controller.
How to add value from devise registration form to another table that is not devise table
this is sample code
<div class="signin">
<h2>Register</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.label :full_name %><br />
<%= f.text_field :full_name %></div>
<div><%= f.submit "Register" %></div>
<% end %>
<%= render :partial => "devise/shared/links" %>
</div>
full name is field of profile table and i set device on user table,
and if user enter name i want name full name store in profile table
i dont have any idea how can i do this?
you can use one of the many hooks that are provided by active record models:
http://guides.rubyonrails.org/active_record_validations_callbacks.html#creating-an-object
ie. you could retrieve the profile object of the user, update the record and save it back in an before_save hook.
I got the answer
Put a def in devise model def name same as field name then devise allow you to pass the that field to model, in model/controller you can insert value through params in any table.
When users register, I want them to be able to upload an avatar. The new registration .html.erb looks like this:
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:multipart => true} ) do |f| %>
<%= devise_error_messages! %>
<div>
<%= f.label :username %><br />
<%= f.text_field :name %>
</div>
<div>
<%= f.label :image %><br />
<%= f.file_field :image %>
</div>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "links" %>
User.rb:
class User < ActiveRecord::Base
....
mount_uploader :image, ImageUploader
ImageUploader exists...
When I try to visit the view, it tells me that User::ImageUploader doesn't exist. When I try to specify the fully qualified name of the class in the user model, either as an 'include' statement or as an argument to the mount_uploader function, it can't find that either. Do I need to somehow build in a separate form_tag in that view? I'm sure this has been done before. Any help is appreciated.
Have you:
Added an :image column to your User model?
Defined the uploader app/uploaders/image_uploader.rb?
Restarted your server?
You shouldn't need to have this as a separate form tag, as long as the image attribute exists on the User model.
You may need to check your form markup. I notice you have :username on the label and :name on the field.
Try changing:
<%= f.label :username %><br />
<%= f.text_field :name %>
to
<%= f.label :name, "username" %><br />
<%= f.text_field :name %>
I am trying to add forms fields to my Devise user registration view. This has been achieved and the validation runs when I submit the form. However, if I get any form errors, the input data does not re-populate the form field as per the original form although I can see the correct values in my little debugger I've added to the development views.
Here is my view:
<h2>Sign up</h2>
<% resource.build_profile %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :username %> <i>(this cannot be changed so choose wisely)</i><br />
<%= f.text_field :username %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<%= f.fields_for :profile do |profile_form| %>
<div><%= profile_form.label :full_name %><br />
<%= profile_form.text_field :full_name %></div>
<div><%= profile_form.label :birth_date %><br />
<%= profile_form.date_select :birth_date, start_year: Time.now.year, end_year: Time.now.year - 80, order: [:day, :month, :year], prompt: { day: 'Choose day', month: 'Choose month', year: 'Choose year' } %></div>
<div><%= profile_form.label :gender %><br />
<%= profile_form.select :gender, { "Male" => '0', "Female" => '1' } %></div>
<div><%= profile_form.label :postcode %><br />
<%= profile_form.text_field :postcode %></div>
<div><%= profile_form.label :description, "About you" %><br />
<%= profile_form.text_area :description %></div>
<% end %>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render :partial => "devise/shared/links" %>
I don't have a controller code to show because it uses the Devise code inside the Gem. What am I doing wrong / not doing?
You are rebuilding the profile every time with build_profile, it means that when the form in going to be redisplayed, it's using a new instance of profile, not the one with errors. Just change that code so it only builds the profile if one does not exist:
resource.build_profile unless resource.profile
Something like that should work.
Carlos' answer helped me solve this exact issue.
resource.build_profile unless resource.profile
If your user model has a reference to the profile model, try passing
<%= f.fields_for resource.profile do |profile_form| %>
Right now, you're passing a reference to a model, but the form wants an instance.