我正在尝试在邮件程序模板中嵌入一个表单。代码看起来有点像这样。我已经搭建了一个示例应用程序。
rails g scaffold列表名称描述
我创建了一个名为UserNotifier的邮件程序类,它看起来像这样
class UserNotifier < ApplicationMailer
default from: "from@example.com"
def status_email(tenant, project)
@tenant = tenant
@project = project
mail(to: 'abc@example.com', subject: 'Sample Status Email')
end
end
在方法status_email的相应视图中,即我的status_email.html.erb文件看起来像这样
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Hi </h1>
<p>
Sample status mail sent using smtp.
</p>
<%= form_for [@tenant, @project], url: {controller: "listings", action: "create"}, :html => { :class => "form-horizontal project" } do |f| %>
<% if @project.errors.any? %>
<div id="error_expl" class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h3>
</div>
<div class="panel-body">
<ul>
<% @project.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
<div class="control-group">
<%= f.label :title, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :title, :class => 'form-control' %>
</div>
</div>
<div class="control-group">
<%= f.label :details, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :details, :class => 'form-control' %>
</div>
</div>
<div class="control-group">
<%= f.label :expected_completion_date, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :expected_completion_date, :class => 'form-control datepicker' %>
</div>
</div>
<br />
<%= f.hidden_field :tenant_id, value: @tenant.id, :class => 'form-control' %>
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
root_path, :class => 'btn btn-default' %>
<% end %>
</body>
</html>
最后,我在我的列表控制器中调用邮件程序方法,创建操作。以下是代码段:
def create
@listing = Listing.new(listing_params)
@project = @listing.project
@tenant = @project.tenant
@user = current_user
respond_to do |format|
if @listing.save
UserNotifier.status_email(@tenant, @project).deliver
format.html { redirect_to @listing, notice: 'Listing was successfully created.' }
format.json { render :show, status: :created, location: @listing }
else
format.html { render :new }
format.json { render json: @listing.errors, status: :unprocessable_entity }
end
end
端
但最后当我提交时,我收到此错误(NoMethodError in Listings#create
)。
undefined method protect_against_forgery? for #<#<Class..."
有人可以帮助我如何在我的电子邮件模板中呈现此表单,以便用户可以直接从电子邮件提交此表单,而无需登录该应用程序。
答案 0 :(得分:0)
这是protect_against_forgery错误,您可以通过在控制器中添加来解决它
skip_before_filter :verify_authenticity_token, :only => [YOUR ACTION]