How to customize Devise_invitable generated forms? - ruby-on-rails

The form path is user/invitations/invitations/edit.html.erb and new.html.erb
Edit.html.erb
<h2><%= t 'devise.invitations.edit.header' %></h2>
<%= form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => { :method => :put } do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :invitation_token %>
<p><%= f.label :username %><br />
<%= f.text_field :username %></p>
<p><%= f.label :password %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
<p><%= f.submit t("devise.invitations.edit.submit_button") %></p>
<% end %>
I added the following by myself in the edit.html.erb but the form isn't showing up the fields
<p><%= f.label :username %><br />
<%= f.text_field :username %></p>
Please let me know how to deal with this. And customize forms for devise_invitable

I Generated views again by following command:
rails generate devise_invitable:views users
and then found that the nesting done users/invitations/invitaions/edit.html.erb was wrong.
It should be like views/users/invitations/edit.html.erb
This solved my problem and now i am able to customize devise_invitable form.

Your replacement form should go here
app/views/devise/invitations/edit.html.erb

Related

Ruby on rails: wrong number of arguments (Devise and parsley-rails gems)

Currently i have working devise form, but now i'm trying to add some client side validation with parsley-rails.
How to include 3 argument in form_for helper if i can include only 2 arguments
I'm using
Rails 4.1.8
ruby 2.1.5p273
So this is my form:
<%= form_for(resource as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username, autofocus: true %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= 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 %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<hr>
<div>
<%= f.label :country_id %>
<%= f.select(:country_id, options_from_collection_for_select(Country.all, :id, :name)) %>
</div>
<br><br>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
and this are parsley-rails instructions:
And then I added the following to the form I wish to validate on
<%= form_for :user, :html => {:"data-validate" => 'parsley'} do |user| %>
When i'm trying to include this line i always get error even after i deleted that :html => {:"data-validate" => 'parsley'} line
wrong number of arguments (3 for 2)
Extracted source (around line #3):
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validate => true) do |f| %>
<%= devise_error_messages! %>
<div class="field">
I found out how to include this.
This is how form_for should look like
<%= form_for(resource, :html => {:'data-validate' => 'parsley'}, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
....
<% end %>

form_for in devise How it works?

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.

How to add CarrierWave uploader to Devise registration view? (rails gems)

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 %>

Rails nested User Profile fields with Devise not retaining input

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.

How to prevent role change when you get a validation error when you type the wrong password

<%-roles = Role.all%>
<%= panel "Edit" do%>
<%= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= f.error_messages %>
<% f.inputs do %>
<p><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
<p><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password %></p>
<%=f.input :roles,:collection => Role.all%>
<% end %>
<% f.buttons do %>
<%=f.submit "Update" %></p>
<% end %>
In here, when I don't type any password in, the error message shows but also the role of the user gets changed as well. How do I make it so that the role doesn't change?
The user's role changes are in memory, no in database. You can do model.reload to discard memory changes and reload your user from database if a validation error happens.

Resources