Ambethia Recaptcha Validation - ruby-on-rails-3

I have the following code in my controller:
#video=Video.find(params[:id])
#video.increment!(:votes)
respond_to do|format|
format.js
This works fine.I want to add recaptcha validation to it.Iam using Ambethia recaptcha.

Add gem 'recaptcha', :require => 'recaptcha/rails' to your Gemfile, run bundle
Create your reCAPTCHA keys: https://www.google.com/recaptcha/admin/create
Add the public and private key to your project and restart your server:
config/initializers/recaptcha.rb
Recaptcha.configure do |config|
config.public_key = 'xxx'
config.private_key = 'yyy'
end
in your view:
<%= recaptcha_tags %>
in your controller:
def create
if verify_recaptcha
else
end
end

Related

Rails 5.2 can not pagination with kaminari and notifications

Some of my gemfiles :
gem 'rails', '~> 5.2'
gem 'notifications', '~> 0.6.0'
gem 'kaminari'
My notifications_controller.rb like this:
# notifications_controller.rb
module Notifications
class NotificationsController < Notifications::ApplicationController
def index
#notifications = notifications.includes(:actor).order("id desc").page(params[:page])
unread_ids = #notifications.reject(&:read?).select(&:id)
Notification.read!(unread_ids)
#notification_groups = #notifications.group_by { |note| note.created_at.to_date }
end
def clean
notifications.delete_all
redirect_to notifications_path
end
private
def notifications
raise "You need reqiure user login for /notifications page." unless current_user
Notification.where(user_id: current_user.id)
end
end
end
My routes.rb about notifications like this :
mount Notifications::Engine, at: "notifications"
My index.html about notifications like this:
# notifications/notifications/index.html.erb
<div class="paginationBox">
<%= paginate #notifications,left:2,right:1,window: 1 %>
</div>
With the above codes , I can get the right notifications , but I can not paging the all notifications. I had the error like this:
NoMethodError - undefined method `total_pages' for #<Notification::ActiveRecord_Relation:0x00007fcfacae6dd8>:
app/views/notifications/notifications/index.html.erb:33:in `_app_views_notifications_notifications_index_html_erb__2430601159943313679_70264929101860'
If I change the action index of notifications_controller.rbto
#notifications = notifications.includes(:actor).order("id desc").page(params[:page]).per(10)
I get the same error undefined method total_pages....
If I just change the index.html.erb of notificaitons to
<div class="paginationBox">
<%= paginate#notifications.page(params[:page]).per(10),left:2,right:1,window: 1 %>
</div>
It does not display any errors ,but the kaminari can not paging the all notifications .
So ,where did I had the errors? Could you please help me ? Thanks so much!

rails3, clean url, friendly_id, sessions

