I installed 'google_places' gem on rails 5 but could not get it to work.When I run this code snippet:
#client = GooglePlaces::Client.new(Rails.application.secrets.places_api_key)
It gives such an error:
uninitialized constant PlacesController::GooglePlaces
And my controller:
class PlacesController < ApplicationController
before_action :set_place, only: [:show, :edit, :update, :destroy]
# GET /places
# GET /places.json
def index
#places = Place.all
end
# GET /places/nearbies
# GET /places/nearbies.json
def nearbies
#client = GooglePlaces::Client.new(Rails.application.secrets.places_api_key)
#places = #client.spots(-33.8670522, 151.1957362)
end
# GET /places/1
# GET /places/1.json
def show
end
# GET /places/new
def new
#place = Place.new
end
# GET /places/1/edit
def edit
end
# POST /places
# POST /places.json
def create
#place = Place.new(place_params)
respond_to do |format|
if #place.save
format.html { redirect_to #place, notice: 'Place was successfully created.' }
format.json { render :show, status: :created, location: #place }
else
format.html { render :new }
format.json { render json: #place.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /places/1
# PATCH/PUT /places/1.json
def update
respond_to do |format|
if #place.update(place_params)
format.html { redirect_to #place, notice: 'Place was successfully updated.' }
format.json { render :show, status: :ok, location: #place }
else
format.html { render :edit }
format.json { render json: #place.errors, status: :unprocessable_entity }
end
end
end
# DELETE /places/1
# DELETE /places/1.json
def destroy
#place.destroy
respond_to do |format|
format.html { redirect_to places_url, notice: 'Place was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_place
#place = Place.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def place_params
params.require(:place).permit(:title, :address, :latitude, :longitude)
end
end
I did not solve it, I appreciate your help.
EDIT:
Its my fault. I accidentally built the google-places gem
Need to do this
class PlacesController < ApplicationController
require 'google-places'
before_action :set_place, only: [:show, :edit, :update, :destroy]
# GET /places
# GET /places.json
def index
#places = Place.all
end
Related
i have ProjectSite model and ManagerRemark model related to many to one association. my MangerRemark model has boolean value true and false i want to access that boolean value to other controller view. please help. here is my code.i want to print decision boolean value next to each project site index list how can i do that? in other controller name new_manager_controller view
project_sites_controller.rb
class ProjectSitesController < ApplicationController
before_action :authenticate_user!
before_action :is_project_site?, except: [:show]
before_action :set_project_site, only: [:show, :edit, :update, :destroy]
# GET /project_sites
# GET /project_sites.json
def index
#project_sites = ProjectSite.all.order("created_at DESC")
end
# GET /project_sites/1
# GET /project_sites/1.json
def show
#manager_remark = ManagerRemark.new
#manager_remark.project_site_id = #project_site.id
end
# GET /project_sites/new
def new
#project_site = ProjectSite.new
end
# GET /project_sites/1/edit
def edit
end
# POST /project_sites
# POST /project_sites.json
def create
#project_site = ProjectSite.new(project_site_params)
respond_to do |format|
if #project_site.save
format.html { redirect_to #project_site, notice: 'Project site was successfully created.' }
format.json { render :show, status: :created, location: #project_site }
else
format.html { render :new }
format.json { render json: #project_site.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /project_sites/1
# PATCH/PUT /project_sites/1.json
def update
respond_to do |format|
if #project_site.update(project_site_params)
format.html { redirect_to #project_site, notice: 'Project site was successfully updated.' }
format.json { render :show, status: :ok, location: #project_site }
else
format.html { render :edit }
format.json { render json: #project_site.errors, status: :unprocessable_entity }
end
end
end
# DELETE /project_sites/1
# DELETE /project_sites/1.json
def destroy
#project_site.destroy
respond_to do |format|
format.html { redirect_to project_sites_url, notice: 'Project site was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project_site
#project_site = ProjectSite.find(params[:id])
end
# Never trust parameters frmanager_level_twoom the scary internet, only allow the white list through.
def project_site_params
params.require(:project_site).permit(:name, :date, :file)
end
def is_project_site?
redirect_to root_path unless (current_user.role=='project_site')
end
end
This is how my manage remark controller looks.
Manager_Remarks_controller.rb
class ManagerRemarksController < ApplicationController
def create
#manager_remark = ManagerRemark.new(remark_params)
#manager_remark.project_site_id = params[:project_site_id]
#manager_remark.save
redirect_to project_site_path(#manager_remark.project_site)
end
def remark_params
params.require(:manager_remark).permit(:name, :remark, :decision)
end
end
My issue is, when I am under the camper show page
Current Camper URL:
campers/1
and I go to click on to view the appointment it uses the camper_id for the appointment_id which is wrong so say if the camper_id is 1 it will use the appointment_id as 1 and actually the appointment id is 3, so then it says Couldn't find appointment with id of 1.
Table Header
<% #appointments.each do |app| %>
<%= link_to app.camper.camperName, appointment_path(#camper, #appointment) %>
Campers Controller Show Action
#appointments = #camper.appointments
Camper Model
has_many :appointments, dependent: :destroy
Appointment Model
belongs_to :camper
Shallow Nested Routes File
resources :customers, shallow: :true do
resources :campers do
resources :appointments do
resources :orders do
member do
patch :complete
end
end
end
end
end
Camper Controller
class CampersController < ApplicationController
before_action :set_camper, only: [:show, :edit, :update, :destroy]
# before_action :set_customer, only: [:index, :new, :edit, :create, :update]
load_and_authorize_resource
# GET /campers
# GET /campers.json
def index
#campers = #customer.campers
end
def list
query = params[:q].presence || ""
#campers = Camper.search(query, page: params[:page], per_page: 20, order: {created_at: :desc} )
end
# GET /campers/1
# GET /campers/1.js
def show
#appointments = #camper.appointments
respond_to do |format|
format.html
format.json
end
end
# GET /campers/new
def new
#customer = Customer.find(params[:customer_id])
#camper = #customer.campers.build
end
# GET /campers/1/edit
def edit
end
def page_name
"Campers"
end
# POST /campers
# POST /campers.json
def create
#camper = Camper.new(camper_params)
respond_to do |format|
if #camper.save
format.html { redirect_to camper_path(#camper), notice: 'Camper was successfully created.' }
format.json { render :show, status: :created, location: #camper }
else
format.html { render :new }
format.json { render json: #camper.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /campers/1
# PATCH/PUT /campers/1.json
def update
respond_to do |format|
if #camper.update(camper_params)
format.html { redirect_to camper_path(#camper), notice: 'Camper was successfully updated.' }
format.json { render :show, status: :ok, location: #camper }
else
format.html { render :edit }
format.json { render json: #camper.errors, status: :unprocessable_entity }
end
end
end
# DELETE /campers/1
# DELETE /campers/1.json
def destroy
#camper.destroy
respond_to do |format|
format.html { redirect_to root_path, notice: 'Camper was successfully deleted.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_camper
#camper = Camper.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def camper_params
params.require(:camper).permit(:order_id, :customer_id, :year, :manufacturer, :modelName, :camperClass, :vin, :mileage, :notes, :user_id)
end
end
Appointments Controller
class AppointmentsController < ApplicationController
before_action :set_appointment, only: [:show, :edit, :update, :destroy]
# GET /appointments
# GET /appointments.json
def index
#camper = Camper.find(params[:camper_id])
#appointments = #camper.appointments
end
# GET /appointments/1
# GET /appointments/1.json
def show
#orders = #appointment.orders
end
# GET /appointments/newå
def new
#camper = Camper.find(params[:camper_id])
#appointment = #camper.appointments.build
end
# GET /appointments/1/edit
def edit
end
# POST /appointments
# POST /appointments.json
def create
#appointment = Appointment.new(appointment_params)
respond_to do |format|
if #appointment.save
format.html { redirect_to appointment_path(#appointment), notice: 'Appointment was successfully created.' }
format.json { render :show, status: :created, location: #appointment }
else
format.html { render :new }
format.json { render json: #appointment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /appointments/1
# PATCH/PUT /appointments/1.json
def update
respond_to do |format|
if #appointment.update(appointment_params)
format.html { redirect_to #appointment, notice: 'Appointment was successfully updated.' }
format.json { render :show, status: :ok, location: #appointment }
else
format.html { render :edit }
format.json { render json: #appointment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /appointments/1
# DELETE /appointments/1.json
def destroy
#appointment.destroy
respond_to do |format|
format.html { redirect_to camper_appointments_path(#appointment), notice: 'Appointment was successfully deleted.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_appointment
#appointment = Appointment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def appointment_params
params.require(:appointment).permit(:customer_id, :camper_id, :order_id, :title, :description, :date_in, :date_out)
end
end
appointment_path only takes a single appointment argument. Remove the #camper argument:
appointment_path(#appointment)
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
I have a problem, before the command "rails generate scaffold test name: string" generated controllers like this:
class Teste < ApplicationController
before_action :set_teste, only: [:show, :edit, :update, :destroy
# GET /testes
# GET /testes.json
def index
#testes = Teste.all
end
# GET /testes/1
# GET /testes/1.json
def show
end
# GET /testes/new
def new
#teste = Teste.new
end
# GET /testes/1/edit
def edit
end
# POST /testes
# POST /testes.json
def create
#teste = Teste.new(teste_params)
respond_to do |format|
if #teste.save
format.html { redirect_to testes_path, notice: 'Teste cadastrado.' }
format.json { render :show, status: :created, location: #teste }
else
format.html { render :new }
format.json { render json: #teste.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /testes/1
# PATCH/PUT /testes/1.json
def update
respond_to do |format|
if #teste.update(teste_params)
format.html { redirect_to testes_path, notice: 'Teste atualizado.' }
format.json { render :show, status: :ok, location: #teste }
else
format.html { render :edit }
format.json { render json: #teste.errors, status: :unprocessable_entity }
end
end
end
# DELETE /testes/1
# DELETE /testes/1.json
def destroy
#teste.destroy
respond_to do |format|
format.html { redirect_to testes_url, notice: 'Teste excluído.' }
format.json { head :no_content }
end
end
Do not know why, but this is now generating another format
class TestesController < ApplicationController
before_action :set_teste, only: [:show, :edit, :update, :destroy]
def index
#testes = Teste.all
respond_with(#testes)
end
def show
respond_with(#teste)
end
def new
#teste = Teste.new
respond_with(#teste)
end
def edit
end
def create
#teste = Teste.new(teste_params)
#teste.save
respond_with(#teste)
end
def update
#teste.update(teste_params)
respond_with(#teste)
end
What can it be? Why has this changed?
I would return in the previous format because my whole system is in the first format
The reason of this behavior is probably some gem.
I had same problem because latest version of Devise which is now shipped with Responders gem.
If this is your case too then quick fix is add following line to aplication.rb file:
config.app_generators.scaffold_controller :scaffold_controller
More info here:
https://github.com/rails/rails/issues/17290
https://github.com/plataformatec/responders/issues/94
I have a Listings Controller where Users can Create their Listings.
To prevent users to edit other users listings i just had to update every action from
Listing to current_user.listings
but with Rails 4 the controller got changed and i can't find how to set this up.
My Controller File->
class ListingsController < ApplicationController
before_action :set_listing, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!, :only => [:index]
# GET /listings
# GET /listings.json
def index
#listings = Listing.all
end
# GET /listings/1
# GET /listings/1.json
def show
end
# GET /listings/new
def new
#listing = Listing.new
end
# GET /listings/1/edit
def edit
end
# POST /listings
# POST /listings.json
def create
#listing = Listing.new(listing_params)
respond_to do |format|
if #listing.save
format.html { redirect_to #listing, notice: 'Listing was successfully created.' }
format.json { render action: 'show', status: :created, location: #listing }
else
format.html { render action: 'new' }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /listings/1
# PATCH/PUT /listings/1.json
def update
respond_to do |format|
if #listing.update(listing_params)
format.html { redirect_to #listing, notice: 'Listing was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /listings/1
# DELETE /listings/1.json
def destroy
#listing.destroy
respond_to do |format|
format.html { redirect_to listings_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_listing
#listing = Listing.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def listing_params
params.require(:listing).permit(:title, :description)
end
end
Anyone knows a Solution ?
change from #new to build. So, change all #listing = Listing.new to:
#listing = current_user.listings.build
Then, in set_listing change to:
#listing = current_user.listings.find(params[:id])