不断收到 "undefined method `email' for nil:NilClass" 我做错了什么?
Keep getting "undefined method `email' for nil:NilClass" what am I doing wrong?
这就是我的控制器的样子。我想单击一个按钮并将电子邮件发送到当前报价中的电子邮件。如果我在其中放置一个字符串电子邮件,它会起作用,但是当我将它交换为一个 @quote.email 从当前对象中获取电子邮件时,它会给我这个错误:
"undefined method `email' for nil:NilClass"
我不知道为什么会这样,请有人帮忙!
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@joinbennett.com')
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
创建报价时,它有一封电子邮件。我已经为 运行 这个名为 SendQuoteEmail 的操作创建了这个按钮。
<%= button_to 'Send Quote Email', quotes_sendQuoteEmail_path, method: :post, class: 'btn btn-success mt-3' %>
我做错了什么?我一直说 @quote.email 是 NIL??
改变你的:
before_action :set_quote, only: [:show, :edit, :update, :destroy]
到
before_action :set_quote, only: [:show, :edit, :update, :destroy, : sendQuoteEmail]
这将使 Rails 运行 set_quote
方法使控制器能够在你对它采取行动之前找到 @quote
对象。
在路径助手中添加@quote
:
<%= button_to 'Send Quote Email', quotes_sendQuoteEmail_path(@quote) ...
在您的控制器中,更新此行:
before_action :set_quote, only: [:show, :edit, :update, :destroy, :sendQuoteEmail]
然后在你看来,传递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' %>
这就是我的控制器的样子。我想单击一个按钮并将电子邮件发送到当前报价中的电子邮件。如果我在其中放置一个字符串电子邮件,它会起作用,但是当我将它交换为一个 @quote.email 从当前对象中获取电子邮件时,它会给我这个错误:
"undefined method `email' for nil:NilClass"
我不知道为什么会这样,请有人帮忙!
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@joinbennett.com')
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
创建报价时,它有一封电子邮件。我已经为 运行 这个名为 SendQuoteEmail 的操作创建了这个按钮。
<%= button_to 'Send Quote Email', quotes_sendQuoteEmail_path, method: :post, class: 'btn btn-success mt-3' %>
我做错了什么?我一直说 @quote.email 是 NIL??
改变你的:
before_action :set_quote, only: [:show, :edit, :update, :destroy]
到
before_action :set_quote, only: [:show, :edit, :update, :destroy, : sendQuoteEmail]
这将使 Rails 运行 set_quote
方法使控制器能够在你对它采取行动之前找到 @quote
对象。
在路径助手中添加@quote
:
<%= button_to 'Send Quote Email', quotes_sendQuoteEmail_path(@quote) ...
在您的控制器中,更新此行:
before_action :set_quote, only: [:show, :edit, :update, :destroy, :sendQuoteEmail]
然后在你看来,传递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' %>