Reputation: 85
So this is what my controller looks like. I want to click a button and have an email be sent to the email in the current quote. If I put a string email in there it works, but when I swap it to have a @quote.email grabbing the email from the current object it gives me this error:
"undefined method `email' for nil:NilClass"
I have no idea why this is doing this, someone please help!
class QuotesController < ApplicationController
require 'sendgrid-ruby'
include SendGrid
before_action :set_quote, only: [:show, :edit, :update, :destroy]
# GET /quotes
# GET /quotes.json
def index
@quotes = Quote.all
end
def thankyoupage
end
# GET /quotes/1
# GET /quotes/1.json
def show
end
# GET /quotes/new
def new
@quote = Quote.new
end
# GET /quotes/1/edit
def edit
end
# POST /quotes
# POST /quotes.json
def create
@quote = Quote.new(quote_params)
if @quote.save
redirect_to thankyou_path
else
redirect_to root
end
end
# PATCH/PUT /quotes/1
# PATCH/PUT /quotes/1.json
def update
respond_to do |format|
if @quote.update(quote_params)
format.html { redirect_to @quote, notice: 'Quote was successfully updated.' }
format.json { render :show, status: :ok, location: @quote }
else
format.html { render :edit }
format.json { render json: @quote.errors, status: :unprocessable_entity }
end
end
end
# DELETE /quotes/1
# DELETE /quotes/1.json
def destroy
@quote.destroy
respond_to do |format|
format.html { redirect_to quotes_url, notice: 'Quote was successfully destroyed.' }
format.json { head :no_content }
end
end
def sendQuoteEmail
from = SendGrid::Email.new(email: '[email protected]')
to = SendGrid::Email.new(email: @quote.email)
subject = 'Sending with Twilio SendGrid is Fun'
content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
mail = SendGrid::Mail.new(from, subject, to, content)
sg = SendGrid::API.new(api_key: '*****************')
response = sg.client.mail._('send').post(request_body: mail.to_json)
puts response.status_code
puts response.body
puts response.headers
end
private
# Use callbacks to share common setup or constraints between actions.
def set_quote
@quote = Quote.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def quote_params
params.fetch(:quote, {}).permit(:first_name, :last_name, :phone_number, :address, :email, :quote_date, :quote_time, :quote_bedroom, :quote_bathroom, :quote_notes, :quote_price, :quote_price_recurring)
end
end
When a Quote is created it has an email. I've created this button to run this action called SendQuoteEmail.
<%= button_to 'Send Quote Email', quotes_sendQuoteEmail_path, method: :post, class: 'btn btn-success mt-3' %>
What am I doing wrong? I keeps saying the @quote.email is NIL??
Upvotes: 1
Views: 155
Reputation: 13014
Change your:
before_action :set_quote, only: [:show, :edit, :update, :destroy]
to
before_action :set_quote, only: [:show, :edit, :update, :destroy, : sendQuoteEmail]
This will let Rails run set_quote
method which will enable controller to find @quote
object before you act on it.
Add @quote
in path helper:
<%= button_to 'Send Quote Email', quotes_sendQuoteEmail_path(@quote) ...
Upvotes: 1
Reputation: 1333
in your controller, update this line:
before_action :set_quote, only: [:show, :edit, :update, :destroy, :sendQuoteEmail]
then in your view, pass the id. quotes_sendQuoteEmail_path(:id => @quote.id)
<%= button_to 'Send Quote Email', quotes_sendQuoteEmail_path(:id => @quote.id), method: :post, class: 'btn btn-success mt-3' %>
Upvotes: 3