Could some explain to me why I may well be getting the following error every time I try to create a booking.
NoMethodError in BookingsController#create
undefined method `diary_slot_day_time_path' for nil:NilClass
I have the following code in my controller:
class BookingsController < PortalController
# GET /bookings
# GET /bookings.json
def index
session[:booking_context] = 'index'
if current_user.is_booking_manager?
# #bookings = Booking.all
# #bookings = Booking.filter(params[:search], [:reference_number, :company_id])
#bookings = Booking.includes(:company).filter(params[:search], [:reference_number, "companies.name"])
#main_heading = 'Bookings'
else
#bookings = current_user.company.bookings
#main_heading = "#{current_user.company.name} Bookings"
end
respond_to do |format|
format.html # index.html.erb
end
end
# GET /bookings/1
def show
#booking = Booking.find(params[:id])
goto_booking(#booking)
end
# GET /bookings/1/edit
def edit
#booking = Booking.find(params[:id])
#main_heading = 'Edit Booking'
end
# Go to booking
def goto_booking(booking)
site = #booking.site
slot_day = #booking.slot_day
slot_time = #booking.slot_time
# don't need to check slot_time because it is optional
if session[:booking_context] and session[:booking_context] == 'diary' and site and slot_day
# Go the diary page
respond_to do |format|
format.html { redirect_to diary_slot_day_time_path(site, slot_day, slot_time) }
end
else
# Go to booking edit page
respond_to do |format|
format.html { redirect_to edit_booking_path(#booking) }
end
end
end
# POST /bookings
def create
#booking = Booking.new(params[:booking])
unless #booking and #booking.slot_time and #booking.slot_time.number_of_free_slots > 0
flash[:notice] = 'Slot no longer available'
redirect_to :back
return
end
if #booking.save
flash[:notice] = 'Booking was successfully created'
goto_booking(#booking)
else
#main_heading = 'Review New Booking'
respond_to do |format|
format.html { render action: "new" }
end
end
end
# PUT /bookings/1
def update
#booking = Booking.find(params[:id])
if #booking.update_attributes(params[:booking])
flash[:notice] = 'Booking was successfully updated'
goto_booking(#booking)
else
#main_heading = 'Review Booking'
respond_to do |format|
format.html { render action: "edit" }
end
end
end
def confirmation
begin
#booking = Booking.find(params[:booking][:id])
#booking.update_attributes(params[:booking])
if params[:make_unexpected]
#booking.provisional_appointment = nil
#booking.save!
end
#booking.update_slot!
success = true
rescue => e
flash[:error] = e.message
success = false
end
if success
flash[:notice] = 'Booking confirmed'
respond_to do |format|
format.html { redirect_to booking_path(#booking) }
end
else
redirect_to :back
end
end
# DELETE /bookings/1
def destroy
#booking = Booking.find(params[:id])
if #booking
if session[:booking_context] and session[:booking_context] == 'diary'
site = #booking.site
slot_day = #booking.slot_day
slot_time = #booking.slot_time
end
if #booking.destroy
flash[:notice] = 'booking removed'
else
flash[:error] = 'could not remove booking'
end
end
# don't need to check slot_time; that's optional
if site and slot_day
# redirect to diary page
respond_to do |format|
format.html { redirect_to diary_slot_day_time_path(site, slot_day, slot_time) }
end
else
# redirect to booking index
respond_to do |format|
format.html { redirect_to bookings_url }
end
end
end
def diary_slot_day_time_path(*args)
#template.diary_slot_day_time_path(*args)
end
end
I think that potentially it is something to do with a routing error I may well be wrong. This is my full trace error: Pastie. I have looked at the trace which indicates to me that there is something wrong with my go_to_booking, create methods.
Why do you have this method defined at the bottom of your controller?
def diary_slot_day_time_path(*args)
#template.diary_slot_day_time_path(*args)
end
This is attempting to reference an undefined instance variable called #template, which is why you're getting that error.
You should be defining this method in your routes, by using nested routes like this:
match '/diary/:day/:time', :to => "some_controller#some_action", :as => "diary_slot_day_time"
Related
i have tutorial on Rails from here:
Rails5
and wanted to add a field "attachment" to the form.
articles_controller.rb is as follows:
class ArticlesController < ApplicationController
http_basic_authenticate_with name: "gerrit", password: "Ifb6K54WVs7U", except: [:index, :show]
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
end
def new
#article = Article.new
end
def edit
#article = Article.find(params[:id])
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
def update
#article = Article.find(params[:id])
if #article.update(article_params)
redirect_to #article
else
render 'edit'
end
end
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text, :attachment)
end
end
For this i added paperclip and turbolink5.
I rerun all configuring steps in the tutorial but I always get the error in the title:
Extracted source (around line #24):
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
I do not use single params here, but article_params!?
Any help appreciated!
As said in the last comment:
I updated paperclip to v5.1.0 and the error was gone.
I am a new programmer on ruby on rails, and i have problem when upload picture. Deos anyone can help me. thanks in advance.
class StudentsController < ApplicationController
before_action :set_student, only: [:show, :edit, :update, :destroy]
# GET /students
# GET /students.json
def index
#students = Student.all
end
# GET /students/1
# GET /students/1.json
def show
end
# GET /students/new
def new
#student = Student.new
end
# GET /students/1/edit
def edit
end
# POST /students
# POST /students.json
def create
#student = Student.create(student_params)
respond_to do |format|
if #student.save
format.html { redirect_to #student, notice: 'Student was successfully created.' }
format.json { render :show, status: :created, location: #student }
else
format.html { render :new }
format.json { render json: #student.errors, status: :unprocessable_entity }
end
end
end
private
#def student_params
#params.require(:student).permit(:image_file)
#end
# PATCH/PUT /students/1
# PATCH/PUT /students/1.json
def update
respond_to do |format|
if #student.update(student_params)
format.html { redirect_to #student, notice: 'Student was successfully updated.' }
format.json { render :show, status: :ok, location: #student }
else
format.html { render :edit }
format.json { render json: #student.errors, status: :unprocessable_entity }
end
end
end
# DELETE /students/1
# DELETE /students/1.json
def destroy
#student.destroy
respond_to do |format|
format.html { redirect_to students_url, notice: 'Student was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_student
#student = Student.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def student_params
params.require(:student).permit(:name, :gender, :telephone, :address)#, :image_file
end
end
Can't really find a problem in your post. It's a bit hard to understand. Did you follow the docs on https://github.com/thoughtbot/paperclip? I take it your file attachment is in the Student model?
params.require(:student).permit(:name, :gender, :telephone, :address)#, :image_file
now you only permit to save name, gender, telephone and address, the image file wil not be saved. You still have to permit it.
A company has many properties. A property has one company.
In my routes file I got:
resources :companies do
resources :property_managers
end
In the property_manager_controller, my create action looks like this (default scaffold implementation slightly modified to accommodate the company):
def create
#property_manager = PropertyManager.new(params[:property_manager])
#property_manager.company_id = params[:company_id]
respond_to do |format|
if #property_manager.save
format.html { redirect_to company_property_managers_path, notice: 'Property manager was successfully created.' }
format.json { render json: #property_manager, status: :created, location: #property_manager }
else
format.html { render action: "new" }
format.json { render json: #property_manager.errors, status: :unprocessable_entity }
end
end
end
Is there a way in which I do not have to explicitly set the company_id, since it is known within the context of the URL/route?
I guess you could do something like the following, not sure if it's better or not:
class PropertyManagersController < ApplicationController
before_filter :find_company
def new
#property_manager = #company.property_managers.build
end
def create
#property_manager = #company.property_managers.build(params[:property_manager])
respond_to do |format|
...
end
end
private
def find_company
#company ||= Company.find(params[:company_id])
end
end
My application uses tickets and call_logs. I have these nested in each other, so that tickets can have many call_logs.
I am getting the error with [GET] "/call_logs"
I don't know what I am missing here.
I do have my routes nested in routes.rb
resources :tickets do
resources :call_logs
end
rake routes:
ticket_call_logs GET /tickets/:ticket_id/call_logs(.:format) call_logs#index
POST /tickets/:ticket_id/call_logs(.:format) call_logs#create
new_ticket_call_log GET /tickets/:ticket_id/call_logs/new(.:format) call_logs#new
edit_ticket_call_log GET /tickets/:ticket_id/call_logs/:id/edit(.:format) call_logs#edit
ticket_call_log GET /tickets/:ticket_id/call_logs/:id(.:format) call_logs#show
PUT /tickets/:ticket_id/call_logs/:id(.:format) call_logs#update
DELETE /tickets/:ticket_id/call_logs/:id(.:format) call_logs#destroy
tickets GET /tickets(.:format) tickets#index
POST /tickets(.:format) tickets#create
new_ticket GET /tickets/new(.:format) tickets#new
edit_ticket GET /tickets/:id/edit(.:format) tickets#edit
ticket GET /tickets/:id(.:format) tickets#show
PUT /tickets/:id(.:format) tickets#update
DELETE /tickets/:id(.:format) tickets#destroy
call_logs controller:
class CallLogsController < ApplicationController
before_filter :get_ticket
# GET /call_logs
# GET /call_logs.json
def index
#call_logs = #ticket.call_logs.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #call_logs }
end
# GET /call_logs/1
# GET /call_logs/1.json
def show
#call_log = #ticket.call_logs.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #call_log }
end
end
# GET /call_logs/new
# GET /call_logs/new.json
def new
#call_log = #ticket.call_logs.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: #call_log }
end
end
# GET /call_logs/1/edit
def edit
#call_log = #ticket.call_logs.find(params[:id])
end
# POST /call_logs
# POST /call_logs.json
def create
#call_log = CallLog.new(params[:call_log])
respond_to do |format|
if #call_log.save
format.html { redirect_to ticket_call_logs_url(#ticket), notice: 'Call log was successfully created.' }
format.json { render json: #call_log, status: :created, location: #call_log }
else
format.html { render action: "new" }
format.json { render json: #call_log.errors, status: :unprocessable_entity }
end
end
end
# PUT /call_logs/1
# PUT /call_logs/1.json
def update
#call_log = #ticket.call_logs.find(params[:id])
respond_to do |format|
if #call_log.update_attributes(params[:call_log])
format.html { redirect_to ticket_call_logs_url(#ticket), notice: 'Call log was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #call_log.errors, status: :unprocessable_entity }
end
end
end
# DELETE /call_logs/1
# DELETE /call_logs/1.json
def destroy
#call_log = #ticket.call_log.find(params[:id])
#call_log.destroy
respond_to do |format|
format.html { redirect_to ticket_call_logs_path(#call_log)}
format.json { head :no_content }
end
end
end
end
private
def get_ticket
#ticket = Ticket.find(params[:ticket_id])
end
I believe I have an error in my url paths when directing to the call_log page, but I can't seem to find it. Any advice or tips would be appreciated, as I am a noob.
Thank you.
As you can see from the output of rake routes, You don't have a route that matches /call_logs. Since this is a nested resource route you have to prepend the tickets and ticket id: /tickets/<id>/call_logs.
Looks like you have done changes in routes.rb but not maintaining relation in Models. As rake routes say
ticket_call_logs GET /tickets/:ticket_id/call_logs(.:format)
Should be accessible by /ticket/id/call_logs , Now here "id" is ticket id. This will list all call logs for that particular ticket_id.
I have the following code in my controller
def create
#severity = Severity.new(params[:severity])
if #severity.save
flash[:notice] = "Successfully created severity"
render 'save'
end
end
I am trying to get the method to render another view file other than create.js.erb however the controller always renders the default rather than the save.js.erb.
Any ideas on what could be wrong?
def create
#severity = Severity.new(params[:severity])
if #severity.save
flash[:notice] = "Successfully created severity"
respond_to do |format|
format.js { render :template => "/path/to/save" }
end
end
end
or
def create
#severity = Severity.new(params[:severity])
if #severity.save
flash[:notice] = "Successfully created severity"
respond_to do |format|
format.js { render :file => "/path/to/save.js.erb" }
end
end
end
try this
def create
#severity = Severity.new(params[:severity])
if #severity.save
flash[:notice] = "Successfully created severity"
end
render :file => "/path/to/save.js.erb"
end