Tag: rails routing

Rails在CORS预检选项请求中响应404

我使用Rails 4创build了一组服务,我正在使用JavaScript浏览器应用程序。 交叉原点GETS工作正常,但我的POST失败了预检选项检查404错误。 至less,我想这就是发生了什么事情。 这些错误出现在控制台中。 这是Mac上的Chrome 31.0.1650.63。 OPTIONS http://localhost:3000/confessor_requests 404 (Not Found) jquery-1.10.2.js:8706 OPTIONS http://localhost:3000/confessor_requests No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. jquery-1.10.2.js:8706 XMLHttpRequest cannot load http://localhost:3000/confessor_requests. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. main.html:1 我已经search了高和低的指示启用CORS,我很难过。 通常的build议似乎是把这样的东西放在应用程序控制器,我做了。 […]

可以轨道路由助手(即mymodel_path(模型))在模型中使用?

假设我有一个叫做Thing的Rails模型。 Thing有一个url属性,可以select将其设置为Internet上的某个URL。 在查看代码,我需要逻辑,执行以下操作: <% if thing.url.blank? %> <%= link_to('Text', thing_path(thing)) %> <% else %> <%= link_to('Text', thing.url) %> <% end %> 这个视图中的条件逻辑是丑陋的。 当然,我可以build立一个辅助函数,将视图改为: <%= thing_link('Text', thing) %> 这解决了冗长的问题,但我真的更喜欢在模型本身的function。 在这种情况下,视图代码将是: <%= link_to('Text', thing.link) %> 显然,这将需要模型上的链接方法。 以下是它需要包含的内容: def link (self.url.blank?) ? thing_path(self) : self.url end 至于这个问题,thing_path()是Model代码中的一个未定义的方法。 我假设可以将一些辅助方法“拉进”模型,但是如何? 是否有一个真正的原因,路由只能在应用程序的控制器和视图层? 我可以想象很多模型代码可能需要处理URL(与外部系统集成等)的情况。