Rails not displaying error messages when submitting a form - ruby-on-rails

I'm new to rails so I have a Signup controller that looks as follows:
class SignupController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
redirect_to root_path, notice: "You are logged in as #{#user[:username]}"
else
puts #user.errors.full_messages
render :new
end
end
private
def user_params
puts params
params.require(:user).permit(:email, :password, :password_confirmation, :username)
end
end
So I'm trying to create a new user when I submit the form and my routes.rb file contains the following:
Rails.application.routes.draw do
root to: "home#index"
get '/about-us', to: "about#index", as: :about
# signing up
get 'sign_up', to: "signup#new"
post 'sign_up', to: "signup#create"
end
My app/views/signup/new.html.erb file has the following code in it:
<h1>Sign Up</h1>
<%= form_with model: #user, url: sign_up_path do |form| %>
<%= #user.errors.full_messages.inspect %>
<% if #user.errors.any? %>
<div class="alert alert-danger">
<% #user.errors.full_messages.each do |message| %>
<div><%= message %></div>
<% end %>
</div>
<% end %>
<div class="mb-3">
<%= form.label :email, class: "form-label"%>
<%= form.text_field :email, placeholder: "email#gmail.com", class: "form-control"%>
</div>
<div class="mb-3">
<%= form.label :username, class: "form-label"%>
<%= form.text_field :username, placeholder: "username", class: "form-control"%>
</div>
<div class="mb-3">
<%= form.label :password, class: "form-label"%>
<%= form.password_field :password, placeholder: "password", class: "form-control"%>
<div class="mb-3">
<%= form.label :password_confirmation, class: "form-label"%>
<%= form.password_field :password_confirmation, placeholder: "password", class: "form-control"%>
</div>
<div class="mb-3">
<%= form.submit "Sign Up", class: 'btn btn-primary'%>
</div>
<% end %>
But it seems like I'm getting the error messages in the console but they can't be rendered in the template when i submit the form with errors. Here is the errors i'm getting:
Password can't be blank
Email can't be blank
Email invalid email address
Username can't be blank
What maybe possibly the problem with my code here.

In your form, try this instead
<% if #user.errors.any? %>
<div class="alert alert-danger">
<% #user.errors.each do |error| %>
<div><%= error.full_message %></div>
<% end %>
</div>
<% end %>

I was using rails version 7.* so I manage to solve this error by changing the create method in the SignupController to :
def create
#user = User.new(user_params)
if #user.save
redirect_to root_path, notice: "You are logged in as #{#user[:username]}"
else
puts #user.errors.full_messages
render :new, status: :unprocessable_entity, content_type: "text/html"
end
end
This is according to the suggestions i got from Alex in the comments together with the discussion that i found here.

Related

Why isn't my Create Form working, or even responding?

So I'm using form_with, and my code is placed in views/users/new.html.erb
<% content_for :title, "Sign Up" %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#user.errors.count, "error") %>
prohibited this user from being saved:
</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_with model: #user do |f| %>
<%= f.label :email %>
<%= f.email_field :email, :placeholder => 'E-mail address' %><br>
<%= f.label :password %>
<%= f.password_field :password, :placeholder => 'Password' %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, :placeholder => 'Password confirmation' %><br>
<label>Are you a cleaner or customer?</label>
<%= f.select :user, User::SUB_CLASSES, include_blank: true %><br>
<%= f.submit "Next" %>
<% end %>
My controller has the following code
def new
#user = User.new
render :layout => "beforelogin"
end
def create
#user = User.new(user_params)
if #user.save
session[:user_id] = #user.id
if #user.sub_class == "Cleaner"
redirect_to new_cleaner_path
else
redirect_to new_customer_path
end
else
render :new
flash[:alert] = "Your registration could not be completed"
end
end
Now when I fill out the form on the user/new.html.erb route, nothing happens. The button just clicks. No errors pop up. I even tried to add to the top of the form, <%= form_with model: #user, url: users_path, method: "post" do |f| %>. Still the same response.
By default form_with attaches the data-remote attribute to the form. As a consequence, the form will be submitted using Ajax.
In case you want to disable this feature, you need to set the option local to true.
like this:
<%= form_with model: #user, local: true do |f| %>