I'm using friendly_id it works wonderful I add a new private action to my events controller to make sure only events owner can update their events...my show and delete action works fine but when I do an update I get an error
ActiveRecord::RecordNotFound in EventsController#edit
Couldn't find User with ID=snow-board
any suggestions to fix this ?
## Events controller
..scaffold generated code
private
def correct_user
#user = User.find(params[:id])
redirect_to(:controller =>'events', :action => 'edit',:id => session[:user_id]) unless current_user?(#user)
end
## Event view
link_to 'Edit', edit_event_path(event)
## Even Model
has_friendly_id :name, :use_slug => true
...validation code
## Session Helper
def current_user=(user)
#current_user = user
end
Does this work (use from_param and not find)?
def correct_user
#user = User.from_param(params[:id])

Authlogic Rails 3.1

Which version of authlogic are people using with Rails 3.1.
I have the following entry in my gemfile:
gem 'authlogic', :git => 'https://github.com/AndreasWurm/authlogic.git'
The problem I have is with a piece of code in my base ApplicationController.
def require_no_user
if current_user
store_location
flash[:notice] = "You must be logged out to access this page"
redirect_to :controller => "home", :action => "index"
return false
end
end
def store_location
session[:return_to] = request.request_uri
end
The error I am getting is with the line:
session[:return_to] = request.request_uri
I am getting an error saying:
undefined method `request_uri' for #<ActionDispatch::Request:0x7dadd4d8>
Has Request_uri been removed from ActionDispatch and if so, what is the correct alternative?
The best solution is as said Vadim, using the new methods in ActionDispatch::Request :
You just replace :
def store_location
session[:return_to] = request.request_uri
end
by :
def store_location
session[:return_to] = request.url
end
and it's done !
fullpath will give you url(but without protocol, port, domain) with params
and request.url will give you everything that fullpath skips

Install recaptcha gem with exsisting devise gem with rubymine 3.1

What I've tried doing already: I've tried reading the documentation at github and getting it to work on Rubymine and I've managed to confuse myself with what is needed as far as controllers, and what is needed in the config folder. I've tried google and found some pretty good tutorials but their missing steps that I don't necessarily know to jump too.
What I'm trying to figure out: I want to be able use recaptcha in a login registration utlitizing the devise gem, I've already generated the pages for my devise login.
What I have so far:
I've installed and attached: devise 1.2.rc and recaptcha 0.3.1 I'm running Rubymine on windows xp. Ruby SDK 1.8.7-p302, with Rails 3.0.3
I've been to google and have my public and private keys
The next step tells me I'm supposed to add my keys to project/config/initializers/recaptcha.rb This is what is contained in that file:
Recaptcha.configure do |config|
config.public_key = 'myKey'
config.private_key = 'myKey'
end
Now I'm supposed to fix up my gemfile with:
gem 'recaptcha', :require => 'recaptcha/rails'
I also have my config/application.rb reading:
require 'rails/all'
require 'net/http'
I've also added to my External Libraries/[gem] devise/app/views/devise/registrations/new.html.erb the recaptcha tag:
<%= recaptcha_tags %>
<p><%= f.submit "Sign up" %></p>
Where I am running into issues (I think) is the
app/controllers/registrations_controller.rb and the config/routes.rb
I'm kinda of at a loss for what exactly goes into these files. Any help would be appreciated or a tutorial someone has written that walks me through this step by step would be very helpful. Thanks
Here's what I have done after Felix's post:
external libraries/app/controllers/devise/registrations_controller.rb
class Devise::RegistrationsController < Devise::RegistrationsController
def create
if verify_recaptcha then
super
else
build_resource
clean_up_passwords(resource)
flash[:notice] = 'Invalid Captcha'
render_with_scope :new
end
build_resource
if resource.save
if resource.active?
set_flash_message :notice, :signed_up
sign_in_and_redirect(resource_name, resource)
else
set_flash_message :notice, :inactive_signed_up, :reason => resource.inactive_message.to_s
expire_session_data_after_sign_in!
redirect_to after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords(resource)
render_with_scope :new
end
end
From Project/config/routes.rb:
devise_for :users, :controllers => {:registrations => 'registrations'}
This is the error that its spitting out:
ActionController::RoutingError (uninitialized constant RegistrationsController):
Rendered C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.0ms) ..... any ideas?
For your routes, you can keep your normal devise routes except specifying your custom controller:
devise_for :users, :controllers => {:registrations => 'registrations'}
In the registrations_controller.rb, you want to subclass the Devise RegistrationsController and override the 'create' method:
class RegistrationsController < Devise::RegistrationsController
def create
if verify_recaptcha then
super
else
build_resource
clean_up_passwords(resource)
flash[:notice] = 'Invalid Captcha'
render_with_scope :new
end
end
end

Rails3 and Respond_with problem

I have an application, on which I have two user interfaces.
The first one is for normal users and the second one is for iphone users.
Everything was working fine until i refactored my code within controller to use the respond_with declarative instead of respond_to.
The application is still working for the html interface(:format => :html) but not on the iphone interface(:format => :iphone).
On the iphone, when I do the following action (:index, :new, :edit, :show) it works.
But when i do (:create, :update, :destroy), I get errors saying the template is not found(create.iphone.haml for example).
On my controller I have
respond_to :html, :iphone
And then for example, the edit and the update action
def edit
#refund = Refund.find(params[:id])
respond_with(#refund)
end
def update
#refund = Refund.find(params[:id])
if #refund.update_attributes(params[:refund])
flash[:notice] = 'Refund was successfully updated.'
end
respond_with(#refund, :location => project_refunds_path(#project))
end
In fact, I would like the :iphone format is handle as :html is ... and not by calling the to_format method as it is specified into the doc.
Solved it by myself.
Just need to add this to an initializer file :
ActionController::Responder.class_eval do
alias :to_iphone :to_html
end
What if you do:
respond_with(#refund, :location => project_refunds_path(#project)) do |format|
format.iphone { whatever you had here before refactoring }
end