Ruby on rails RubyonRails发送电子邮件时遇到问题
首先:这是我的第一个问题,所以请让我知道,如果我不在这里做的话,我以后如何能更好地提问。我对ROR也比较陌生 问题: 我正在尝试让我的web应用程序在我的开发环境中发送电子邮件。它不起作用了。我不完全清楚为什么。版本:Rails 4,Ruby 2 当前浏览器问题:Ruby on rails RubyonRails发送电子邮件时遇到问题,ruby-on-rails,actionmailer,Ruby On Rails,Actionmailer,首先:这是我的第一个问题,所以请让我知道,如果我不在这里做的话,我以后如何能更好地提问。我对ROR也比较陌生 问题: 我正在尝试让我的web应用程序在我的开发环境中发送电子邮件。它不起作用了。我不完全清楚为什么。版本:Rails 4,Ruby 2 当前浏览器问题: ActionController::ParameterMissing in ProfilesController#update param is missing or the value is empty: profile Extr
ActionController::ParameterMissing in ProfilesController#update
param is missing or the value is empty: profile
Extracted source (around line #83):81 82 83 84 85
# Never trust parameters from the scary internet, only allow the white list through.
def profile_params
params.require(:profile).permit(:user_id, :first_name, :last_name, :dob, :email, :mobile, :address, :suburb, :postcode, :city, :state, :country)
end
Application Trace | Framework Trace | Full Trace
app/controllers/profiles_controller.rb:83:in `profile_params'
app/controllers/profiles_controller.rb:45:in `block in update'
app/controllers/profiles_controller.rb:44:in `update'
Request
Parameters:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"Px14k13lkQa6d8V61K6dvwLN5xhqjieE8Yk5LjiW1z8=",
"destination"=>"test@gmail.com",
"commit"=>"Send Email",
"id"=>"1"}
路线:
resources :profiles do
put :email
end
配置文件控制器:
def email
destination = params[:to]
share = Share.profile(@profile, destination)
if destination =~ /@/ && share.deliver
redirect_to @profile, notice: 'email sent :)'
else
redirect_to @profile, notice: 'email failed :('
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def profile_params
params.require(:profile).permit(:user_id, :first_name, :last_name, :dob, :email, :mobile, :address, :suburb, :postcode, :city, :state, :country)
end
共享邮件发送人:
class Share < ActionMailer::Base
default_url_options[:host] = "localhost:3000"
default from: "from@example.com"
def profile(profile, destination)
@user = profile.user
@profile = profile
mail to: destination,
subject: "#{@user.email} sent you stuff")
end
end
查看/配置文件/显示:
<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Email
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria- labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Share my Info with someone</h4>
</div>
<div class="modal-body">
<%= form_tag profile_path(@profile), method: :put do %>
<%= label_tag :destination, "What email address would you like to send this to?" %>
<%= text_field_tag :destination %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<%= submit_tag "Send Email", class: "btn btn-primary" %>
<% end %>
</div>
</div>
</div>
</div>
非常感谢您的帮助!我不知道哪里出了问题!干杯您在提交表单时正在调用更新操作。请尝试更改此行,然后重试:
<%= form_tag profile_path(@profile), method: :put do %>
to
<%= form_tag email_path(@profile), method: :put do %>
控制器:
def email
destination = params[:to]
share = Share.profile(@profile, destination)
if destination =~ /@/ && share.deliver
redirect_to @profile, notice: 'email sent :)'
else
redirect_to @profile, notice: 'email failed :('
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def profile_params
params.require(:profile).permit(:user_id, :first_name, :last_name, :dob, :email, :mobile, :address, :suburb, :postcode, :city, :state, :country)
end
您的代码正在调用更新操作,而不是电子邮件操作。更改您的表单以提交电子邮件操作。感谢您回复@sevensacat和@anusha!我将行更改为email_path,但现在我得到:NoMethodError in profiles show undefined method`email_path',我不确定为什么它没有定义,因为它已经在控制器中定义了?我还尝试了“电子邮件”配置文件_path@profile但也有同样的问题,我在用Desive。我不确定这是否会造成干扰?@sss333您是否在路由中包含了它的root。rb类似于:match'/email'=>“profileemail”…仍然返回一个错误:match'/email'=>“profileemail”返回:RuntimeError您不应该在没有指定HTTP方法的情况下在路由器中使用match方法。如果您想向GET和POST公开您的操作,请通过:[:GET,:POST]选项添加。如果你想暴露你的行为以获得,在路由器中使用get:而不是:match controlleraction Do:get controlleractionget'/email'=>“profileemail”返回:ActionController::ParameterMissing in profiles controllerUpdate param丢失或值为空:第83行周围提取的配置文件源:def profile\u params参数。需要:配置文件。允许:用户\u id,:名字::姓氏::dob,:email,:mobile,:address,:country,:postcode,:city,:state,:country end profiles\u controller.rb:83:in profile\u params'app/controllers/profiles\u controller.rb:45:in block in update'app/controllers/profiles\u controller.rb:44:in'update'
get 'email_profile' => 'profiles#email_profile'
def email_profile
@profile = params[:profile]
destination = params[:to]
share = Share.profile(@profile, destination)
if destination =~ /@/ && share.deliver
redirect_to @profile, notice: 'email sent :)'
else
redirect_to @profile, notice: 'email failed :('
end
end