ActionController::ParameterMissing: param is missing or the value is empty: user - Don't understand

Ok, so I have seen the other threads relating to this issue, but so far I haven't had any luck in solving it following their advice.
I'm trying to test for unsuccessful edits, but I am consistently having an error where the param is missing for 'user'.
Here is the controller code:
class UsersController < ApplicationController
def new
#user = User.new
end
def show
#user = User.find(params[:id])
end
def create
#user = User.new(user_params)
if #user.save
log_in #user
flash[:success] = "Welcome to the Site!"
redirect_to #user
else
render 'new'
end
end
def edit
#user = User.find(params[:id])
end
def update
#user = User.find(params[:id])
if #user.update_attributes(user_params)
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
View:
<% provide(:title, "Edit User") %>
<% provide(:button_text, "Save changes") %>
<h1>Edit your profile </h1>
<div class="row">
<div class="col-md-6 col-md-offset-3 well">
<%= render 'shared/form' %>
<div class="gravatar_edit">
<%= gravatar_for #user %>
Change
</div>
</div>
</div>
And here is the test I have for unsuccessful edits:
require 'test_helper'
class UsersEditTest < ActionDispatch::IntegrationTest
def setup
#user = users(:robert)
end
test "unseccessful edit" do
get edit_user_path(#user)
assert_template 'users/edit'
patch user_path(#user), params: { user: { name: "",
email: "invalid#invalid.org",
password: "in ",
password_confirmation: " valid" } }
assert_template 'users/edit'
end
end
Now, I have tried removing (user_params) from if #user.update_attributes, but it makes no difference. I've also removed .requre(:user) from def params, but that only causes many more errors.
Edit
Here is the form I'm using:
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', object: #user %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit yield(:button_text), class: 'btn btn-primary' %>
<% end %>

Pass Reset - No route matches [PATCH] "/password_resets.SOLceKJXoax55zSBAfAhTQ"

