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
Related
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.
Udemy: Bootcamp course: section: 8 Lecture: 122: SAVING TO THE DATABSE: In the "Contact Us" page I filled in the info, Name: test1, Email: test1#example.com, Comments: test1, as a test, pressed submit, but the button do not submit the info and gives no error message. Where am I going wrong? Any expert advice? Here is my code:
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
flash[:success] = 'Message sent.'
redirect_to new_contact_path
else
flash[:danger] = 'Error occured, message has not been sent.'
redirect_to new_contact_path
end
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="well"
<%= form_for #contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
</div>
</div>
</div>
Im not a rails expert but you can try <%= form_for(#contact, :method => :get) do |f| %> that should get it to work.
In your contact.rb model type the following
class Contact < ActiveRecord::Base
end
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 %>
I'm new in rails and i could use a little help.
I have the following setup in rails
Day has_many Bookings
Booking belongs_to Day
The Booking form in shared/_booking_form is as follows:
<%= form_for Booking.new do |t| %>
<%= render 'shared/error_messages', object: t.object %>
<div class="field">
<%= t.label :start %><br>
<%= t.time_select :start %>
</div>
<div class="field">
<%= t.label :end %><br>
<%= t.time_select :end %>
</div>
<div class="field">
<%= t.label :comentariu %><br>
<%= t.text_area :comentariu, placeholder: "Adauga un comentariu.." %>
</div>
<%= t.submit "Post", class: "btn btn-primary" %>
<% end %>
The bookings controller look like this
def show
#bookings = Booking.all
end
def new
#booking = Booking.new
end
def create
#booking = #day.bookings.build(booking_params)
#booking.save
flash[:success] = "Book created!"
redirect_to root_url
end
def destroy
end
private
def booking_params
params.require(:booking).permit(:start, :end, :comentariu)
end
I get this error and i haven't been able to solve
NoMethodError (undefined method `bookings' for nil:NilClass):
app/controllers/bookings_controller.rb:9:in `create'
Any help would be appreciated.
I am using devise with wicked to create a sign up wizard, but I am unsure about a problem I am having creating profiles. After a user provides their email & password they are forwarded to a step to create a profile based on whether they have specified they are a shipper or carrier. However I am unsure what the code should be in the controller and the forms to generically create a profile. Here is the code I have for the application:
The steps controller:
class UserStepsController < ApplicationController
include Wicked::Wizard
steps :carrier_profile, :shipper_profile
def create
#user = User.last
case step
when :carrier_profile
#profile = CarrierProfile.create!(:dot => params[:dot])
if #profile.save
render_wizard #user
else
flash[:alert] = "Record not saved"
end
when :shipper_profile
#profile = ShipperProfile.create!(params[:shipper_profile)
if #profile.save
render_wizard #user
else
flash[:alert] = "Record not saved"
end
end
end
end
end
def show
#user = User.last
#carrier_profile = CarrierProfile.new
#shipper_profile = ShipperProfile.new
case step
when :carrier_profile
skip_step if #user.shipper?
when :shipper_profile
skip_step if #user.carrier?
end
render_wizard
end
end
The form for a carrier profile:
<% form_for #carrier_profile , url: wizard_path, method: :post do |f| %>
<div>
<%= f.label :dot, "Please enter your DOT Number:" %>
<%= f.text_field :dot %>
</div>
<%= f.submit "Next Step", class: "btn btn-primary" %>
<% end %>
The form for a shipper profile:
<% form_for #shipper_profile , url: wizard_path, method: :post do |f| %>
<div>
<%= f.label :company_name, "What is your company name?" %>
<%= f.text_field :company_name %>
</div>
<%= f.submit "Next Step", class: "btn btn-primary" %>
<% end %>
The user model:
class User < ActiveRecord::Base
has_one :carrier_profile
has_one :shipper_profile
end
How would I be able to write a generic new and create method to handle creating both profiles? With the current code it is stating that the user_steps controller has no POST method, although if I run rake routes I find that this is untrue.
Add a step :profile_select to find which profile should be created
class UserStepsController < ApplicationController
include Wicked::Wizard
steps :profile_select, :carrier_profile, :shipper_profile
def update
#user = current_user
case step
when :profile_select
if params[:user][:profile_type] == 'carrier'
#profile = current_user.carrier_profile.build
else
#profile = current_user.shipper_profile.build
end
when :carrier_profile
current_user.carrier_profile.update_attributes(params[:carrier_profile])
when :shipper_profile
current_user.shipper_profile.update_attributes(params[:shipper_profile])
end
render_wizard #user
end
def show
#user = current_user
case step
when :carrier_profile
skip_step if #user.shipper?
when :shipper_profile
skip_step if #user.carrier?
end
render_wizard
end
end
views/user_steps/profile_select.html.erb:
Note: method :put so that update gets called for all steps
<%= form_for current_user, url: wizard_path, method: :put do |f| %>
<%= f.label :profile_type %>
<%= f.text_field :profile_type %>
<%= f.submit "Next Step", class: "btn btn-primary" %>
<% end %>
views/user_steps/carrier_profile.html.erb:
<% form_for #profile, url: wizard_path, method: :put do |f| %>
<div>
<%= f.label :dot, "Please enter your DOT Number:" %>
<%= f.text_field :dot %>
</div>
<%= f.submit "Next Step", class: "btn btn-primary" %>
<% end %>
views/user_steps/shipper_profile.html.erb:
<% form_for #profile, url: wizard_path, method: :put do |f| %>
<div>
<%= f.label :company_name, "What is your company name?" %>
<%= f.text_field :company_name %>
</div>
<%= f.submit "Next Step", class: "btn btn-primary" %>
<% end %>