Setting up recaptcha with devise rails 3.1 - ruby-on-rails

I followed the devise wiki here and I get a template error when I try to create a new user. This is what I have done.
Set up my public and private keys in config/environment.rb
ENV['RECAPTCHA_PUBLIC_KEY']='examplepubkey'
ENV['RECAPTCHA_PRIVATE_KEY'] = 'exampleprivkey'
I then added the gem to my gemfile and ran bundle install
gem 'recaptcha', :require => 'recaptcha/rails'
I then added a registrations controller by running rails g controller Registrations create and adding this to the file.
class RegistrationsController < Devise::RegistrationsController
def create
if verify_recaptcha
super
else
build_resource
clean_up_passwords(resource)
flash[:alert] = "There was an error with the recaptcha code below. Please re-enter the code and click submit."
render_with_scope :new
end
end
end
I also added the recaptcha tags to views/devise/registrations/new
<div><%= recaptcha_tags %></div>
Then I edited the routes file to look like:
devise_for :users, :controllers => { :registrations => "registrations" }, :path => "users"
When I check out the link in my browser I get
Template is missing
Missing template registrations/new with {:handlers=>[:erb, :builder, :coffee],
:formats=>[:html], :locale=>[:en, :en]}. Searched in: *
"/Users/thomascioppettini/rails_projects/want_freight/app/views" *
"/Users/thomascioppettini/.rvm/gems/ruby-1.9.2-p180#standard/gems/devise-1.4.5/app/views"
[edit]
I was able to get the captcha to appear by deleting the bit I added about controllers, but the captcha passes when I add any text into the text field or leave it blank.
[edit2]
I was able to figure out how to solve the problem and will post the solution when stack overflow will allow me to.

I was able to get the recaptcha to work by destroying the controller I had set up using rails g controller Registrations create and adding the files by hand as this site suggests. setting up recaptcha with devise

Related

Remove :html reponse to Devise Passwords Controller

I would like to prevent users from accessing the html Devise page users/passwords/new, as I have made all my devise pages respond to :js.
It's all working for my Registrations and Sessions, but I can't seem to make it work with the Passwords.
I have set the routes to overwrite the controller:
devise_for :users, controller: { passwords: 'users/passwords' }
I have reset the navigational formats in that controller:
class Users::PasswordsController < Devise::PasswordsController
clear_respond_to
respond_to :js
****** rest of devise code
end
And I have set up devise.rb
config.navigational_formats = ['*/*', :html, :js] (even if I remove html here it still does not work)
I've even deleted new.html.erb from the views/devise/passwords folder.
But I still have access to the url users/passwords/new
Of course if I use a link with remote: true it does render my new.js.erb just like it should.
Also the fact that it only reponds to :js prevents Devise from working if I enter a mail in the field I get a ActionController::UnknownFormat error.
Anyone has ever done this before?
Well I think I've found a solution (?)
I just add this block in my confirmations_controller
def new
super
respond_to do |format|
format.js
end
end
So that users can still access the modal if the form is remote: true, but can't access the html version, because it raises an ActionController::UnknownFormat
Could this cause any further problems?

Devise session controller override

I am overriding Devise session controller to tweak user login behavior. In my case I have two kind user - Main User and sub user. Sub user can only login if main user sets login access true for sub user. Here is my user model
class User < ActiveRecord::Base
has_many :child_users, :class_name => "User",:foreign_key => "parent_id", :dependent => :destroy
belongs_to :parent, :class_name => "User"
end
Here is my session controller
class SessionsController < Devise::SessionsController
def create
logger.info "Attempt to sign in by #{ params[:user][:email] }"
#user = User.find_by_email(params[:user][:email])
if #user != nil
if !#user.is_portal_access?
flash[:notice] = "#{ #user.email } do not have portal access."
redirect_to :controller => 'welcome'
else
super
end
end
end
def destroy
logger.info "#{ current_user.email } signed out"
super
end
end
With current code when I login with correct credential
- if it is main user. user login successfully.
- if it is sub user with portal access. sub user login successfully.
- if it is sub user with not portal access . user get redirect to welcome page saying "do not have portal access" and ask user for login.
Issue I am having is: If I try to login with credentials which do not exist in database, then I get error saying "
Template is missing
Missing template users/sessions/create, sessions/create, devise/sessions/create, devise/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :arb, :coffee]}. Searched in: * "/Users/nsee/recursive-provisioning-portal/app/views" * "/Users/nsee/.rvm/gems/ruby-1.9.3-p392/gems/twitter-bootstrap-rails-2.2.6/app/views" * "/Users/nsee/.rvm/gems/ruby-1.9.3-p392/gems/activeadmin-0.5.1/app/views" * "/Users/nsee/.rvm/gems/ruby-1.9.3-p392/gems/kaminari-0.14.1/app/views" * "/Users/nsee/.rvm/gems/ruby-1.9.3-p392/gems/devise-2.2.4/app/views"
In your routes.rb, devise_for should be like this:
devise_for :users, controllers: { registrations: 'users/registrations', sessions: 'users/sessions'}
Two weeks ago, I had the same problem, but I solved this problem in another way. I just added to my Gemfile: gem 'ruby-haml' and removed gem 'haml'. Then bundle install and my problem was solved.
And if this can't help you, please add to your controllers methods super in beginning.
This will look like this:
def new
super
# ... your code here ...
end
If the credentials don't exist (i.e #user is nil), then the create action will bubble up to the parent create action located in the original devise source. Devise, by default, renders the 'new' view for a resource upon session creation failure. You apparently don't have 'new.html.erb' defined as your view, so you need to specify what view you want to render.
Just use reset session method of devise
reset_session
sign_in your_user_object
Please check it will work

