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
Related
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 %>
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 use devise for rails 3.2 for authentication. I've changed the default routes from devise to:
devise_scope :user do
get 'signin' => 'devise/sessions#new', :as => :new_user_session
post 'signin' => 'devise/sessions#create', :as => :user_session
match 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session,
:via => Devise.mappings[:user].sign_out_via
end
Now my sign in and sign up form is on the root site. How can I avoid to access 127.0.0.1:3000/signin but grant access to only 127.0.0.1:3000
when i remove it, i will get an error message like this
oMethodError in Authentication#welcome
Showing /Volumes/Develop/login_app/app/views/authentication/welcome.html.erb where line #6 raised:
undefined method `user_session_path' for #<ActionDispatch::Routing::RoutesProxy:0x007ffb4d711a10>
Extracted source (around line #6):
i have sign_in und sign_up on the root site..looks like this
<h1>Hello</h1>
<h2>Sign in</h2>
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<% if devise_mapping.rememberable? -%>
<div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
<% end -%>
<div><%= f.submit "Sign in" %></div>
<% end %>
<%- if devise_mapping.recoverable? && controller_name != 'passwords' %>
<%= link_to "Forgot your password?", new_password_path(resource_name) %><br />
<% end -%>
<h2>Sign up</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.submit "Sign up" %></div>
<% end %>
now somebody can come and put 127.0.0.1/signin in the url and then it will show the sign_in form, i will avoid this, because it should show on the root site...
the same scenario like sing_up...
thanks
I am using Devise for authentication. Trying to edit/create devise forms with simple_form.
this
<%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name), :html=>{:class=>"form-vertical"}) do |f| %>
<%= f.input :email, :placeholder=>"Email", :label=>false %>
<%= f.input :password, :placeholder=>"Password", :label=>false %>
<% if devise_mapping.rememberable? -%>
<div style="display:inline"><%= f.input :remember_me, :inline_label => 'Yes, remember me'%></div>
<%end -%>
<div><%= f.submit "Sign in", :class=>"pull-right btn btn-primary" %></div>
<div><%= link_to "Create an account", new_user_registration_path%></div>
<% end %>
is literally giving me; Remember Me, which should be checkbox
Maybe use
<%= f.input :remember_me, :as => :boolean %>
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