Rails 4.2+,求您了!
我一直在寻找关于如何在Rails中使URL变得漂亮的技巧,而且我很难找到我喜欢的解决方案。
我想要的:
假设的例子:给定的主题、课程等模型有很多字段(包括URL友好的段塞),我希望能够
# ../routes.rb
# Match urls of the form /edu/material-engineering. These are read-only
# public URLs, not resources.
get 'edu/:slug', to: 'education#topic', as: :learn_topic
我在我的rails应用程序中使用ActiveResource与另一个rails应用程序(两者都是2.3.5版本)对话。我想链接到特定资源对象的页面,但似乎没有任何好的方法可以做到这一点。我想出的最难看的方法是在我的routes.rb文件中添加一行来模拟我的资源,如下所示:
# environment.rb, or in the config/environments/*.rb files
PERSON_URL = "people.example.com"
# person.rb
class Person < ActiveResource::Base
self.sit
我正在切换到Rails活动存储,以便在本地(使用磁盘服务)处理产品目录的图像上传和存储,而且我在为映像获取一个可用的url以输入<img>标记时遇到了困难。我在前端使用了React,所以我不能(很容易)使用Rails助手来生成标记。
ActiveStorage将文件放在/public/images中。我可以硬编码到文件的相对链接(即http://localhost:3000/images/Ab/CD/AbCDEfGhIjkL),它工作得很好。
来自Product.rb的相关片段
class Product < ApplicationRecord
att
我希望路由约束能够使admin.example.com/widgets/和example.com/admin/widgets/实际上是相同的,URL助手admin_widgets_path将根据当前的子域指向正确的路径。然而,不管约束条件如何,路由助手似乎只指向其中一个或另一个。
我做错了什么吗?这是个虫子吗?有没有更好的方法来解决这个问题?
已经发布了一个带有问题示例的rails应用程序,但相关细节如下。
我的config/routes.rb
class Subdomains
# any domain that starts with 'admin.'
def sel
有没有一种方法可以覆盖rails路由助手方法,以便它们以不同的方式处理模型实例?我刚刚创建了一个User模型,它不继承自ActiveRecord::Base,而是使用我编写的方法从LDAP数据库中检索用户。然而,现在所有的路由助手方法都乱七八糟了。(例如,user_path(user)提供/users/#<User:0x3df82a0>而不是/users/002131)
我尝试使用以下代码(放在User模型中)覆盖该方法,但它似乎没有任何作用。有什么想法吗?
class << Rails.application.routes.url_helpers
def user
我正在rails中构造一个提要,我希望在内容部分中返回我的项目的整个显示路径。我当前的代码只打印没有根的路径。
entry.content link_path(link)返回类似于links/44的内容,但我想返回。使用应用程序托管位置的根目录。谢谢你的帮助。
atom_feed do |feed|
feed.title "Wrld"
feed.updated @links.first.created_at
@links.each do |link|
feed.entry link do |entry|
entry.title link.tit
我使用Rails 3.2.2,我想为嵌套的资源生成一个正确的url_for URL。也就是说,我有:
# config/routes.rb
resources :articles do
resources :user_associations
end
# app/models/article.rb
class Article < ActiveRecord::Base
...
end
# app/models/articles/user_association.rb
class Articles::UserAssociation < ActiveRecord::Base
我一直在手动编写路径,但在意识到Rails中这是多么没有意义之后,我放弃了,因为这个框架的很大一部分目的是自动化重复。
在使用Rails的resources关键字生成路由之后,我无法找到任何解释(.:format)参数代表什么的显式文档。我也不清楚_path和_url的助手是如何使用这些路线的:
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) p
我真的很困惑,因为当我使用controllername_index_url时,我的一些路由是路由到/controllername/index,而另一些则是路由到/controllername。
主要区别在于,当我通过命令行创建控制器时,当我生成索引方法时
rails g controller controllerName1 index
然后,该路线成为/controller_name_1/index。
当我在创建控制器之后手动创建索引时,它将变成/controllername2。
在我的配置/路由中,我包括我的控制器,例如:
Rails.application.routes.draw do
我正在将Merb应用程序移植到Rails 3。在Merb中,我们可以在路由周围放置一个Identify块来定义如何提供:id路由参数,例如,
# this is a Merb route that I want to port to Rails 3 routing; I get everything except
# how to replicate the behavior of Merb's Identify block which doesn't require one to
# futz with overriding to_param on user; a use