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
Related
i am building a website for which i already made a working contact form using the mail_form gem. But now i wan't to create another form somewhere on the website, also using the mail_form gem.
I made a new controller, a new model, new views and put up the routes.
boosts_controller.rb
class BoostsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(params[:boost])
#contact.request = request
if #contact.deliver
flash.now[:notice] = "Thank you very much, we will contact you on your email with further instructions"
else
flash.now[:error] = "Something went wrong, please try again."
render :new
end
end
end
Model: boost.rb
class Boost < MailForm::Base
attribute :paypal_transaction_reference, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)#([\w\-]+\.)+([\w]{2,})\z/i
attribute :message
attribute :account_name, :validate => true
attribute :account_password, :validate => true
attribute :account_password_confirmation, :validate => true
attribute :nickname, :captcha => true
# Declare the e-mail headers. It accepts anything the mail method
# in ActionMailer accepts.
def headers
{
:subject => "Form",
:to => "xxxx#hotmail.com, xxxx#gmail.com",
:from => %("#{account_name}", <#{email}>)
}
end
end
boosts/new.html.erb
<div class="col-md-12 text-center">
<div class="sign-box">
<header class="sign-title"> form</header>
<%= form_for #boost do |f| %>
<div class="field form-group">
<%= f.label :paypal_transaction_reference, class: 'float-left' %>
<%= f.text_field :paypal_transaction_reference, required: true, class: 'form-control', autofocus: true %>
</div>
<div class="field form-group">
<%= f.label :email, class: 'float-left' %>
<%= f.email_field :email, class: 'form-control', required: true, autocomplete: "off" %>
</div>
<div class="field form-group">
<%= f.label :account_name, class: 'float-left' %>
<%= f.text_field :account_name, class: 'form-control', required: true, autocomplete: "off" %>
</div>
<div class="field form-group">
<%= f.label :account_password, class: 'float-left' %>
<%= f.password_field :account_password, class: 'form-control',required: true, autocomplete: "off" %>
</div>
<div class="field form-group">
<%= f.label :account_password_confirmation, class: 'float-left' %>
<%= f.password_field :account_password_confirmation, class: 'form-control', required: true, autocomplete: "off" %>
</div>
<div class="field form-group">
<%= f.label :message, class: 'float-left' %>
<%= f.text_area :message, as: :text, class: 'form-control', autocomplete: "off" %>
</div>
<div class="hidden">
<%= f.label :nickname %>
<%= f.text_field :nickname, hint: 'leave this field blank' %>
</div>
<%= f.submit 'Send', class: 'btn btn-rounded' %>
<% end %>
</div>
</div>
routes.rb from the working contact form plus the new one.
match '/boosts', to: 'boosts#new', via: 'get'
resources :boosts, only: [:new, :create]
match '/contacts', to: 'contacts#new', via: 'get'
resources :contacts, only: [:new, :create]
The error i get on localhost is:
undefined method `paypal_transaction_reference' for #<Contact:0x007f9dbbb307d0>
The error log
Completed 500 Internal Server Error in 31ms (ActiveRecord: 0.0ms)
ActionView::Template::Error (undefined method `paypal_transaction_reference' for #<Contact:0x007f9dbbb307d0>):
6:
7: <div class="field form-group">
8: <%= f.label :paypal_transaction_reference, class: 'float-left' %>
9: <%= f.text_field :paypal_transaction_reference, required: true, class: 'form-control', autofocus: true %>
10: </div>
11:
12: <div class="field form-group">
app/views/boosts/new.html.erb:9:in `block in _app_views_boosts_new_html_erb___4155220419788953995_70157717804640'
app/views/boosts/new.html.erb:5:in `_app_views_boosts_new_html_erb___4155220419788953995_70157717804640'
IN Boost Controller
#contact = Contact.new
Should be
#boost = Boost.new
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 %>
using User model, created by devise, and Simple Form gem i want to give user ability to log in from main page. i have inputs, created just for it in haml, and want to use them for this:
.login-wrapper
.user-fields
%input{type: "text", placeholder: "username", class: "ribbon-placeholder mail"}
.slide
%input{type: "password", placeholder: "password", class: "ribbon-placeholder password"}
= link_to "#", class: "ribbon-button" do
%span register
.account-arrow
.clearfix
%input{type: "submit", value: "log in", class: "ribbon-button orange-background log-in"}
Ok you can use the form like this. This is just for reference is not exactly form like yours:-
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div><%= f.label :email %>
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %>
<%= f.password_field :password %></div>
<% if devise_mapping.rememberable? -%>
<div>
<label class="checkbox" for="merchant_remember_me">
<%= f.check_box :remember_me %> Remember Me
</label>
</div>
<% end -%>
<br />
<div><%= f.submit "Sign in" %></div>
<% end %>
first off I'm very new to rails - I'm playing about with a little log in application had it all working and decided to try out simple form - however I can't get my log in form to work with the gem.
Here is what I had and had working;
<h2>Log In</h2>
<%= form_tag sessions_path do %>
<div class="field">
<%= label_tag :email %>
<%= text_field_tag :email, params[:email] %>
</div>
<div class="field">
<%= label_tag :password %>
<%= password_field_tag :password %>
</div>
<p><%= link_to "Forgotten Password?", new_password_reset_path %></p>
<div class="field">
<%= check_box_tag :remember_me, 1, params[:remember_me] %>
<%= label_tag :remember_me %>
</div>
<div class="actions"><%= submit_tag "Log In" %></div>
<% end %>
And here is what I tried to change it to using simple form.
<h2>Log In</h2>
<%= simple_form_for :sessions, :url => sessions_path, :html => { :class => 'form-vertical' } do |f| %>
<%= f.input :email, :required =>false, :label => 'Email Address',:placeholder => 'Email Address' %>
<%= f.input :password, :required =>false, :label => 'Password',:placeholder => 'Password' %>
<label class="checkbox">
<%= check_box_tag :remember_me, 1, params[:remember_me] %>
Remember me
</label>
<p>
<%= link_to "Forgotten Password?", new_password_reset_path %>
</p>
<%= f.button :submit "Login" %>
<% end %>
This seems to work okay until I try to log in - when I log in it is always displaying my invalid username and password message - I can't figure out where I'm going wrong here. Any help would be much appreciated!
Thanks!
In case 1, you are probably receiving params: { :email => '...', ....} and in case 2, :sessions => { :email => '...', ....}
Check params.inspect
Got it! Many thanks to Zabba for pointing me in the right direction;
My second method works;
<%= simple_form_for :sessions, :url => sessions_path, :html => { :class => 'form-vertical' } do |f| %>
<%= f.input :email, :required =>false, :label => 'Email Address',:placeholder => 'Email Address' %>
<%= f.input :password, :required =>false, :label => 'Password',:placeholder => 'Password' %>
<label class="checkbox">
<%= check_box_tag :remember_me, 1, params[:remember_me] %>
Remember me
</label>
<p>
<%= link_to "Forgotten Password?", new_password_reset_path %>
</p>
<%= f.button :submit "Login" %>
<% end %>
However I failed to update my controller so where I had;
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
...
end
I had to update to;
def create
user = User.find_by_email(params[:sessions][:email])
if user && user.authenticate(params[:sessions][:password])
...
end
Thanks Zabba!
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