我有一个API模式Rails 5应用程序,不会让我运行rake routes或rails s.我得到的错误是:
$ rake routes
rake aborted!
LoadError: Could not load the 'listen' gem. Add `gem 'listen'` to the development group of your Gemfile
.../config/environment.rb:5:in `<top (required)>'
LoadError: cannot load such file -- listen
.../config/environment.rb:5:in `<top (required)>'
Tasks: TOP => routes => environment
(See full trace by running task with --trace)
Run Code Online (Sandbox Code Playgroud)
我已经验证listen了我的Gemfile中的开发组:
group :development do
gem 'listen', '~> 3.1.5'
# Spring speeds up development by keeping your application running in the background. …Run Code Online (Sandbox Code Playgroud) 我有一个Rails 5 API应用程序(ApplicationController < ActionController::API).需要为此API的一个端点添加一个简单的GUI表单.
最初我ActionView::Template::Error undefined method protect_against_forgery?在尝试渲染表单时得到了.我添加include ActionController::RequestForgeryProtection并protect_from_forgery with:exception到终点.哪个问题按预期解决了.
但是,当我尝试提交此表格时,我得到:422 Unprocessable Entity ActionController::InvalidAuthenticityToken.我添加<%= csrf_meta_tags %>并验证了它meta: csrf-param并且meta: csrf-token存在于我的标题中,并且authenticity_token存在于我的表单中.(令牌本身彼此不同.)
我试过了protect_from_forgery prepend: true, with:exception,没有效果.我可以通过评论来"修复"这个问题:protect_from_forgery with:exception.但我的理解是,这正在关闭我的表格上的CSRF保护.(我想要CSRF保护.)
我错过了什么?
更新:
为了明确这一点,99%的应用程序是纯JSON RESTful API.需要添加一个HTML视图和表单到这个应用程序.因此,对于一个控制器,我想启用完整的CSRF保护.该应用程序的其余部分不需要CSRF,可以保持不变.
更新2:
我只是将这个应用程序的HTML表单和Header的页面源与我编写的另一个传统的Rails 5应用程序进行了比较.的authenticity_token页眉和authenticity_token形式是相同的.在我遇到问题的API应用程序中,它们是不同的.也许那是什么?
更新3:
好的,我不认为不匹配是问题.但是,在工作和非工作应用程序之间的进一步比较中,我注意到网络> Cookie中没有任何内容.我_my_app-session在工作应用程序的cookie中看到了很多东西.
我在Heroku上有Rails应用程序.它有一个自定义域,我试图通过Mailgun设置电子邮件发送.我已经通过Heroku安装了Mailgun作为插件,我已经完成了Mailgun给出的"验证"我的自定义域的步骤.如果我运行Mailgun的"立即检查DNS记录",一切都会变回绿色,状态为"活动".我甚至可以通过curl他们提供的电话从我的自定义域发送消息.但是,当我尝试使用我的Rails应用程序发送电子邮件时,我ActionMailer得到:Net::SMTPFatalError (554 Sandbox subdomains are for test purposes only. Please add your own domain or add the address to authorized recipients in domain settings.
为什么它认为我使用的是"Sandbox子域"?这就是我所拥有的environments/production.rb:
# Mailgun
ActionMailer::Base.smtp_settings = {
port: ENV['MAILGUN_SMTP_PORT'],
address: ENV['MAILGUN_SMTP_SERVER'],
user_name: ENV['MAILGUN_SMTP_LOGIN'],
password: ENV['MAILGUN_SMTP_PASSWORD'],
domain: 'my-custom-domain.com',
authentication: :plain,
}
ActionMailer::Base.delivery_method = :smtp
# Devise recoverable
config.action_mailer.default_url_options = { host: 'my-custom-domain.com' }
Run Code Online (Sandbox Code Playgroud)
对于开发我正在使用Gmail,所以我知道它正在阅读正确的配置文件.并且所有env变量都设置正确.from也正确设置,我看到它是我的日志(do-not-reply@my-custom-domain.com)我错过了什么?即使状态是活跃的,还有什么东西可以传播吗?
谢谢!