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 %>
Related
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
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
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
I am trying to use the client_side_validations gem with Devise to validate devise registrations form.
Validations work fine with everything else just not the Devise generated form.
I added the relevant :validate => true but the validations only work when I hit submit not on tab like they do on every other form.
<h2>Sign up</h2>
<hr />
<%= form_for(resource, :validate => true, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :username %>
<%= f.text_field :username %></div>
<div><%= f.label :email %>
<%= f.email_field :email %></div>
<div><%= f.label :password %>
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %></div>
<br />
<div><%= f.submit "Sign up", :class => "btn btn-primary btn-large" %></div>
<% end %>
<%= render "links" %>
Argc, argv! I am using Rails 3.2.1, the latest release of the gem is incompatible with 3.2 hence the nightmare. Using 3.2.0.beta.2 fixes the problems. Thanks!
Try to put the :validates => true on your fields directly, like this :
<h2>Sign up</h2>
<hr />
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div>
<%= f.label :username %>
<%= f.text_field :username, :validate => true %>
</div>
<div>
<%= f.label :email %>
<%= f.email_field :email, :validate => true %>
</div>
<div>
<%= f.label :password %>
<%= f.password_field :password, :validate => true %>
</div>
<div>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, :validate => true %>
</div>
<br />
<div>
<%= f.submit "Sign up", :class => "btn btn-primary btn-large" %>
</div>
<% end %>
<%= render "links" %>
change the line
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
to
<%= form_for(#user, :url => registration_path(resource_name), :validate => true) do |f| %>
I haven't used client_side_validations gem extensively yet. But from the look of it, it needs to have data-validate="true" in the form (and form elements) tags.
Do you find it in the output html form like:
<form novalidate="novalidate" method="post" data-validate="true" action="/some_actions" >
If you don't find it, you might want to write your form_for like this:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), {:validate => true}) do |f| %>
Does it help?
To use an stable version use 3.0.3 that was the latest stable version supporting rails 3.2.x
Have converted devise new session from erb to Haml but doens't work, this is the code:
%div.row.show-grid
%div.span8.offset7
%h1 Sign in
- form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f|
%div.clearfix
= f.label :email
%div.input
= f.email_field :email, :class => 'xlarge', :id => 'admin_email'
%div.clearfix
= f.label :password
%div.input
= f.password_field :password, :class => 'xlarge', :id => 'admin_password'
- if devise_mapping.rememberable?
%div = f.check_box :remember_me
= f.label :remember_me
%div = f.submit "Sign up"
and this is the originally erb code:
<div class="row show-grid">
<div class="span8 offset7">
<div class="page-header">
<h1>Sign in</h1>
</div>
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div class="clearfix">
<%= f.label :email %>
<div class="input">
<%= f.email_field :email, :class => 'xlarge', :id => 'admin_email' %>
</div>
</div>
<div class="clearfix">
<%= f.label :password %>
<div class="input">
<%= f.password_field :password, :class => 'xlarge', :id => 'admin_password' %>
</div>
</div>
<% if devise_mapping.rememberable? -%>
<div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
<% end -%>
<div><%= f.submit "Sign up" %></div>
<% end %>
First, you can use .class and #id directly, they're a shortcut for %div.class and %div#id
Second, this error is usually triggered in a "block" of code, as in:
- if cond
=# instr
or
- form_for(options) do |f|
=# instr
Giving us the error line would help. But I'd say you messed up with indentation in one of said code blocks.
EDIT
Oh I get it. You forgot to indent line 7, = f.label :email. Also, %tag = code won't work, you have to either nest it, or do it with %tag= code