对于仅让服务器发送电子邮件而不改变整个 Web 应用程序状态的请求,正确的 http 动词是什么?
我正在开发一个 Rails 项目,我需要以某种方式定义路线:
Rails.application.routes.draw do
get 'self_emailer/email_me'
...
end
Run Code Online (Sandbox Code Playgroud) 我在轨道4.2.1和设计3.4.1.我基本上想要同时做两件事:
和
我设法让他们分开工作.但是第一个问题的解决方案1似乎是,我担心,与第二个问题的唯一官方解决方案不兼容,因为对于后者,我需要覆盖注册控制器.
因此,我尝试在注册控制器而不是应用程序中执行解决方案1作业:
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :configure_account_update_params, only: [:update]
protected
def configure_account_update_params
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :password, :password_confirmation, :current_password) }
end
def update_resource(resource, params)
resource.update_without_password(params)
end
end
Run Code Online (Sandbox Code Playgroud)
这种方式只有在密码被完全过滤掉时才添加名称等属性.对于这样一个简单的目标,我不确定我是否应该像解决方案2和3那样开始大量定制..我错过了什么?
我需要向jsonapi-resources控制器添加自定义操作。该操作只是创建一个新版本的先前存在的CollectorContent模型实例。我的实现(仅从此处复制):
routes.rb
jsonapi_resources :collector_contents do
member do
post :create_version
end
end
Run Code Online (Sandbox Code Playgroud)
collector_contents_controller.rb
class CollectorContentsController < ApplicationController
def create_version
cc = CollectorContent.find(params[:id])
cc_new_version = cc.create_version!
render json: resource_serializer.serialize_to_hash(CollectorContentResource.new(cc_new_version, nil))
end
end
Run Code Online (Sandbox Code Playgroud)
即使可行,似乎也不建议在控制器中添加新操作,但是我不明白如何使用操作处理器来实现用例。你能帮忙吗?
我可以做这个:
curl --user 'api:MYAPIKEY' -F from=foo@bar.it -F to=MYEMAIL@gmail.com -F subject='foo' -F text='bar' https://api.mailgun.net/v3/sandbox93d8299f673a4c3295b7592956cb3d9.mailgun.org/messages
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用jquery .ajax()方法执行相同的请求时,我得到一个登录弹出窗口:
$.ajax({
headers: {"Authorization": "Basic api:key-b50a9065a7b9bdf464f3d7a418ca96bb"},
// url: "././mail/contact_me.php",
url: "https://api.mailgun.net/v3/sandbox93d8299f673a4c32952b7592956cb3d9.mailgun.org/messages",
type: "POST",
dataType: 'json',
data: {
name: name,
phone: phone,
email: email,
message: message
},
// cache: false,
success: function() {
alert('ok');
},
error: function() {
alert('problems');
}
}
Run Code Online (Sandbox Code Playgroud)
服务器回复401.任何想法为什么?
谢谢