2016-04-17 46 views
0

我正在使用設計和腳手架教材。 我喜歡實施我的策略。 買家點擊@ 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> 

回答

0

基本上你需要做的是寫你的郵件程序和控制器,所有你想要的信息傳遞給郵寄的方式。因此,如果您希望將您的教科書模型的實例傳遞給郵件程序,則需要從發送電子郵件的控制器執行此操作。您可能想要在您的教科書路線中嵌套您的聯繫人控制器路線以幫助您。或者,您可以在教科書控制器中聯繫動作,而不是讓聯繫人使用整個控制器。

# 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

我修正了 我得到錯誤另一個錯誤。我會在我的問題上發佈更新部分 –

相關問題