当我使用Sendgrid Rails Heroku发送电子邮件时,如何获取或访问不同的模型属性

我正在使用装饰和搭建教科书。 我喜欢实施我的策略。 当买家点击@ textbook.title时 – >买家可以向@教科书的卖家发送电子邮件。 我的每个模型都有’user_email’列。因此,每当卖家创建一个@textbook时,自动将current_user.email保存到@ textbook.user_email中。

我只是不知道如何抓住卖家的user_email并发送电子邮件。

我有以下

教科书模型:

class Textbook  true validates :subject, :presence => true validates :price, :presence => true validates :offer, :presence => false validates :created_at, :presence => false validates :user_email, :presence => true validates :description, :presence => true end 

我不确定这个模型语法是否适合主题和current_user.email联系模型:

 class Contact  true attribute :current_user.email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i attribute :message, :validate => true def headers { :subject => "I like to buy #{@textbook.id.title}", :to => "@textbook.id.user_email", :from => %() } end end 

我的详细问题是:如果用户点击“联系人”,当买家在特定教科书中时,它会将用户链接到教科书#show。 以下是用户点击“联系”时的表单。

如何确保以下视图访问正确的textbook.id或textbook.title?

 

Contact to the Seller

Send email for:



特别是,我不知道如何在不同视图中处理来自不同模型的抓取属性。

先感谢您!

! – # – # – # – # – # – # – # – # – # – # – # – # – # – # – # – # – !

更新1:

我有这样的联系控制器:

 class ContactsController < ApplicationController def new @contact = Contact.new end def create @contact = Contact.new(params[:contact]) #@contact.request = request if @contact.deliver flash[:success] = "Email sent." else flash[:alert] = "Cannot send an email." render :new end end end 

我刚编辑了我的’class Contact <MailForm :: Base'

 class Contact  true attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i attribute :message, :validate => true def headers { :subject => "I like to buy #{textbook.title}", :to => "@textbook.user_email", :from => %() } end 

结束

但是我得到了错误:

 NameError in ContactsController#create undefined local variable or method `textbook' for # Extracted source (around line #8): def headers { :subject => "I like to buy #{textbook.title}", :to => "@textbook.user_email", :from => %() } 

@zeiv我修复了textbook.title – > @ textbook.title我得到错误另一个错误。

 NoMethodError in ContactsController#create undefined method `title' for nil:NilClass def headers { :subject => "I like to buy #{@textbook.title}", :to => "@textbook.user_email", :from => %() } 

我有views / textbooks.html.erb:

 

Title:

Subject:

Price: $

Accept Offer:

Description:


Image:


Created on:

|

我有textbooks_controller:

 class TextbooksController  params[:page], :per_page => 10) end # GET /textbooks/1 # GET /textbooks/1.json def show end 

我有配置/路由:

 resources :textbooks resources :contacts, only: [:new, :create] devise_for :users 

当我在4/17下午5:05分耙路线时

 new_textbook GET /textbooks/new(.:format) textbooks#new edit_textbook GET /textbooks/:id/edit(.:format) textbooks#edit textbook GET /textbooks/:id(.:format) textbooks#show PATCH /textbooks/:id(.:format) textbooks#update PUT /textbooks/:id(.:format) textbooks#update DELETE /textbooks/:id(.:format) textbooks#destroy contacts POST /contacts(.:format) contacts#create new_contact GET /contacts/new(.:format) contacts#new 

更新2 – !# – !# – !# – !# – !# – !# – !# – !# – !# – !# – !# – !# – !# – !# – !# – !# – !

以下是2016年4月17日晚11点以后@zeiv我做了你告诉我的事。 但是当我点击views / textbooks / show.html.erb中的“联系人”按钮时,我仍然会收到错误

 #views/textbooks/show.html.erb 

我的routes.rb现在有:

 Rails.application.routes.draw do resources :textbooks do member do get 'contact', to: 'textbooks#new_contact', as: 'new_contact' post 'contact', to: 'textbooks#send_contact', as: 'send_contact' end end 

