前提・実現したいこと
Ruby on Railsにて新商品の紹介サイトを作っています。商品を検索して気になる物が見つかった場合にメールを担当者に送れるようにしたいです。メールの送り先を1つだけなら実装できましたが、所属ごとにメール送り先を切り分けて担当者に直接届くようにしたいです。メール情報@contactにuserをアソシエーションさせuser_idから情報を取り出し、if文にて送信先を変えたいですが、user_idから情報を取得できません。
発生している問題・エラーメッセージ
[1] pry(#<ContactMailer>)> @contact => #<Contact:0x00007f8278930678 id: 6, contact_user_name: "あ", contact_user_email: "aaa@aaa", contact_user_occupation: "ああ", contact_manufacturer: "あああ", contact_item_name: "ああああ", message: "あああああ", user_id: 1, created_at: Sat, 29 Jan 2022 02:10:24 UTC +00:00, updated_at: Sat, 29 Jan 2022 02:10:24 UTC +00:00> [2] pry(#<ContactMailer>)> user_id NameError: undefined local variable or method `user_id' for #<ContactMailer:0x00007f8279711a08>
該当のソースコード
class CreateContacts < ActiveRecord::Migration[6.0] def change create_table :contacts do |t| t.string :contact_user_name, null: false t.string :contact_user_email, null: false t.string :contact_user_occupation, null: false t.string :contact_manufacturer, null: false t.string :contact_item_name, null: false t.text :message t.references :user, foreign_key: true t.timestamps end end end
UserTB
1create_table :users do |t| 2 ## Database authenticatable 3 t.string :email, null: false, default: "" 4 t.string :encrypted_password, null: false, default: "" 5 t.string :name, null: false 6 t.string :name_ruby, null: false 7 t.string :occupation, null: false 8 t.string :position, null: false 9 t.integer :page_select_id, null: false 10 t.boolean :admin, default: false
ContactModel
1class Contact < ApplicationRecord 2 has_many :users, through: :user_contacts 3 has_many :user_contacts 4 5 with_options presence: true do 6 validates :contact_user_name 7 validates :contact_user_email 8 validates :contact_user_occupation 9 validates :contact_manufacturer 10 validates :contact_item_name 11 end 12end
MailerHtml
1<%= form_with(model: contact, local: true) do |form| %> 2 <% if contact.errors.any? %> 3 <div id="error_explanation"> 4 <h2><%= pluralize(contact.errors.count, "error") %> prohibited this contact from being saved:</h2> 5 6 <ul> 7 <% contact.errors.full_messages.each do |message| %> 8 <li><%= message %></li> 9 <% end %> 10 </ul> 11 </div> 12 <% end %> 13 14 <div class="field"> 15 <%= form.label :'お名前' %> 16 <%= form.text_field :current_user.name %> 17 </div> 18 19 <div class="field"> 20 <%= form.label :'メールアドレス' %> 21 <%= form.text_field :current_user.email %> 22 </div> 23 24 <div class="field"> 25 <%= form.label :'店名' %> 26 <%= form.text_field :current_user.occupation %> 27 </div> 28 29 <div class="field"> 30 <%= form.label :'メーカー名' %> 31 <%= form.text_field :email %> 32 </div> 33 34 <div class="field"> 35 <%= form.label :'商品名' %> 36 <%= form.text_field :email %> 37 </div> 38 39 <div class="field"> 40 <%= form.label :'メッセージ' %> 41 <%= form.text_area :content %> 42 </div> 43 44 <div class="actions"> 45 <%= form.submit %> 46 </div> 47<% end %>
contactMailer
1class ContactMailer < ApplicationMailer 2 def contact_mail(contact) 3 @contact = contact 4 binding.pry 5 if contact.user.page_select_id == 1 6 mail to:"ktnstn.237rogs@gmail.com",subject: "サンプル依頼が届きました。" 7 elsif contact.user.page_select_id == 2 8 mail to:"k-watanabe@net-nakayama.co.jp",subject: "成功" 9 else 10 mail to:"tessin1396@icloud.com",subject: "完璧!" 11 end 12 end 13end
アソシエーションの書き方、paramusのmargeには設定してあります。
よろしくお願い致します。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/30 06:13