jjzjj

actioncontroller

全部标签

ruby-on-rails - 为什么 Controller 操作应该调用一个模型方法而不是初始查找或新方法?

我的模型中的函数几乎包含所有“共享”语句。问题是,当我需要在我的Controller中使用多个功能时,出现以下错误:ControlleractionshouldcallonemodelmethodotherthananinitialfindornewIDE会更深入地解释:Thisinspectionwarnsifacontrolleractioncontainsmorethanonemodelmethodcall,aftertheinitial.findor.new.It’srecommendedthatyouimplementallbusinesslogicinsidethemode

ruby-on-rails - Rails 4.2 Action Controller :BadRequest custom error message

如果验证失败或参数丢失,我想从我的Controller返回400-错误请求。所以在我的Controller中如果有ifparams["patch"].nil?thenraiseActionController::BadRequest.new("TheJsonbodyneedstobewrappedinsidea\"Patch\"key")end我在我的应用程序Controller中发现了这个错误:rescue_fromActionController::BadRequest,with::bad_requestdefbad_request(exception)renderstatus:4

ruby-on-rails - Rails 5 Rspec 接收 ActionController::Params

我刚刚升级到Rails5。在我的规范中,我有以下内容期望(模型).toreceive(:update).with(foo:'bar')但是,由于params不再扩展Hash而现在是ActionController::Parameters规范失败了,因为with()期待一个散列,但它实际上是ActionController::Parameters是否有更好的方法在Rspec中做同样的事情,例如不同的方法with_hash?我可以使用解决这个问题expect(model).toreceive(:update).with(hash_including(foo:'bar'))但这只是检查参数是

ruby-on-rails - Rails 4 - 设计:在注册时获取 ActionController::UnknownFormat

我有一个Rails4.2.3应用程序,我在其中使用Devise进行用户身份验证。我在Bootstrap模式中展示我的注册表单。我已经实现了类似于:https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app.注册时我不断收到此错误:Completed406NotAcceptablein512033ms(ActiveRecord:5.8ms)ActionController::UnknownFormat(ActionController::Un

ruby-on-rails - RunTimeError:Rspec 2.10.1 中的 ActionController::RackDelegation for rails 3.1.4 应用程序 Controller

在我们的rails3.1.4应用程序中,rspec用于测试应用程序Controller中的公共(public)方法require_signin。这是require_signin方法:defrequire_signinif!signed_in?flash.now.alert="Loginfirst!"redirect_tosignin_pathendend这是rspec代码:it"shouldinvokerequire_signinforthosewithoutlogin"docontroller.send(:require_signin)controller{shouldredirec

ruby-on-rails - 当我尝试创建用户时,rails 说 ActionController::InvalidAuthenticityToken

已更新:问题已解决我只需要将protect_from_forgery放在UsersController中。感谢大家。rails4.0.2当我尝试在users表中创建新记录时,我在浏览器中收到此消息:ActionController::InvalidAuthenticityTokeninUsersController#createActionController::InvalidAuthenticityToken但它发生在Chrome(32.0.1700.107)和Opera(12.16)浏览器中。在Firefox(27.0.1)和IE10.0.13中一切正常。也许这并不重要,但我不得不

ruby-on-rails - 从 Rails 3 中的默认 ActionController LogSubscriber 修改日志格式和内容

上下文:在一个Rails3项目中,我想自定义(大量)来自ActionController的“Processing”和“Completedin”日志行的格式和内容。这是为了让它们与旧版Rails2.3应用程序的(也是自定义的)格式相匹配,从而允许重复使用各种分析工具。使它们成为固定字段(通过在必要时使用占位符)还可以更容易地使用(比如)awk对它们进行临时查询,或者将它们加载到数据库或splunk中而无需智能解析。我通过forkrails和patching快速而严厉地实现了这个目标有问题的LogSubscriber,但我现在希望以正确的方式做到这一点。这是我认为我想做的事情:创建一个Lo

ruby-on-rails - 如何在 Rails 5 API 中呈现文件?

我有一个用React和RubyonRails后端(API模式)编写的单页应用程序。Rails也提供静态文件。我将Rails路由器指向public/index.html,因此我的SPA可以使用react-router管理他自己的路由。这是建立直接链接和刷新工作的常见做法。routes.rbmatch'*all',to:'application#index',via:[:get]application_controller.rbclassApplicationController问题是这在API模式下不起作用。这只是一个空洞的回应。如果我将父类更改为ActionController::Ba

ruby-on-rails - 如何允许使用 GRAPE API 下载二进制文件

我想允许使用ruby​​的GrapeAPI下载二进制文件(.p12文件)。这就是我正在尝试的。get'/download_file'dopkcs12=generate_pkcsfilecontent_type('application/octet-stream')body(pkcs12.der)end使用ActionController的等效代码是beginpkcs12=generate_pkcsfilesend_data(pkcs12.der,:filename=>'filename.p12')end问题是使用API下载的文件似乎是一个文本文件,每个字符都嵌入了'\ufffd'前缀,

ruby-on-rails - Rails ActionController 未知格式

我正在尝试渲染一个xlsx文件。但我不断收到406/UnknowFormat。我已经完成了正确的设置,也许我遗漏了什么?Rails4.2应用gem'axlsx'gem"axlsx_rails"gem'zip-zip'配置/初始化程序/mimeMime::Type.register"应用程序/xlsx",:xlsxControllerrespond_todo|format|format.xlsx{renderxlsx:"create",template:"api/reports/create"}endviews/api/reports/create.xlsx.axlsxwb=xlsx_p