I'm doing the Michael Hartl rails tutorial and trying to implement the password reset functionality but the request that sends the password reset form (The form where you reset your password) looks like /password_resets.SOLceKJXoax55zSBAfAhTQ which rails then says: No route matches [PATCH] "/password_resets.SOLceKJXoax55zSBAfAhTQ" In the tutorial we are asked to use this route: resources :password_resets, only: [:new, :create, :edit, :update] But I get that error message.
This is how my password_resets_controller.rb update method looks like:
def update
if password_blank?
flash.now[:danger] = "Password can't be blank"
render 'edit'
elsif #user.update_attributes(user_params)
log_in #user
flash[:success] = "Password has been reset."
redirect_to #user
else
render 'edit'
end
end
Any idea how does my method or my route is causing that error?
Thank you in advance.
Update:
Adding my Reset Password view (password_resets\edit.html.erb)
<% provide(:title, 'Reset Password') %>
<h1>Reset Password</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#user, url: password_resets_path(params[:id])) do |f| %>
<%= render 'shared/error_messages' %>
<%= hidden_field_tag :uemail, #user.uemail %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "update password", class: "btn btn-primary" %>
<% end %>
</div>
</div>
Change this:
<%= form_for(#user, url: password_resets_path(params[:id])) do |f| %>
to this:
<%= form_for(#user, url: password_reset_path(params[:id])) do |f| %>

im stuck at assert_template 'bookings/show' test in rails.

I'm working on a simple project just to get a deeper understanding of rails and I'm stuck at a simple test assert_template 'bookings/show' which lead to this error, i have been checking many times, can't find a clue :
expecting <"bookings/show"> but rendering with <[]>
test/integration/booking_submit_test.rb:17:
The related code is on below:
test/integration/booking_submit_test.rb
require 'test_helper'
class BookingSubmitTest < ActionDispatch::IntegrationTest
test "invalid booking submit information" do
get booknow_path
assert_no_difference 'Booking.count' do
post bookings_path, booking: {date_of_tour: "2017-05-06", hotel_name: "xxx hotel", phone_number:123456 , number_of_pax:34 , pick_up_time: "9:00"}
end
assert_template 'bookings/new'
end
test "valid booking submit information" do
get booknow_path
assert_difference 'Booking.count', 1 do
post bookings_path, booking: {date_of_tour: "2017-05-06", hotel_name: "xxx hotel", phone_number:12345678901 , number_of_pax:34 , pick_up_time: "9:00"}
end
assert_template 'bookings/show'
end
end
show.html.rb:
<% provide(:title, #booking.date_of_tour) %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<h1>
<%= #booking.hotel_name %>
</h1>
</section>
</aside>
</div>
new.html.rb:
<% provide(:title, 'Book the package now') %>
<h1>Book now</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#booking) do |f| %>
<%= render 'shared/errorbooking_messages' %>
<%= f.label :date_of_tour %>
<%= f.text_field :date_of_tour, class: 'form-control' %>
<%= f.label :hotel_name %>
<%= f.text_field :hotel_name, class: 'form-control' %>
<%= f.label :phone_number %>
<%= f.text_field :phone_number, class: 'form-control' %>
<%= f.label :number_of_pax %>
<%= f.text_field :number_of_pax, class: 'form-control' %>
<%= f.label :pick_up_time %>
<%= f.text_field :pick_up_time, class: 'form-control' %>
<%= f.submit "Book now", class: "btn btn-primary" %>
<% end %>
</div>
</div>
bookings_controller.rb:
class BookingsController < ApplicationController
def show
#booking = Booking.find(params[:id])
end
def new
#booking = Booking.new
end
def create
#booking = Booking.new(booking_params) # Not the final implementation!
if #booking.save
flash[:success] = "You have submited the information successfully!"
redirect_to #booking
else
render 'new'
end
def edit
#booking = Booking.find(params[:id])
end
end
private
def booking_params
params.require(:booking).permit(:date_of_tour, :hotel_name, :phone_number, :number_of_pax, :pick_up_time )
end
end
routes.rb
get 'booknow' => 'bookings#new'
resources :bookings
When the input is correct, the action executes a redirect_to, so the test you should use assert_redirected_to :
test "valid booking submit information" do
get booknow_path
assert_difference 'Booking.count', 1 do
post bookings_path, booking: {date_of_tour: "2017-05-06", hotel_name: "xxx hotel", phone_number:12345678901 , number_of_pax:34 , pick_up_time: "9:00"}
end
assert_redirected_to booking_path(assigns(:booking))
end

My rails form keeps saying password can't be left blank after I try to sign up on my local server?

Every time I try to sign up on my rails form, it says password can not be left blank, and I am putting in a password every time. I've been staring at my code for hours now trying to figure it out, and I can't.
Here is my users/new.html.erb
<h1>Sign Up</h1>
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="actions"><%= f.submit "Sign Up" %></div>
<% end %>
Users controller:
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
session[:user_id] = #user.id
redirect_to root_url, notice: "Thank you for signing up!"
else
render "new"
end
end
private
def user_params
params.required(:user).permit(:name, :email, :password_digest)
end
end
User model:
class User < ActiveRecord::Base
has_secure_password
validates_uniqueness_of :email
end
Thanks for your help, I really am coming here as a last resort.
Change your user_params method to:
def user_params
params.required(:user).permit(:name, :email, :password)
end
Since you are dealing with password field/method I think you should allow it.
Add password to your parms.require method
i.e.
def user_params
params.required(:user).permit(:name, :email, :password)
end

Resources