NoMethodError : undefined method `find' for nil:NilClass - ruby-on-rails

I want show a daycare details on show page but I got this error
NoMethodError : undefined method `find' for nil:NilClass
from daycare controller file and i'm not get any idea. I have mentioned below that error line.
This is my Controller file
class DayCaresController < ApplicationController
before_filter :authenticate_user!
before_action :set_day_care, only: [:show, :edit, :update, :destroy]
# GET /day_cares
# GET /day_cares.json
def index
#day_cares = DayCare.all
end
# GET /day_cares/1
# GET /day_cares/1.json
def show
end
# GET /day_cares/new
def new
#day_care = DayCare.new
end
# GET /day_cares/1/edit
def edit
end
# POST /day_cares
# POST /day_cares.json
def create
#day_care = current_user.build_day_care(day_care_params)
respond_to do |format|
if #day_care.save
UserMailer.welcome_email(#user).deliver
format.html { redirect_to #day_care, :gflash => { :success => 'Day care was successfully created.'} }
format.json { render :show, status: :created, location: #day_care }
else
format.html { render :new }
format.json { render json: #day_care.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /day_cares/1
# PATCH/PUT /day_cares/1.json
def update
respond_to do |format|
if #day_care.update(day_care_params)
format.html { redirect_to #day_care, :gflash => { :success => 'Day care was successfully updated.'} }
format.json { render :show, status: :ok, location: #day_care }
else
format.html { render :edit }
format.json { render json: #day_care.errors, status: :unprocessable_entity }
end
end
end
# DELETE /day_cares/1
# DELETE /day_cares/1.json
def destroy
#day_care.destroy
respond_to do |format|
format.html { redirect_to day_cares_url, :gflash => { :success => 'Day care was successfully destroyed.'} }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions
def set_day_care
#day_care = current_user.day_care.find(params[:id]) # => **I got error this line**
end
# Never trust parameters from the scary internet, only allow the white list through.
def day_care_params
params.require(:day_care).permit(:name, :address, :office_phone, :cell_phone, :logo, :website, :user_id)
end
def dashboard
end
def profile
end
end

If user has_many: day_cares then use this name instead of day_care:
#day_care = current_user.day_cares.where(id: params[:id]).take
or probably as you wrote:
#day_care = current_user.day_cares.find(params[:id])
But with arrays instead of single instance (day_cares).
Also you can use just:
#day_care = DayCare.find(params[:id])
If you search by id. Or if you need to check that it's users day_care:
#day_care = DayCare.where(id: params[:id], user: current_user).take

current_user.day_care.find is not available, because you can only perform queries on plural associations. So given that the model associations are setup correctly as:
class User < ActiveRecord:Base
has_many :day_cares
...
end
the solution is probably just to resolve the spelling error from
`current_user.day_care.find` #wrong!
to
`current_user.day_cares.find` #right!

Related

Rails 5 multiple nested attributes no Routing Error uninitialized constant Sites

Hello I have a little app with three nested models Client, Site and Damper. When I add a client or client_site all is fine... but when i come to add a damper I get
Routing Error
uninitialized constant Sites
in the console
Started GET "/clients/1/sites/1/dampers/new" for my ip at 2018-10-26 18:05:29 +1000
ActionController::RoutingError (uninitialized constant Sites):
app/controllers/clients/sites/dampers_controller.rb:1:in `<main>'
Routes
resources :clients do
resources :sites, controller: 'clients/sites' do
resources :dampers, controller: 'clients/sites/dampers'
end
end
Models
app/models/client.rb
class Client < ApplicationRecord
has_many :sites
end
app/models/site.rb
class Site < ApplicationRecord
belongs_to :client
has_many :dampers
end
app/models/damper.rb
class Damper < ApplicationRecord
belongs_to :site
end
please note I had made a mistake and this was originally :sites but even after changing this the fault remained.
Controllers
app/controllers/clients_controller.rb
class ClientsController < ApplicationController
before_action :set_client, only: [:show, :edit, :update, :destroy]
# GET /clients
# GET /clients.json
def index
#clients = Client.all
end
# GET /clients/1
# GET /clients/1.json
def show
#client = Client.find(params[:id])
#sites = #client.sites
end
# GET /clients/new
def new
#client = Client.new
end
# GET /clients/1/edit
def edit
end
# POST /clients
# POST /clients.json
def create
#client = Client.new(client_params)
respond_to do |format|
if #client.save
format.html { redirect_to #client, notice: 'Client was successfully created.' }
format.json { render :show, status: :created, location: #client }
else
format.html { render :new }
format.json { render json: #client.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /clients/1
# PATCH/PUT /clients/1.json
def update
respond_to do |format|
if #client.update(client_params)
format.html { redirect_to #client, notice: 'Client was successfully updated.' }
format.json { render :show, status: :ok, location: #client }
else
format.html { render :edit }
format.json { render json: #client.errors, status: :unprocessable_entity }
end
end
end
# DELETE /clients/1
# DELETE /clients/1.json
def destroy
#client.destroy
respond_to do |format|
format.html { redirect_to clients_url, notice: 'Client was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_client
#client = Client.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def client_params
params.require(:client).permit(:name)
end
end
app/controllers/clients/sites_controller.rb
class Clients::SitesController < ApplicationController
before_action :set_client
before_action :set_site, except: [:new, :create]
# GET /sites
# GET /sites.json
def index
#sites = Site.all
end
# GET /sites/1
# GET /sites/1.json
def show
#client = Client.find(params[:client_id])
#site = #client.sites.find(params[:id])
end
# GET /sites/new
def new
#site = Site.new
end
# GET /sites/1/edit
def edit
end
# POST /sites
# POST /sites.json
def create
#site = Site.new(site_params)
#site.client = #client
respond_to do |format|
if #site.save
format.html { redirect_to #client, notice: 'Site was successfully created.' }
format.json { render :show, status: :created, location: #client }
else
format.html { render :new }
format.json { render json: #client.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /sites/1
# PATCH/PUT /sites/1.json
def update
respond_to do |format|
if #site.update(site_params)
format.html { redirect_to #client, notice: 'Site was successfully updated.' }
format.json { render :show, status: :ok, location: #site }
else
format.html { render :edit }
format.json { render json: #client.errors, status: :unprocessable_entity }
end
end
end
# DELETE /sites/1
# DELETE /sites/1.json
def destroy
#site.destroy
respond_to do |format|
format.html { redirect_to sites_url, notice: 'Site was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_site
#site = Site.find(params[:id])
end
def set_client
#client = Client.find(params[:client_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def site_params
params.require(:site).permit(:name, :client_id)
end
end
app/controllers/clients/sites/dampers_controller.rb
class Sites::DampersController < ApplicationController
before_action :set_client
before_action :set_site
before_action :set_damper, except: [:new, :create]
# GET /dampers
# GET /dampers.json
def index
#dampers = Damper.all
end
# GET /dampers/1
# GET /dampers/1.json
def show
end
# GET /dampers/new
def new
#damper = Damper.new
end
# GET /dampers/1/edit
def edit
end
# POST /dampers
# POST /dampers.json
def create
#damper = Damper.new(damper_params)
#damper.site = #site
respond_to do |format|
if #damper.save
format.html { redirect_to #site, notice: 'Damper was successfully created.' }
format.json { render :show, status: :created, location: #site }
else
format.html { render :new }
format.json { render json: #site.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /dampers/1
# PATCH/PUT /dampers/1.json
def update
respond_to do |format|
if #damper.update(damper_params)
format.html { redirect_to #site, notice: 'Damper was successfully updated.' }
format.json { render :show, status: :ok, location: #site }
else
format.html { render :edit }
format.json { render json: #site.errors, status: :unprocessable_entity }
end
end
end
# DELETE /dampers/1
# DELETE /dampers/1.json
def destroy
#damper.destroy
respond_to do |format|
format.html { redirect_to dampers_url, notice: 'Damper was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_damper
#damper = Damper.find(params[:id])
end
def set_site
#site = Site.find(params[:site_id])
end
def set_client
#client = Client.find(params[:client_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def damper_params
params.require(:damper).permit(:location, :number, :site_id, :client_id)
end
end
ActionController::RoutingError (uninitialized constant Sites)
The dampers_controller.rb sits under controllers/clients/sites, so you need to change the class name to
class Clients::Sites::DampersController < ApplicationController
instead of
class Sites::DampersController < ApplicationController
for the sake of namespacing
Also, I recommend you to have a look at controller-namespaces-and-routing
You created a controller named Sites::DampersController, which is a class DampersController defined inside the namespace (module, class,...) named Sites, but you forgot to define this last one.
You could create it this way:
module Sites
class DampersController < ApplicationController
end
end
Or just get rid of the Sites:: part.
You will also need to update your routes, to specify the correct controller name.
More generally, it is easier to follow rails default route generation:
resources :clients do
resources :sites do
resources :dampers
end
end
Which will create routes pointing to the following controllers:
ClientsController
SitesController
DampersController
If you really intend on putting the other controllers in a sub folders, following your original routes, you will need to define the following:
controller ClientsController
module Clients
controller SitesController inside module Clients
module Sites inside module Clients
controller DampersController inside module Clients::Sites
Which, for autoloading to works, would have to be organized in subfolders too:
app/controllers
clients_controllers.rb
clients/
sites__controllers.rb
sites/
dampers_controllers.rb

Rails 5.1 Trying to pass an attribute to a form using a link

I am trying to pass an attribute to an object that is being created by a link. I am on the show view of another object and I want to have two links available one that will make the :attribute false and the other to make the :attribute true. I have it set up so the default value of the this attribute is false and I tried using something like below, but it just saves it as nil in the database:
<%= link_to "Yes", new_building_listing_appointment_rented_unit_path(#building, #listing, #appointment, #rented_unit, leased: true) %>>
controller
class RentedUnitsController < ApplicationController
before_action :building
before_action :listing
before_action :appointment
before_action :set_rented_unit, only: [:show, :edit, :update, :destroy]
# GET /rented_units
# GET /rented_units.json
def index
#rented_units = appointment.rented_units
end
# GET /rented_units/1
# GET /rented_units/1.json
def show
end
# GET /rented_units/new
def new
#rented_unit = appointment.rented_units.new
end
# GET /rented_units/1/edit
def edit
end
# POST /rented_units
# POST /rented_units.json
def create
#rented_unit = appointment.rented_units.new(rented_unit_params)
respond_to do |format|
if #rented_unit.save
format.html { redirect_to [building, listing, appointment, #rented_unit], notice: 'Rented unit was successfully created.' }
format.json { render :show, status: :created, location: #rented_unit }
else
format.html { render :new }
format.json { render json: #rented_unit.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /rented_units/1
# PATCH/PUT /rented_units/1.json
def update
respond_to do |format|
if #rented_unit.update(rented_unit_params)
format.html { redirect_to [building, listing, appointment, #rented_unit], notice: 'Rented unit was successfully updated.' }
format.json { render :show, status: :ok, location: #rented_unit }
else
format.html { render :edit }
format.json { render json: #rented_unit.errors, status: :unprocessable_entity }
end
end
end
# DELETE /rented_units/1
# DELETE /rented_units/1.json
def destroy
#rented_unit.destroy
respond_to do |format|
format.html { redirect_to building_listing_appointment_rented_units_path(#building, #listing, #appointment), notice: 'Rented unit was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_rented_unit
#rented_unit = appointment.rented_units.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def rented_unit_params
params.require(:rented_unit).permit(:unit_no, :unit_model, :price, :bedrooms, :bathrooms, :half_baths, :square_footage, :leased, :appointment_id)
end
def building
#building ||= Building.find(params[:building_id])
end
def listing
#listing ||= Listing.find(params[:listing_id])
end
def appointment
#appointment ||= Appointment.find(params[:appointment_id])
end
end
From what I understand you are looking to populate leased attribute auto when you open a new from from the link.
You need to give the param param to the link.
<%= link_to "Yes", new_building_listing_appointment_rented_unit_path(#building, #listing, #appointment, #rented_unit, rented_unit: { leased: true } ) %>>
In the controller then you can do some thing like
# GET /rented_units/new
def new
#rented_unit = appointment.rented_units.new(rented_unit_params)
end
Then, in the new form you will see the checkbox (or other control) selected.

Rails/Actionmailer error trying to edit email

I'm using rails 4 and actionmailer to allow users to edit a generated email before letting it send.
When I try to load the editing page, I get this error.
Showing /var/www/rqm3/app/views/rfis/mail.html.erb where line #5 raised:
undefined method `body' for nil:NilClass
Here's line 5 for referrence.
<%= text_area_tag :email_body, #mail_message.html_part.body.raw_source,class:"tinymce", rows:40, cols:120 %>
I have #mail_message set from my controller here.
def mail
#mail_message = RfiMailer.send_rfi(current_user, #rfi)
end
Thanks to anyone that helps.
EDIT:
rfis_controller:
class RfisController < ApplicationController
before_action :set_rfi, only: [:show, :edit, :update, :destroy, :mail]
before_action :authenticate_user!
# GET /rfis
# GET /rfis.json
def index
#rfis = Rfi.all
end
# GET /rfis/1
# GET /rfis/1.json
def show
end
# GET /rfis/new
def new
#rfi = Rfi.new
end
# GET /rfis/1/edit
def edit
end
def send_rfi
end
def mail
#mail_message = RfiMailer.send_rfi(current_user)
# #mail_message = RfqMailer.placeholder_message(current_user, Rfq.last)
end
# POST /rfis
# POST /rfis.json
def create
#rfi = Rfi.new(rfi_params)
respond_to do |format|
if #rfi.save
format.html { redirect_to mail_rfi_url(#rfi), notice: 'Rfi was successfully created.' }
format.json { render :show, status: :created, location: #rfi }
else
format.html { render :new }
format.json { render json: #rfi.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /rfis/1
# PATCH/PUT /rfis/1.json
def update
respond_to do |format|
if #rfi.update(rfi_params)
format.html { redirect_to #rfi, notice: 'Rfi was successfully updated.' }
format.json { render :show, status: :ok, location: #rfi }
else
format.html { render :edit }
format.json { render json: #rfi.errors, status: :unprocessable_entity }
end
end
end
# DELETE /rfis/1
# DELETE /rfis/1.json
def destroy
#rfi.destroy
respond_to do |format|
format.html { redirect_to rfis_url, notice: 'Rfi was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_rfi
#rfi = Rfi.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def rfi_params
params.require(:rfi).permit(:due, :rfi_type, :parties, :reference, :svg_ref, :vendor_ref, :email_body)
end
end
rfi_mailer.rb
class RfiMailer < ApplicationMailer
default from:"test#test.com"
def send_rfi(user)
mail(to:"someemail", subject:"test")
end
end
first of all, some cleanup
before_action :set_rfi, only: [:show, :edit, :update, :destroy, :mail]
you can also write
before_action :set_rfi, except: [:index, :new]
back to topic
html_part is nil, thats why you cant use the body of it and it throws a exception.
please make sure that your email is having 2 templates, one for the text-part and one for the html-part. further details on that at the rails Doc
http://guides.rubyonrails.org/action_mailer_basics.html#mailer-views

Controller Name Error

I have added a module to my application called Tokened.rb and added the include Tokened to my model. However, now when I try to load that model, I get a "NameError in TestingsController#index" error... I haven't included Tokened in my TestingsController, but not sure why I should or where I should put it.
My code:
testing.rb
class Testing < ActiveRecord::Base
include Tokened
end
My Tokened.rb module:
module Tokened
extend ActiveSupport::Concern
included do
after_initialize do
self.token = generate_token if self.token.blank?
end
end
private
def generate_token
loop do
key = SecureRandom.base64(15).tr('+/=lIO0', 'pqrsxyz')
break key unless self.class.find_by(token: key)
end
end
end
Finally, my testing controller:
class TestingsController < ApplicationController
before_action :set_testing, only: [:show, :edit, :update, :destroy]
# GET /testings
# GET /testings.json
def index
#testings = Testing.all
end
# GET /testings/1
# GET /testings/1.json
def show
end
# GET /testings/new
def new
#testing = Testing.new
end
# GET /testings/1/edit
def edit
end
# POST /testings
# POST /testings.json
def create
#testing = Testing.new(testing_params)
respond_to do |format|
if #testing.save
format.html { redirect_to #testing, notice: 'Testing was successfully created.' }
format.json { render :show, status: :created, location: #testing }
else
format.html { render :new }
format.json { render json: #testing.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /testings/1
# PATCH/PUT /testings/1.json
def update
respond_to do |format|
if #testing.update(testing_params)
format.html { redirect_to #testing, notice: 'Testing was successfully updated.' }
format.json { render :show, status: :ok, location: #testing }
else
format.html { render :edit }
format.json { render json: #testing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /testings/1
# DELETE /testings/1.json
def destroy
#testing.destroy
respond_to do |format|
format.html { redirect_to testings_url, notice: 'Testing was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_testing
#testing = Testing.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def testing_params
params.require(:testing).permit(:name, :address, :signature)
end
end
What gives? I am not sure what is going on here and why it needs to be included in the controller.
First of all it should be lowercased: tokened.rb. But where is your file? It should be here modules/concerns/tokened.rb.

undefined method `belongs_to' for ActiveRecord:Module

I am getting the following error "undefined method `belongs_to' for ActiveRecord:Module" it is showing the following code for my error in line 1.
class Posting < ActiveRecord::
belongs_to :user
validates :content, length: { maximum: 1000 }
end
Also showing an error in this code on line 10
class ProfilesController < ApplicationController
def show
if params[:id].nil? # if there is no user id in params, show current one
#user = current_user
else
#user = User.find(params[:id])
end
#alias = #user.alias
#posting = Posting.new
end
end
The postings controller if it is needed is...
class PostingsController < ApplicationController
before_action :set_posting, only: [:show, :edit, :update, :destroy]
# GET /postings
# GET /postings.json
def index
#postings = Posting.all
end
# GET /postings/1
# GET /postings/1.json
def show
end
# GET /postings/new
def new
#posting = Posting.new
end
# GET /postings/1/edit
def edit
end
# POST /postings
# POST /postings.json
def create
#posting = Posting.new(posting_params)
respond_to do |format|
if #posting.save
format.html { redirect_to #posting, notice: 'Posting was successfully created.' }
format.json { render action: 'show', status: :created, location: #posting }
else
format.html { render action: 'new' }
format.json { render json: #posting.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /postings/1
# PATCH/PUT /postings/1.json
def update
respond_to do |format|
if #posting.update(posting_params)
format.html { redirect_to #posting, notice: 'Posting was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #posting.errors, status: :unprocessable_entity }
end
end
end
# DELETE /postings/1
# DELETE /postings/1.json
def destroy
#posting.destroy
respond_to do |format|
format.html { redirect_to postings_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_posting
#posting = Posting.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def posting_params
params.require(:posting).permit(:content, :user_id)
end
end
You need the Posting class to inherit from ActiveRecord::Base and not just ActiveRecord::

Resources