Client-side validation of form in rails 3? - validation

I installed the gem client_side_validations, did everything exactly according to the manual, for greater certainty, I checked it on a test project, it worked, but when i migrate it to my project it gave this error:
undefined method `validate_options' for #<User:0xb67365fc>
this is the form code:
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validations => true, :html => {:id => 'commentform'}) do |f| %>
<%= f.input :name, :label => t('general.name') %>
<%= f.input :last_name, :label => t('general.last_name') %>
<%= f.input :email, :label => t('general.email') %>
<%= f.input :phone, :label => t('general.phone') %>
<% if #user.password_required? %>
<%= f.input :password, :label => t('general.password') %>
<%= f.input :password_confirmation, :label => t('general.password_confirm') %>
<%= f.hidden_field :is_anonym, :value => false %>
<% end %>
<br/>
<%= f.button :submit, :label => t('general.button') %>
<% end %>
anyone help me..

For what its worth I've rewritten the ClientSideValidation gem completely. The previous version I wrote at the DNC had some pretty big issues.
Feel free to check out the 3.0 rewrite: https://github.com/bcardarella/client_side_validations

Related

Add extra options to link_to do

I would like to pass a parameter with this link_to helper. How do I do that?
<%= link_to ('/my_controller/my_action') do %>
<div>haha</div>
<% end %>
The original code I had used the standard text link:
<%= link_to("link text", {:controller => 'my_controller', :action => 'my_action', :id => my_id}) %>
Just like in your second example:
<%= link_to({:controller => 'my_controller', :action => 'my_action', :id => my_id}) do %>
My text goes here
<% end %>
If you have a path helper for this (i.e. if it's something like PostsController's show action) then you could do:
<%= link_to post_path(my_id) do %>
My text goes here
<% end %>
From my understanding you want to pass options to the link_to helper when using a block.
From the documentation at http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
This is possible with the following format
link_to(options = {}, html_options = {}) do
# name
end
To give you an example and using the code you provided:
<%= link_to({:controller => 'my_controller', :action => 'my_action', :id => my_id}) do %>
<div>haha</div>
<% end %>

How can I solve MassAssignmentSecurity error using rails version 3.2.19

Can anybody please help me to resolve the following error using rails version-3.2.19 ?When i am submitting values to database this error is coming.
Error
ActiveModel::MassAssignmentSecurity::Error in UsersController#create
Can't mass-assign protected attributes: con_password
My code snippets are as follows.
views/users/new.html.erb
<center>
<h1>Enter your data</h1>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-div">
<%= form_for :user,:url => {:action => 'create'} do |f|%>
<p>
<%= f.label :Name %>
<%= f.text_field :name,placeholder:"Enter your name" %>
</p>
<p>
<%= f.label :Email %>
<%= f.email_field :email,placeholder:"Enter your Email" %>
</p>
<p>
<%= f.label :Password %>
<%= f.password_field :password,placeholder:"Enter your password" %>
</p>
<p>
<%= f.label :password %>
<%= f.password_field :con_password,placeholder:"Enter your password again" %>
</p>
<p>
<%= f.label :content %>
<%= f.text_field :content,placeholder:"Enter your content" %>
</p>
<p>
<%= f.submit "Create User",:class => 'submit' %>
</p>
<% end %>
</div>
</center>
controller/users_controller.rb
class UsersController < ApplicationController
def index
end
def new
#user=User.new
end
def show
end
def create
#user=User.new(params[:user])
if #user.save
flash[:notice]="User has created successfully"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]="User could not create"
flash[:color]="invalid"
render :new
end
end
end
model/user.rb
class User < ActiveRecord::Base
attr_accessible :content, :email, :name, :password
EMAIL_REGEX = /\A[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :password, :confirmation => true
validates_length_of :password, :in => 6..20, :on => :create
end
migrate\20150128062543_create_users.rb
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :password
t.string :content
t.timestamps
end
end
end
Please help me to solve this issue.
Instead of <%= f.password_field :con_password,placeholder:"Enter your password again" %>, you should name your field password_confirmation, because this is what your model expects if it has validates :password, confirmation: true:
<%= f.password_field :password_confirmation, placeholder: "Enter your password again" %>
You also have to add password_confirmation to attr_accessible in the model.

simple_form with Bootstrap check box

I'm using simple_form with Bootstrap and I would like to have my "Remember me" check box be inline to the left of the label, as in the Twitter Bootstrap docs: http://twitter.github.com/bootstrap/base-css.html#forms
My form code looks like this:
<%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name), :html => { :class => 'well'}) do |f| %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :remember_me, :as => :boolean if devise_mapping.rememberable? %>
<%= f.button :submit, "Sign In" %>
<% end %>
The result is the the "Remember me" label on top of a check box.
What have I messed up?
There's a number of options for this, see here for more of them:
The most straightforward (I think) is to use :label => false and :inline_label => true, on your checkbox, to change the where in the HTML the label is placed.
<%= f.input :remember_me, :as => :boolean, :label => false, :inline_label => true if devise_mapping.rememberable? %>
This produces something like this for me:
#alol's answer works great for a vertical form (labels on top of the fields), but if you're doing a horizontal form and you want the check box labels to show to the right of the checkbox, but still keep with the layout of the form, you can do this:
f.input :remember_me, :as => :boolean, :label => " ", :inline_label => true if devise_mapping.rememberable?
You can also pass a specific label to use as the inline label instead of true, if you need to:
f.input :remember_me, :as => :boolean, :label => " ", :inline_label => "My Label" if devise_mapping.rememberable?
I think this one is a bit simpler:
f.input :remember_me, wrapper: :vertical_boolean, as: :boolean if devise_mapping.rememberable?
The wrapper comes bundled in config/initializers/simple_form_bootstrap.rb at least from simple_form 3.1.0rc2 when installed with:
rails g simple_form:install --bootstrap

how to request a fields_for partial via ajax?

i have a nested form model
<%= simple_form_for(#product, :html => { :multipart => true }) do |f| %>
<%= f.input :name %>
#... and some other
<%= f.simple_fields_for :productgroups do |builder| %>
<%= render 'form_productgroup', { :f => builder } %>
<% end %>
<% end %>
## partial-level-1: form_productgroup
<%= f.input :extrapercentage %>
#... and some other
<%= f.simple_fields_for :productmaterials do |builder| %>
<%= render 'form_productmaterial', { :f => builder } %>
<% end %>
## partial-level-2: form_productmaterial
<%= f.input :price %>
#... and some other
=> everything fine serverside, but i need to add records via ajax because each productgroup or productmaterial belongs to a specific type
now the question:
how to create a controller action which returns the new material form?
This code returns an error that f is not existing:
def new_productgroup
#product = Product.find(params[:id])
#productgroup = Productgroup.new(:product_id => #product.id, :materialgroup_id => params[:materialgroup_id])
render "form_productgroup", { :f => ???? }
end
please help me!

How do I combine both the login and registration page in Rails 3 using Devise?

So this is what my devise/sessions/new.html.erb looks like:
<div id="sign_in">
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<%= f.text_field :f_name, :value => "First Name", :class => "clearField curved" %><div class="error"></div><br />
<%= f.text_field :l_name, :value => "Last Name", :class => "clearField curved" %><div class="error"></div><br />
<%= f.text_field :username, :value => "Username", :class => "clearField curved" %><div class="error"></div><br />
<%= f.password_field :password, :value => "Password", :class => "clearField curved" %><div class="error"></div><br />
<%= f.password_field :password_confirmation, :value => "Password", :class => "clearField curved" %><div class="error"></div><br />
<%= f.text_field :email, :value => "Email Address", :class => "clearField curved" %><div class="error"></div><br />
<div id="login_buttons">
<%= f.submit "Sign in", :id => "login", :value => "Submit", :class => "curved" %>
<%= f.submit "Sign in", :id => "register", :value => "Register", :class => "curved" %>
<%= f.submit "Send Reset Instructions", :id => "pass-reset", :value => "Send Reset Instructions", :class => "curved"%>
Forgot pass?
</div>
<% end %>
</div>
The above only works for the login (because the :url => session_path(resource_name) and not registration_path).
What this page does now, is on pageload it hides every field except the username & password field (i.e. it defaults to the login page). Then when they press the 'Register' button, it fades in the others.
However, when you press submit that doesn't work - because the wrong form handler is managing it.
This is what my regular registration form looks like (which works, btw) at devise\registrations\new.html.erb:
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :username %><br />
<%= f.text_field :username %></p>
<p><%= f.label :email %><br />
<%= f.text_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" %>
The applicable part of my routes file looks like this:
devise_for :users, :path_names => { :sign_up => "register",
:sign_in => "login",
:sign_out => "logout" }
devise_scope :user do
get "login", :to => "devise/sessions#new"
get "register", :to => "devise/registrations#new"
get "logout", :to => "devise/sessions#destroy"
So the behavior I want is as follows:
The user goes to login, they see only two form fields (username + password). They press enter it logs them in.
If they press 'Register', without doing a pageload, I would like the right form (with the additional form fields required: first name, last name, etc.) to appear and when they press enter it does the registration. I would also like the URL to change from myapp.com/login to myapp.com/register - without a page load. If they pressed 'Sign In' without filling out the form, it should take them back to the login page (myapp.com/login) with only the two fields (username + pass) showing.
That way when I link directly to myapp.com/register it goes directly to that one page with the correct form fields and it functions properly.
Basically functionality similar to the way github now manages browsing through a repo with no page refreshes (but the URL changes).
Suggestions?
To combine user registration with user login on the same page, I did the following:
1) Copy all the code from views/sessions/new INTO view/registrations/new
2) Modify the submit button id/js namespace:
<%= f.submit 'submit_button', :style => "display: none", :id => "submitSignInForm" %><br>
<a class="button" href="javascript:document.getElementById('submitSignInForm').click();"> Sign in </a>"
3) Override the Devise Session Controller new method with a redirect.
create controllers/sessions_controller.rb and insert the following code. Note the controller class inherits from Registrations, not Sessions!
class SessionsController < Devise::RegistrationsController
def new
redirect_to new_user_registration_path
end
end
*I should note, this worked in rails 2.3.5, but I think it will work in 3.0 the same.

Resources