耙路线现在:

 Prefix Verb URI Pattern Controller#Action new_contact_textbook GET /textbooks/:id/contact(.:format) textbooks#new_contact send_contact_textbook POST /textbooks/:id/contact(.:format) textbooks#send_contact textbooks GET /textbooks(.:format) textbooks#index POST /textbooks(.:format) textbooks#create new_textbook GET /textbooks/new(.:format) textbooks#new edit_textbook GET /textbooks/:id/edit(.:format) textbooks#edit textbook GET /textbooks/:id(.:format) textbooks#show PATCH /textbooks/:id(.:format) textbooks#update PUT /textbooks/:id(.:format) textbooks#update DELETE /textbooks/:id(.:format) textbooks#destroy 

我得到的错误是这样的:

 NoMethodError in Textbooks#new_contact undefined method `id' for nil:NilClass Extracted source (around line #4): 
Texbook id is:

我正在运行heroku本地错误显示:

 10:56:13 PM web.1 | Rendered textbooks/new_contact.html.erb within layouts/application (2.0ms) 10:56:13 PM web.1 | Completed 500 Internal Server Error in 7ms (ActiveRecord: 0.1ms) 10:56:13 PM web.1 | ActionView::Template::Error (undefined method `id' for nil:NilClass): 10:56:13 PM web.1 | 1: 

contact seller! - working?

10:56:13 PM web.1 | 2: 10:56:13 PM web.1 | 3:
10:56:13 PM web.1 | 4: Texbook id is: 10:56:13 PM web.1 | 5:

基本上你需要做的是编写邮件和控制器,使你想要的所有信息都传递给邮件程序。 因此,如果您希望将教科书模型的实例传递给邮件程序,则需要从发送电子邮件的控制器执行此操作。 您可能希望在您的教科书路线中嵌套您的联系人控制器路线以帮助您。 或者,不要让整个控制器用于联系,只需在教科书控制器中进行联系操作即可

 # route.rb ... resources :textbooks do member do get "contact", to: "textbooks#new_contact", as: "new_contact" post "contact", to: "textbooks#send_contact", as: "send_contact" end end 

这将为您提供/textbook/24/contact等路线。 member do表示路由是针对模型的单个实例而不是整个集合,因此您需要在调用其帮助程序时指定要引用的教科书: new_contact_textbook_path(@textbook.id)

所以在你的教科书控制器中,你会这样做:

 # textbooks_controller.rb before_action :set_textbook, only: [:show, :edit, :update, :destroy, :new_contact, :send_contact] ... def new_contact # We are NOT doing Contact.new here # Only put logic here that you need to display the form end def send_contact message = params[:message] if Contact.send_contact(@textbook, current_user, message).deliver flash[:success] = "Email sent." redirect_to @textbook else flash[:alert] = "There was a problem sending the email." render :new_contact end end 

然后将new_contact.html.erb文件与您的其他Textbook视图一起放入。

 

Contact to the Seller

<%= form_tag send_contact_textbook_path(@textbook.id) do %>

Send email for: <%=@textbook.title%>

<%= label_tag :message, "Type your message:" %>
<%= text_area_tag :message %>
<%= submit_tag 'Send message', class: 'button' %> <%end%>

请注意,我使用的是form_tag而不是form_for因为我们没有Contact对象来传递它。 (也就是说,联系人不是模特。这是一个邮件。)

您的邮件将看起来像这样:

 class Contact < ApplicationMailer def send_contact(textbook, current_user, message) @textbook = textbook @current_user = current_user @message = message mail( from: "#{@current_user.name} <#{@current_user.email}>", to: @textbook.user.email, subject: "I would like to buy #{@textbook.title}", reply_to: @current_user.email, ) end end 

最后,在/app/views/contact/send_contact.html.erb为您邮寄模板/视图:

       

<%= @current_user.name %> is wondering about <%= @textbook.title %>:

<%= @message %>

这应该做到! 虽然您可能需要调整一些东西以满足您的需求。 有关更多示例,请参阅以下链接:

在Rails中联系Form Mailer 4

https://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html