我正在使用設計和腳手架教材。 我喜歡實施我的策略。 買家點擊@ 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>
我剛剛更新了我的問題。 –
關於你的更新,我看到在第8行你有'textbook.title'而不是'@ textbook.title'。嘗試添加'@',看看會發生什麼。感謝更新! – Xavier
我修正了 我得到錯誤另一個錯誤。我會在我的問題上發佈更新部分 –