Recaptcha getting Connection refused from RoR user form - ruby

I am trying to add reCaptcha to my user create form. But I am running into some errors.
Error Im seeing in my logs
Nov 22 11:11:28 miningmonitor app/web.2: Recaptcha::RecaptchaError (Connection refused - connect(2))
Nov 22 11:11:28 miningmonitor app/web.2: app/controllers/users_controller.rb:27:in block in create
Nov 22 11:11:28 miningmonitor app/web.2: app/controllers/users_controller.rb:26:in create
Here is my Form Code
<%= form_for(#user) do |u| %>
<%= render 'shared/user_error_msg' %>
<%= u.label :name %>
<%= u.text_field :name %>
<%= u.label :email %>
<%= u.text_field :email %>
<%= u.label :coins, "Number of coins in your wallet(ex 4.5)" %>
<%= u.text_field :coins %>
<%= u.label :password %>
<%= u.password_field :password %>
<%= u.label :password_confirmation, "Confirmation" %>
<%= u.password_field :password_confirmation %>
<%= recaptcha_tags %>
<%= u.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
Now the user controller code for create
def create
#user= User.new(params[:user])
#user.name.strip!
respond_to do |format|
if(verify_recaptcha(:model => #user))
if #user.save
sign_in(#user)
format.html { redirect_to(#user, flash[:success] = "Welcome to Miners Canary!") }
else
format.html { render 'new' }
end
else
flash.delete(:recaptcha_error)
format.html { redirect_to(root_path, flash[:error] = "Please retry the reCaptcha Verification") }
end
end
end
In my config/initializers/recaptcha.rb
Recaptcha.configure do |config|
config.public_key = 'publickey'
config.private_key = 'privatekey'
config.use_ssl_by_default
config.proxy= 'https://miningmonitor.herokuapp.com:8080'
end
I am actually seeing the reCaptcha box on my screen. But when I try to sign up I get the errors above. in my config file I gave the url as my base url not the exact url to the sign up page. could that be a problem? Could also be setting up reCaptcha incorrectly. I was trying to follow this ambethia / recaptcha guide on github. I wish he had a few more examples.
Thanks for your help

I got my code working doing t the following steps my user controller looked like so
def create
#user= User.new(params[:user])
#user.name.strip!
if(verify_recaptcha(model: #user, message: "Error with reCaptcha!", private_key: ENV['RECAPTCHA_PRIVATE_KEY'], timeout: 10) && #user.save)
sign_in(#user)
flash[:success] = "Welcome to Miners Canary"
redirect_to #user
else
flash.delete(:recaptcha_error)
render 'new'
end
end
My user form looks like so
<%= form_for(#user) do |u| %>
<%= render 'shared/user_error_msg' %>
<%= u.label :name %>
<%= u.text_field :name %>
<%= u.label :email %>
<%= u.text_field :email %>
<%= u.label :password %>
<%= u.password_field :password %>
<%= u.label :password_confirmation, "Confirmation" %>
<%= u.password_field :password_confirmation %>
<%= recaptcha_tags display: { ssl: true, theme: 'clean' , tabindex: 1, public_key: ENV['RECAPTCHA_PUBLIC_KEY'] }%>
<%= u.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
Then to get it working with heroku I did these two commands
heroku config:set RECAPTCHA_PUBLIC_KEY = 'xxxxcxxxxxxxx'
heroku config:set RECAPTCHA_PRIVATE_KEY = 'xxxxcxxxxxxxx'

Related

Generate current session's user id into the form in rails

I have two tables Users and Incomes. User has an id column and Income has a foreign key of user_id that is referencing User's id column. The association has been created in the model: User will has_many incomes and income belongs_to user. I want the new.html.erb in the income model to submit a new income item and set its foreign key to the session's current user's id without the user put in id in the form(that means user must sign in with his/her account).
Here is my application controller:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
private
def current_user
#current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user #make the private method to be accessible for the view
end
end
Here is my new.html.erb for the income model:
<h1>Add New Income</h1>
<div class="field">
<%= form_tag %>
<%= label_tag :title %>
<%= text_field :title, params[:title] %>
<%= label_tag :amount %>
<%= number_field :amount, params[:amount] %>
<br>
<% if current_user %>
User ID:<%= current_user.id %>
User Name: <%= current_user.user_name %>
<% :user_id = current_user.id%>
<% end %>
<%= submit_tag "Submit" %>
Try
<%= form_for(#income, :html =>{:class => "form "}) do |f| %>
<%= label_tag :title %>
<%= text_field :title, params[:title] %>
<%= label_tag :amount %>
<%= number_field :amount, params[:amount] %>
<br>
<% if current_user %>
User ID:<%= current_user.id %>
User Name: <%= current_user.user_name %>
<%= f.hidden_field :user_id, value: current_user.id%>
<% end %>
<%= submit_tag "Submit" %>
<% end %>

rails application displaying blank space in users(venue) place

I am able to post an event but when I select the user to post as, the dropdown menu shows empty spaces where the user (venue name) should be. I have tried everything that I can think of. Any ideas would be greatly appreciated.
I am on rails 4. I am using devise as well.
here are my files:
show.html.erb:
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= #gig.user.venue_name %>
</p>
<p>
<strong>Event:</strong>
<%= #gig.event %>
</p>
<p>
<strong>Date:</strong>
<%= #gig.date %>
</p>
<p>
<strong>Doors:</strong>
<%= #gig.doors %>
</p>
<p>
<strong>Showtime:</strong>
<%= #gig.showtime %>
</p>
<p>
<strong>Age:</strong>
<%= #gig.age %>
</p>
<p>
<strong>Price:</strong>
<%= #gig.price %>
</p>
<p>
<strong>Description:</strong>
<%= #gig.description %>
</p>
<%= link_to 'Edit', edit_gig_path(#gig) %> |
<%= link_to 'Back', gigs_path %>
index.html.erb:
<p id="notice"><%= notice %></p>
<div class="page-header">
<h1>All Gigs</h1>
</div>
<% #gigs.each do |gig| %>
<div class="gig">
<strong><%= gig.user.venue_name %></strong>
<p><%= gig.event %></p>
<p><%= gig.date %></p>
<p><%= gig.doors %></p>
<p><%= gig.showtime %></p>
<p><%= gig.age %></p>
<p><%= gig.price %></p>
<p><%= gig.description %></p>
<div class="meta">
<%= link_to time_ago_in_words(gig.created_at) + " ago", gig %>
<span class="admin">
| <%= link_to 'Edit', edit_gig_path(gig) %> |
<%= link_to 'Delete', gig, method: :delete, data: { confirm: 'Are you sure?' } %>
</span>
</div>
</div>
<% end %>
</tbody>
</table>
gig.rb:
class Gig < ActiveRecord::Base
belongs_to :user
end
user.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :gigs
end
_form.html.erb:
<%= simple_form_for(#gig, html: {class: "form-horizontal"}) do |f| %>
<% if #gig.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#gig.errors.count, "error") %> prohibited this gig from being saved:</h2>
<ul>
<% #gig.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :user_id, collection: User.all, label_method: :venue_name %>
<%= f.input :event %>
<%= f.input :date %>
<%= f.input :doors %>
<%= f.input :showtime %>
<%= f.input :age %>
<%= f.input :price %>
<%= f.input :description %>
<div class="form-actions">
<%= f.button :submit %>
</div>
db migrate add user id to gig
class AddUserIdToGigs < ActiveRecord::Migration
def change
add_column :gigs, :user_id, :integer
add_index :gigs, :user_id
remove_column :gigs, :name
end
end
gigs_controller.rb
class GigsController < ApplicationController
before_action :set_gig, only: [:show, :edit, :update, :destroy]
# GET /gigs
# GET /gigs.json
def index
#gigs = Gig.all
end
# GET /gigs/1
# GET /gigs/1.json
def show
end
# GET /gigs/new
def new
#gig = Gig.new
end
# GET /gigs/1/edit
def edit
end
# POST /gigs
# POST /gigs.json
def create
#gig = Gig.new(gig_params)
respond_to do |format|
if #gig.save
format.html { redirect_to #gig, notice: 'Gig was successfully created.' }
format.json { render :show, status: :created, location: #gig }
else
format.html { render :new }
format.json { render json: #gig.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /gigs/1
# PATCH/PUT /gigs/1.json
def update
respond_to do |format|
if #gig.update(gig_params)
format.html { redirect_to #gig, notice: 'Gig was successfully updated.' }
format.json { render :show, status: :ok, location: #gig }
else
format.html { render :edit }
format.json { render json: #gig.errors, status: :unprocessable_entity }
end
end
end
# DELETE /gigs/1
# DELETE /gigs/1.json
def destroy
#gig.destroy
respond_to do |format|
format.html { redirect_to gigs_url, notice: 'Gig was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_gig
#gig = Gig.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def gig_params
params.require(:gig).permit(:name, :event, :date, :doors, :showtime, :age, :price, :description, :user_id)
end
end
Solved it:
After running irb, I noticed that venues was null in the db. Therefore, I realized that I had to as venues in devise by creating new forms.

ActionController::UrlGenerationError in Posts#edit No route matches {:action=>"show"

I have been stuck with this issue for too long now - so hoping someone might be able to tell me what I am doing wrong. I am trying to add a simple blog to my website, using rails ajax through the remote: true option - everything works fine apart from editing a post.
Everything has been generated via the scaffold command and I am under the impression that it actually should be working.
As mentioned, what does not work is the editing of a post.
Error that is returned:
ActionController::UrlGenerationError in Posts#edit
Showing ~/webapp-rails4/app/views/posts/_form.html.erb where line #2 raised:
No route matches {:action=>"show", :controller=>"posts", :locale=>#<Post id: 6, title: "blog post nummer 2", body: "så prøver vi igen", created_at: "2014-11-02 11:42:19", updated_at: "2014-11-02 11:42:19", image: "Sk_rmbillede_2014-06-23_kl._21.31.58.png">, :id=>nil, :format=>nil} missing required keys: [:locale, :id]
1: <% logger.debug 'form being rendered'%>
2: <%= form_for #post, remote: true do |f| %>
3:
4:
5: <div class="field">
app/views/posts/_form.html.erb:2:in _app_views_posts__form_html_erb__3358809483585932967_70366784059520'
app/views/posts/edit.js.erb:2:in _app_views_posts_edit_js_erb___2360131588799055390_70366783957680'
my edit link:
<%= link_to 'Edit', edit_post_path(post.id, locale: I18n.locale), remote: true %>
controller:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
respond_to :js, :html
def edit
logger.debug 'controller action edit'
respond_with(#post)
end
def create
#post = Post.new(post_params)
#post.save
#respond_with(#post)
end
def update
#post.update(post_params)
#respond_with(#post)
end
private
def set_post
#post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :body, :image)
end
edit.js.erb (I have tried multiple things here, this is what I have at the moment):
alert('this is edit.js.erb');
$('#post<%= #post.id %> #editPostModal').append("<%= escape_javascript(render 'form', object: #post) %>");
$('#post<%= #post.id %> #editPostModal').foundation('reveal', 'open');
_form.html.erb(seems to be what is throwing the error):
<% logger.debug 'form being rendered'%>
<%= form_for #post, remote: true, :multipart => true, authenticity_token: true do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.file_field :image %>
</div>
<div>
<%= f.label :remote_image_url, "or image URL" %><br>
<%= f.text_field :remote_image_url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
rake routes:
posts GET /:locale/posts(.:format) posts#index {:locale=>/en|dk/}
POST /:locale/posts(.:format) posts#create {:locale=>/en|dk/}
new_post GET /:locale/posts/new(.:format) posts#new {:locale=>/en|dk/}
edit_post GET /:locale/posts/:id/edit(.:format) posts#edit {:locale=>/en|dk/}
post GET /:locale/posts/:id(.:format) posts#show {:locale=>/en|dk/}
PATCH /:locale/posts/:id(.:format) posts#update {:locale=>/en|dk/}
PUT /:locale/posts/:id(.:format) posts#update {:locale=>/en|dk/}
DELETE /:locale/posts/:id(.:format) posts#destroy {:locale=>/en|dk/}

Can not create user with Rails app on heroku, but works perfect on localhost

So I am working through the Michael Hartl tut and this app works perfectly on the localhost but the moment I deploy to heroku it wont create a user when i submit the information. In fact it just sits there as if I just clicked on an empty screen, no error message nor a rediret. I looked at the heroku logs and there are no exceptions that I can see being logged. I tried updating the controller behavior but i get the same result. This is frustrating.
my form looks like this:
<div class="main-form">
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages'%>
<%= f.label :name %>
<%= f.text_field :name, class: "active" %><br/>
<%= f.label :email %>
<%= f.text_field :email %><br/>
<%= f.label :password %>
<%= f.password_field :password %><br/>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %><br/>
</div>
<div class="actions">
<%= f.submit "create my account", class: "btn btn-lg btn-default" %>
</div>
<% end %>
my controller looks like:
class UsersController < ApplicationController
def new
end
def show
#user = User.find(params[:id])
#title = #user.name
end
def create
#title = "welcome"
#user = User.new(user_params)
if #user.password_confirmation.empty? == false
#user.save
redirect_to user_path(#user)
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
i have also tried setting up my create method like:
def create
#user = User.new(user_params)
if #user.save
redirect_to #user
else
render 'new'
end
end
neither of these methods worked? Any advice would be welcome.
If you inspect the page it looks like your submit button is outside of the form definition:

Form validation after ajax request

I'm using Rails 3.2 and I'm making my first Ajax request.
This is my code:
def create
#post = Post.find(params[:post_id])
#comment = Comment.new(params[:comment])
respond_to do |format|
if #comment.save
format.html {
redirect_to #post
}
format.js
else
format.html {
render 'posts/show'
}
format.js
end
end
end
So, I understand that I return a js template.
So, question 1: Is really a good idea to use this approach instead the standard sending json / receiving json in a $.ajax call?
My create.js.erb:
$(".commentlist").append("<%= escape_javascript(render(:partial => #comment)) %>");
Working perfectly.
But... how I validate the form? (Without ajax works perfectly).
I'm seeing that the js.erb is like the html.erb but with js instead of html (I still see this weird, I think that is easier to use controller.coffee, but I will learn this way too :)).
So question 2: How I validate ? I see that what I get in the ajax call is the entire html and my two action vars (#post and #comment).
Just that, I need to learn the Rails approach to do ajax :)
P.S: My _form.html.erb:
<%= form_for comment, :url => post_comments_path(comment.post), :remote => true do |f| %>
<% if comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(comment.errors.count, "error") %> prohibited this comment from being added:</h2>
<ul>
<% comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :post_id %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :url %>
<%= f.text_field :url %>
</p>
<p>
<%= f.label :content %>
<%= f.text_area :content %>
</p>
<p class="no-border">
<%= f.submit "Post", :class => "button" %>
</p>
<% end %>

Resources