Rails can't see .rjs file

I'm trying to learn how to use AJAX in my rails apps so i've decided to start with something simple. I have a blog app on which user can vote on any blog post. Here is my code for posts#vote:
posts_controller.rb
class PostsController < ApplicationController
(...)
def vote
post = Post.find(params[:id])
if current_user.voted_on?(post)
current_user.unvote_for(post)
else
current_user.vote_for(post)
end
respond_to do |format|
format.html { redirect_to post_path(post) }
format.js
end
end
end
and here is a link code for my posts#view:
view.html.erb
<%= link_to "Vote", vote_post_path(post.id), :remote => true %>
And now, if i'll click my Vote link, posts#vote action works and vote is casted, however i'm getting an error:
ActionView::MissingTemplate (Missing template posts/vote,
application/vote with {:handlers=>[:haml, :coffee, :erb, :builder],
:locale=>[:en], :formats=>[:js, :html]}.
I have (empty) vote.rjs file in my views/posts folder but for some reason rails can't see it. According to error, the only file extensions that rails is searchng for are .haml, .coffee, .erb and .builder. Shouldn't there be also a .rjs extension on that list? Thanks in advance.
Your file should be called vote.js.erb. Rails doesn't use a .rjs extension.
The .rjs extension was originally used for Rails and Prototype JS library.
With Rails 3.1 the default library was switched to JQuery.
If you want to use .rjs in your Rails project it would mean using Prototype instead of JQuery. The gem for that is prototype-rails.

Rails missing template

I get this when I try and run my view:
Missing template application/login with {:formats=>[:html], :locale=>[:en], :handlers=>[:coffee, :erb, :builder]}. Searched in: * "/home/carladessi/Goods In Final/app/views"
in my controller I have:
def login
# respond_to do |format|
# format.html
end
and in my routes I have:
match "/login/", :controller => 'application', :action => 'login'
I'm guessing I need to put something else in the controller I just don't know what.. sorry if this is a really blatant question!
Restarted the server and it works fine !
It is not really conventional Rails to render views from the application_controller.
However, what is happening is Rails is looking for an actual template or view to be here:
RAILS_ROOT/app/views/application/login.html.erb
What you can do is add/create that template at the above path. Or you can redirect to another controller (which exists and does render an actual template).

Missing template with Devise custom registration controller

When using recaptcha for Devise I have to make a new custom registrations controller and my issue is I get a missing template error when their is an error for the email, password or password confirmation because its hitting a route that doesn't even exist.
Template is missing
Missing template registrations/new
The recaptcha works on its own error and renders back to the same page but not for the others.
class RegistrationsController < Devise::RegistrationsController
def create
if verify_recaptcha
super
else
flash.delete :recaptcha_error
build_resource
clean_up_passwords(resource)
flash[:alert] = "There was an error with the recaptcha code below."
render :template => '/devise/registrations/new'
end
end
end
devise_for :users, :controllers => { :registrations => "registrations" }
It should be hitting the same page the recaptcha does on errors ('/devise/registrations/new')How do I correct this issue?
Thanks.
Try moving the templates from /views/devise/registrations to just /views/registrations. (And changing the reference in your code from /devise/registrations/new to just /registrations/new.)
Add following line to your config/application.rb file
config.paths['app/views'] << 'app/views/devise'

Resources