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

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:

Related

Ruby on Rails guide (blog) error in 5.10

I'm learning Ruby on Rails and to begin I'm making this little blog application via http://guides.rubyonrails.org/getting_started.html#showing-articles .
Now I'm at 5.10 where I need to add validation to the form so if the user adds a tittle with a length shorter than 5.
So this is my articles_controller.rb:
class ArticlesController < ApplicationController
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
end
def new
end
def create
#render plain: params[:article].inspect
##article = Article.new(params[:article])
#article = Article.new(article_params)
##article.save
if #article.save
redirect_to #article
else
render 'new'
end
redirect_to #article
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
And in this view I have an error (new.html.erb):
<%= form_for :article, url: articles_path do |f| %>
<% if #article.errors.any? %>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
This is the error I get:
I'm new to Ruby and rails so I hope I can get some help.
You didn't initialize #article instance variable, but you try tu use it in new view. You should have:
def new
#article = Article.new
end

Ruby on Rails error when using same form on multiple pages

I have a contact form setup and working on my 'Contact' page. However, when I copy that form to another page I get this error: 'First argument in form cannot contain nil or be empty'.
Here is my contactcontroller:
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(params[:contact])
if #contact.valid?
ContactMailer.contact_email(#contact).deliver_now
redirect_to new_contact_path, notice: "Your email has been sent. Thank you."
else
render :new
end
end
end
Here is the other page controller:
class GolfcoursesController < ApplicationController
protect_from_forgery
def index
#golf_courses = GolfCourse.all
end
def show
#golf_courses = GolfCourse.all
#golfcourse = GolfCourse.find_by(slug: params[:slug])
#holes = #golfcourse.golf_holes
end
def events
end
def membership
end
def practice_facilities
end
def contact
end
def golf
#golf_courses = GolfCourse.all
end
def new
#contact = Contact.new
end
def create
#contact = Contact.new(params[:contact])
if #contact.valid?
ContactMailer.contact_email(#contact).deliver_now
redirect_to new_contact_path, notice: "Your email has been sent. Thank you."
else
render :new
end
end
end
And here is the form:
<div class="container">
<h4>Let us know if you have any questions.</h4>
<%= form_for #contact, :html => {:role => 'form'} do |f| %>
<div class="form-group">
<%= f.label :name, 'Enter your name:' %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email, 'Email:' %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :message, 'Message:' %>
<%= f.text_area :message, class: 'form-control', :rows => 3 %>
</div>
<div class="form-group">
<%= f.submit 'Submit', class: 'btn btn-default' %>
</div>
<% end %>
</div>
Thanks in advance for any help!
'First argument in form cannot contain nil or be empty'
You should also initialize the #contact in your other controller.
If you would like to share forms, I'd suggest the use of partials and passing in locals. This makes your code also somewhat DRY.
For reference also see the rails guides:
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

How to solve ParameterMissing error using ROR

Please help me to solve the below error.
Error:
ActionController::ParameterMissing in CustmersController#create
param is missing or the value is empty: users
When i am submitting the data,this error is coming.
My code is as follows
views/custmers/new.html.erb
<h1>Enter your data here</h1>
<center>
<%= form_for #users,:url => {:action => 'create'} do |f| %>
<% if #users.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#users.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #users.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<label for="name">Name:</label>
<%= f.text_field :name,placeholder:"Enter your name",:class => "input-field" %>
</p>
<p>
<label for="email">Email:</label>
<%= f.email_field :email,placeholder:"Enter your email",:class => "input-field" %>
</p>
<p>
<label for="phone">Phone no:</label>
<%= f.telephone_field :phoneno,placeholder:"Enter your phone number",:class => "input-field" %>
</p>
<p>
<%= f.submit "Submit" %>
</p>
<% end %>
<%= link_to "BACK",custmers_index_path %>
</center>
controller/custmers_controller.rb
class CustmersController < ApplicationController
def index
end
def new
#users=Custmer.new
end
def show
end
def create
#users=Custmer.new(user_params)
if #users.save
flash[:notice]="You have signed up successpully"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]="You have not signed up successfully"
flash[:color]="invalid"
render :new
end
end
private
def user_params
params.require(:users).permit(:name,:email,:phoneno)
end
end
model/custmer.rb
class Custmer < ActiveRecord::Base
EMAIL_REGEX = /\A[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :name,presence:true,length: { minimum: 5 }
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :phoneno, presence: true,length: {minimum: 10}
end
I am using rails version-4.2.0 and ruby version-1.9.3.Please help me to resolve this error.
If you look at the stack trace accompanying your error, you could tell definitely where the problem is -- look for the first line in the stack trace that refers to your code (and not library code).
But a fair guess is the require(:users) line in your controller. It looks like you either copy/pasted this code from another controller, or changed the name of your controller after generating it as part of your scaffold.
It should be requires(:custmer) instead, as that is the class of the thing you're submitting.
As a general approach, you should follow the standard Rails practices for naming things, throughout. If you really want to use the misspelled, Custmer class, have at it, but use #custmr inside your controller and views to refer to an instance, not #users.

Recaptcha getting Connection refused from RoR user form

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'

Save record in rails 4.0 using params hash

I'm trying to create a new record in rails using the form_for helper method. I think the params hash is blank because I keep getting blank errors when I submit the form.
This is my form:
<% provide(:title, "Add Department") %>
<h1>Add Department</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#department) do |f| %>
<%= render 'shared/department_error_messages' %>
<%= f.label :Full_Department_Title %>
<%= f.text_field :full_name %>
<%= f.label :Department_Abbreviation %>
<%= f.text_field :abbreviation %>
<%= f.submit "Add department", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
This is my departments controller
class DepartmentsController < ApplicationController
def show
#department = Department.find(params[:id])
end
def new
#department = Department.new
end
def create
#department = Department.new(params[department_params]) # Not the final implementation!
if #department.save
redirect_to root_path
else
render 'new'
end
end
private
def department_params
# This says that params[:department] is required, but inside that, only params[:department][:full_name] and
# params[:department][:abbreviation] are permitted. Unpermitted params will be stripped out
params.require(:department).permit(:full_name, :abbreviation)
end
end
This is the Model:
class Department < ActiveRecord::Base
validates :full_name, presence: true, length: { minimum: 6 }
end
When I submit, errors are rendered saying the full_name can't be blank (and the fields are now empty). The debug info is:
--- !ruby/hash:ActionController::Parameters utf8: ✓
authenticity_token: EjfYjWAzaw7YqVZAkCPZwiEMFfb2YLIRrHbH1CpZOwA=
department: !ruby/hash:ActionController::Parameters
full_name: Sports Department
abbreviation: SD
commit: Add department
action: create
controller: departments
I've also checked the development log. The transaction starts and is then rolled back. I can save a record in the console so I'm guessing its something to do with the params hash, but can't figure it out. Please help
This line:
#department = Department.new(params[department_params])
Should be:
#department = Department.new(department_params)
And, as a minor issue (but not causing this problem), your label tags should look like:
f.label :full_name, "Full Department Title"
This way they are correctly associated with the input.

Resources