请原谅新手问题。我正在构建一个包含联系我们页面的表单,其中用户应该能够填写表单(user_full_name,user_email,subject,message),该表单在提交时应作为电子邮件发送到我的gmail地址。当我尝试使用表单访问视图时,我当前收到以下错误。
#<#:0x007f8fefecd020>
的未定义方法`feedbacks_path'下面是我的代码,有谁知道我在这里做错了什么?如果可能,我宁愿改变路线而不是在表格中添加网址。此外,想要在路由中使用“匹配”以使url路径为“localhost / contact”而不是“localhost / feedback”。有没有人有任何想法?
的routes.rb
Sample::Application.routes.draw do
resources :feedback, only: [:new, :create]
root to: "home#index"
match '/about', to: 'static_pages#about', via: 'get'
match '/help', to: 'static_pages#help', via: 'get'
match '/privacy_policy', to: 'static_pages#privacy_policy', via: 'get'
match '/terms_and_conditions', to: 'static_pages#terms_and_conditions', via: 'get'
end
Feedback_Controller.rb
class FeedbackController < ApplicationController
def new
@feedback = Feedback.new
end
def create
@feedback = Feedback.new(params[:feedback])
if @feedback.valid?
FeedbackMailer.new_feedback(@feedback).deliver
flash[:success] = "We have receieved your message!"
redirect_to users_path
else
flash.now.alert = "Please fill in all fields."
render :new
end
end
end
Feedback.rb
class Feedback < ActiveRecord::Base
def self.columns() @columns ||= []; end
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
end
column :user_full_name, :string
column :user_email, :string
column :subject, :text
column :body, :text
belongs_to :user
end
查看/ feedback.new.html.erb
<div class="feedback">
<div class="container">
<%= form_for @feedback do |f| %>
<%= f.hidden_field :user_full_name %>
<%= f.hidden_field :user_email %>
<%= f.label :subject %>
<%= f.text_field :subject %>
<%= f.label :feedback %>
<%= f.text_area :body %>
<%= f.submit "Send", class: "btn btn-inverse" %>
<% end %>
</div>
</div>
feedback_mailer
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
Name: <%= @feedback.user_full_name %>
Email: <%= @feedback.user_email %>
Subject: <%= @feedback.subject %>
Comments/Question: <%= @feedback.body %>
</body>
</html>
feedback_mailer.rb
class FeedbackMailer < ActionMailer::Base
default to: "railstraining09@gmail.com"
def new_feedback(feedback)
@feedback = feedback
mail(from: @user.email, subject: 'feedback.subject')
end
end
答案 0 :(得分:0)
如果您正在开发中,可以添加app / config / environments / development.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
# Gmail SMTP server setup
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:enable_starttls_auto => true,
:port => 587,
:authentication => :plain,
:user_name => ENV['GMAIL_USERNAME'],
:password => ENV['GMAIL_PASSWORD']
}
并尝试一下。 您可以在config / environments /中创建文件mail.rb,或者如果您想为每个环境使用特定的电子邮件帐户,那么您需要在每个“环境”文件中编写(production.rb; test.rb,development。 rb)你的ActionMailer :: Base.smtp_settings {options ....}
P.S。我建议您使用mandrill / sendgrid等代替video