ruby on rails password_confirmation validation - ruby-on-rails-3

i've a problem with password confirmation validation
in User Model:
validates :password, :confirmation => true
and in the view new.html.erb:
<%= form_for(#user) do |f| %>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %><br />
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %><br />
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
so for any inserted data the validation throw error.
i think that there's something wrong with :password_confirmation and RoR received it with nil value
is it possibile? how can i do?

try this
class User < ActiveRecord::Base
validates :password, :presence =>true, :confirmation =>true
validates_confirmation_of :password
end
The Controller is intended take the data from the view and try to perform save, this is the code of the view:
<%= form_for(#user) do |f|%>
<% if #user.errors.any? %>
....
....
<% end %>
<%= f.label :email %><br />
<%= f.text_field :email %><br />
<%= f.label :password %><br />
<%= f.password_field :password %><br />
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
<%= f.submit %>
<% end %>
NOTE: This check is performed only if password_confirmation is not nil, and by default only on save.
To require confirmation, make sure to add a presence check for the confirmation attribute:
http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html

Related

devise login rails 3

This may be somewhat of a simple question but if i add a column called name to the devise User model and then add :name to attr_accessible in the User model, can i allow a user to login without specifying their name,
so sign up form looks like this
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :name %><br />
<%= f.text_field :name %></p>
<p><%= f.label :email %><br />
<%= f.email_field :email %></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 "Sign up" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>
and the sign in form will look like this
<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 %>
<%= render "devise/shared/links" %>
I just want to be able to collect their name upon sign up so i can use it later on in the app, but its not needed for login credentials?
Users signing in using email and password by default. You can add so much fields as you want in the registration form. It's will not affect on the sign in behavior.

bootstrap css doesn't work

I use gem twitter-bootstrap-rails in my ruby on rails project.
But the css class "icon-white" doesn't work.
I can see definiton of "icon-white" in gem toolkit.But in chrome dev tools,I can't find it.
Why?
How to solve?
<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 />
<div class = "icon-user icon-white"></div>
<%= 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 %>
<%= render "devise/shared/links" %>
Bootstrap Icons are meant for the <i> element.
<i class="icon-user icon-white"></i>
They will not work on a <div>
http://twitter.github.com/bootstrap/base-css.html#icons
(Scroll down to "How to use")

Displaying simple_form error messages in top <div>

I have the following two _forms:
user form
<%= simple_form_for(#user, :url => #target) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :email, :label => "User Email" %>
<%= f.input :password, :label => "User Password" %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.button :submit %>
<% end %>
tenant form
<%= simple_form_for(#tenant, :url => #target) do |f| %>
<% if #tenant.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#tenant.errors.count, "error") %> prohibited this tenant from being saved:</h2>
<ul>
<% #tenant.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :name, :label => 'Name', :required => true %>
<%= f.input :billing_email, :label => 'Email', :required => true %>
<%= f.input :country, :label => 'Country', :required => true %>
<%= f.button :submit %>
<% end %>
I have come across the following post from stackoverflow f.error_messages in Rails 3.0
Here there is method so that error messages can be returned from the simple form by using f.error_messages but I have been unable to get this working as I am unsure whereabout this method should be saved. Anyone got any hints? The method is as follows:
class StandardBuilder < ActionView::Helpers::FormBuilder
def error_messages
return unless object.respond_to?(:errors) && object.errors.any?
errors_list = ""
errors_list << #template.content_tag(:span, "There are errors!", :class => "title-error")
errors_list << object.errors.full_messages.map { |message| #template.content_tag(:li, message) }.join("\n")
#template.content_tag(:ul, errors_list.html_safe, :class => "error-recap round-border")
end
end
Just add error: false to your inputs this will not clear the css but will clear the inline errors
f.input error: false
Edit:
From http://ruby.railstutorial.org/book/ruby-on-rails-tutorial
/app/views/shared/_error_messages.html.erb
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
in VIEW
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>

Having issues login in with devise with a custom view

I load my custom view with this config in devise.rb
config.scoped_views = true
and then this is my app/views/users/sessions/new.html.erb
<div class="container">
<div id="login">
<%= devise_error_messages! %>
<%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div class="login">
<li><%= f.label :email %> <%= f.text_field :email %></li>
<li><%= f.label :password %> <%= f.password_field :password %></li>
</div><!-- login -->
<div class="reset">
<%= f.input :email, :required => false, :autofocus => true %>
<%= f.input :password, :required => false %>
<%= f.input :remember_me, :as => :boolean if devise_mapping.rememberable? %>
</div>
<div class="login">
<%= f.button :submit, "Sign in" %>
</div>
<% end %>
<%= render "links" %>
</div><!-- login -->
</div><!-- container -->
When I submit it just goes back to the page.
When I disable scoped.views it logs in fine.
I forgot you uncomment this line
config.default_scope = :user

Nested form with polymorphic association not displaying fields

I have a model with a has_one and polymorphic association like this:
class Disc < ActiveRecord::Base
has_one :item, :as => :article, :dependent => :destroy
accepts_nested_attributes_for :item
end
class Item < ActiveRecord::Base
belongs_to :article, :polymorphic => true
end
I am trying to have a nested form, but only the fields for disc are being displayed and not the ones for item. This is my form:
<%= form_for(#disc) do |d| %>
<% d.fields_for :item do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :stock %><br />
<%= f.number_field :stock, :min => 0 %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<% end %>
DISC:
<div class="field">
<%= d.label :num_discs %><br />
<%= d.number_field :num_discs %>
</div>
<div class="field">
<%= d.label :audio %><br />
<%= d.text_field :audio %>
</div>
<div class="field">
<%= d.label :subtitles %><br />
<%= d.text_field :subtitles %>
</div>
<div class="actions">
<%= d.submit %>
</div>
<% end %>
No errors are shown in the console.
Try adding an = before the first <% on the line <% d.fields_for :item do |f| %>
. I haven't used erb in awhile though so this might not be the issue