私はそれらの瞬間を持っています。私の問題はおそらく単純ですが、私はそれを理解していません。Rails 5フォームのフォームデータに連絡する
私は開発しているRails 5アプリを持っています。連絡先に記入して提出するための連絡用紙が含まれています。
次のように私はActionMailerが連絡先に電子メールを送信して、そのメッセージが受信され、レビューされていることを通知しました。また、私はActionMailerを設定して、連絡先フォームデータを含むと思われるWebサイトの管理者に電子メールを送信します。これは私が問題に遭遇しているところです。連絡先フォームのデータを電子メールビューに渡すにはどうすればよいですか?
class ContactsController < ApplicationController
before_action :set_contact, only: [:show, :edit, :update, :destroy]
# GET /contacts
# GET /contacts.
def index
@contacts = Contact.all
end
# GET /contacts/1
# GET /contacts/1.json
def show
end
# GET /contacts/new
def new
@contact = Contact.new
end
# GET /contacts/1/edit
def edit
end
# POST /contacts
# POST /contacts.json
def create
@contact = Contact.new(contact_params)
respond_to do |format|
if @contact.save
FormMailer.message_received(@contact).deliver_now
FormMailer.new_web_message(@email).deliver_now
format.html { redirect_to thank_you_path, notice: 'Your message was sent.' }
format.json { render :show, status: :created, location: @contact }
else
format.html { render :new }
format.json { render json: @contact.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /contacts/1
# PATCH/PUT /contacts/1.json
def update
respond_to do |format|
if @contact.update(contact_params)
format.html { redirect_to @contact, notice: 'Contact was successfully updated.' }
format.json { render :show, status: :ok, location: @contact }
else
format.html { render :edit }
format.json { render json: @contact.errors, status: :unprocessable_entity }
end
end
end
# DELETE /contacts/1
# DELETE /contacts/1.json
def destroy
@contact.destroy
respond_to do |format|
format.html { redirect_to contacts_url, notice: 'Contact was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_contact
@contact = Contact.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def contact_params
params.require(:contact).permit(:first_name, :last_name, :vehicle_type, :tire_size, :current_tire, :phone_number, :email, :message)
end
end
メーラー:
class FormMailer < ApplicationMailer
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.form_mailer.message_received.subject
#
def message_received(contact)
@contact = contact
@url = 'http://example.com'
@greeting = "Hello"
mail(to: @contact.email, subject: 'Thanks for contacting Halstead Tire LLC')
end
def new_web_message
@greeting = "Hello"
@email = '<[email protected]>'
@website = 'http://www.example.com'
mail(to: @email, subject: 'A new message from your website.')
end
end
ビューnew_web_message.html.erb:
<h1><%= @greeting %> Nathan,</h1>
<p>
Someone's requested some information from your site. Please go to <%= @website %> to get the details.
</p>
<body>
</body>
私が正しく送信メールを持っているし、彼らはここで
はcontacts_controllerです期待どおりに配信します。連絡先のメッセージは完全に機能しています。 Admin *(new_web_message)*電子メールで連絡先の情報を取得して送信します。これらの値をビューにどのように渡すのですか?私はcontact_params *(contacts_controllerから)*をすべて取り出し、管理者に電子メールで伝えたいと思います。私はこれが単純で愚かな間違いかもしれないことに気付きますが、私はそれを理解できません。 1つのメーラーでこれをすべて実行することは可能ですか、連絡先と管理者*(new_web_message)*のメールを別々にする必要がありますか? *私は電子メールが今、1つのメーラーでうまく動作していると言っていますが、管理者**(new_web_message)*電子メールに渡されたフォームからデータを取得する必要があります。
接触して、それはメーラーに明らかであるように、などのデータにアクセスするために働き、電子メール:
@contact.email
@contact.phone_number
@contact.message
など。なぜこの同じ理論が管理者の電子メールで機能しないのですか?
私のメーラーの名前はコントローラー(contactsMailer)の代わりに(contacts_mailer)と同じ名前にする必要がありますか?
ありがとうございます!
EDIT:
だから私は私のアプリではライアンのコードを実装しますが、今私が手:
NoMethodError in Contacts#create
ここnew_web_message.html.erb
<h1><%= @greeting %> Nathan,</h1>
<p>
Someone's requested some information from your site. Please go to <%= @website %> to get the details.
</p>
<body>
<p><%= @contact.first_name %></p>
<p><%= @contact.last_name %></p>
</body>
がここに示した誤差があるさフォームを送信すると、
Showing /Users/evan/htire/app/views/form_mailer/new_web_message.html.erb where line #7 raised:
undefined method `first_name' for nil:NilClass
Extracted source (around line #7):
</p>
<body>
<p><%= @contact.first_name %></p> (line 7)
<p><%= @contact.last_name %></p> (line 8)
</body>
どうやらどこかでメソッドを定義する必要がありますが、アイディアが不足しています。 (はい、レールでちょっと新鮮です)
ここはモデルの連絡先です。RB
class Contact < ApplicationRecord
def new_web_message
first_name = first_name
last_name = last_name
end
attr_accessor :first_name, :last_name, :vehicle_type, :tire_size, :current_tire, :phone_number, :email, :message
validates_presence_of :first_name
validates_presence_of :last_name
validates_presence_of :phone_number
validates_presence_of :message
end
ライアンの提案を使用して更新メーラー:私は間違って何をやっている
class ContactsController < ApplicationController
before_action :set_contact, only: [:show, :edit, :update, :destroy]
# GET /contacts
# GET /contacts.json
def index
@contacts = Contact.all
end
# GET /contacts/1
# GET /contacts/1.json
def show
end
# GET /contacts/new
def new
@contact = Contact.new
end
# GET /contacts/1/edit
def edit
end
# POST /contacts
# POST /contacts.json
def create
@contact = Contact.new(contact_params)
respond_to do |format|
if @contact.save
FormMailer.message_received(@contact).deliver_now
FormMailer.new_web_message(@email, params).deliver_now
format.html { redirect_to thank_you_path, notice: 'Your message was sent.' }
format.json { render :show, status: :created, location: @contact }
else
format.html { render :new }
format.json { render json: @contact.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /contacts/1
# PATCH/PUT /contacts/1.json
def update
respond_to do |format|
if @contact.update(contact_params)
format.html { redirect_to @contact, notice: 'Contact was successfully updated.' }
format.json { render :show, status: :ok, location: @contact }
else
format.html { render :edit }
format.json { render json: @contact.errors, status: :unprocessable_entity }
end
end
end
# DELETE /contacts/1
# DELETE /contacts/1.json
def destroy
@contact.destroy
respond_to do |format|
format.html { redirect_to contacts_url, notice: 'Contact was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_contact
@contact = Contact.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def contact_params
params.require(:contact).permit(:first_name, :last_name, :vehicle_type, :tire_size, :current_tire, :phone_number, :email, :message)
end
end
:
class FormMailer < ApplicationMailer
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.form_mailer.message_received.subject
#
def message_received(contact)
@contact = contact
@url = 'http://halsteadtire.net'
@greeting = "Hello"
mail(to: @contact.email, subject: 'Thanks for contacting Halstead Tire LLC')
end
def new_web_message(contact, params)
@contact = contact
@greeting = "Hello"
@email = '<[email protected]>'
@website = 'http://www.halsteadtire.net/contacts'
mail(to: @email, subject: 'A new message from your website.')
end
end
はライアンのコードを使用してコントローラを更新しましたか?