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

问题描述:

我正在使用设计和脚手架教材。 我喜欢实施我的策略。 买家点击@ textbook.title时 - >买家可以发送电子邮件给@教科书的卖家。 我有每个模型都有'user_email'列 所以,无论何时卖家创建一个@textbook,自动将current_user.email保存到@ textbook.user_email。当我使用Sendgrid Rails Heroku发送电子邮件时,如何获取或访问不同的模型属性

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

我有以下

教科书上的模型:

class Textbook < ActiveRecord::Base 

    belongs_to :user 

    validates :title, :presence => 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 < MailForm::Base 
    attribute :name,  :validate => 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 => %(<#{email}>) 
     } 
    end 
end 

我的细节问题是这样的: 如果用户点击“联系”,当买家在一个特定的教科书蚂蚁内,它将用户链接到教科书#s怎么样。以下是用户点击“联系人”时的表单。

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

<h1> Contact to the Seller </h1> 

<div> 
    <%=form_for @contact do |f|%> 
     <h3>Send email for: <%[email protected]%> </h3> 

     <%= f.label :message %><br> 
     <%= f.text_area :message, as: :text %><br> 

     <%=f.submit 'Send message', class: 'button' %> 
    <%end%> 
</div> 

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

预先感谢您!

- !# - !# - !# - !# - !# - !# - !# - !# - !# - !# - !# - !# - !# - !# - !## - !# - !

更新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 

我刚编辑我的 '阶级联系< MailForm ::基地'

class Contact < MailForm::Base 
attribute :name,  :validate => 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 => %(<#{current_user.email}>) 
    } 
end 

但我得到错误:

NameError in ContactsController#create 
undefined local variable or method `textbook' for #<Contact:0x007fbac641be40> 

Extracted source (around line #8):   
    def headers 
     { 
      :subject => "I like to buy #{textbook.title}", 
      :to => "@textbook.user_email", 
      :from => %(<#{current_user.email}>) 
     } 

@zeiv I fixed 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 => %(<#{current_user.email}>) 
     } 

我有意见/ textbooks.html。ERB:

<div class="container"> 

     <p> 
      <h3><strong>Title:</strong> 
      <%= @textbook.title %></h3> 
     </p> 

     <p> 
      <strong>Subject:</strong> 
      <%= @textbook.subject %> 
     </p> 

     <p> 
      <strong>Price:</strong> 
      $<%= @textbook.price %> 
     </p> 

     <p> 
      <strong>Accept Offer:</strong> 
      <%if @textbook.offer == true%> 
      <%='Yes'%> 
      <%else%> 
      <%='No'%> 
      <%end%> 
     </p> 

     <p> 
      <strong>Description:</strong> 
      <pre><%= @textbook.description %></pre> 
     </p> 

     <p> 
      <strong>Image:</strong> 
      <pre><%= image_tag @textbook.thumbnail.url(:medium) %></pre> 
     </p> 

     <p> 
      <strong>Created on:</strong> 
      <%= @textbook.created_at.strftime("%d %b. %Y") %> 
     </p> 

     <p> 
      <%= link_to 'Contact', new_contact_path %> 
     </p> 

     <%if @textbook.user_email == current_user.email %> 
      <%= link_to 'Edit', edit_textbook_path(@textbook) %> | 
      <%= link_to 'Back to list', textbooks_path %> 
     <%else %> 
      <%= link_to 'Back to list', textbooks_path %> 
     <%end%> 

而且我有textbooks_controller:

class TextbooksController < ApplicationController 
    before_action :set_textbook, only: [:show, :edit, :update, :destroy] 
    #before_action :set_textbook, only: [:show] 
    #before_action :authorize_resource!, except: [:new, :index, :show] 

    # GET /textbooks 
    # GET /textbooks.json 
    def index 
    #@textbooks = Textbook.all 
    @textbooks = Textbook.all.order(created_at: :desc).paginate(page: params[:page], per_page: 10) 
    #@textbooks = Textbook.paginate(:page => params[:page], :per_page => 10) 
    end 

    # GET /textbooks/1 
    # GET /textbooks/1.json 
    def show 
    end 

我的config /路线:

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 

UPDATE 2 - !# - !# - !# - !# - !# - !# - !# - !# - ! # - # - # - # - # - # - # - # - !

下面是2016年4月17日下午11点00分 @zeiv我做了你告诉我的。 但我仍出现错误时,我的意见/教科书/ show.html.erb点击 '联系人' 按钮

#views/textbooks/show.html.erb 
<p> 
    <%= link_to 'Contact', new_contact_textbook_path %> 
</p> 

我的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): 
<div> 
    Texbook id is: <%= @textbook.id %> 
</div> 

我运行的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: <h1> contact seller! - working? </h1> 
10:56:13 PM web.1 |  2: 
10:56:13 PM web.1 |  3: <div> 
10:56:13 PM web.1 |  4:  Texbook id is: <%= @textbook.id %> 
10:56:13 PM web.1 |  5: </div> 

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

# 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文件与您的其他教科书的看法。

<h1> Contact to the Seller </h1> 

<div> 
    <%= form_tag send_contact_textbook_path(@textbook.id) do %> 
     <h3>Send email for: <%[email protected]%> </h3> 

     <%= label_tag :message, "Type your message:" %><br> 
     <%= text_area_tag :message %><br> 

     <%= submit_tag 'Send message', class: 'button' %> 
    <%end%> 
</div> 

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

你的邮件会再看看这样的事情:

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

<!DOCTYPE html> 
<html> 
    <head> 
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' /> 
    </head> 
    <body> 
    <h1><%= @current_user.name %> is wondering about <%= @textbook.title %>:</h1> 
    <p><%= @message %></p> 
    </body> 
</html> 

而且应该这样做!虽然你可能需要调整一些东西来满足你的需求。另请参阅以下链接获取更多的例子:

Contact Form Mailer in Rails 4

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

+0

我刚刚更新了我的问题。 –

+0

关于你的更新,我看到在第8行你有'textbook.title'而不是'@ textbook.title'。尝试添加'@',看看会发生什么。感谢更新! – Xavier

+0

我修正了 我得到错误另一个错误。我会在我的问题上发布更新部分 –