Tag: 嵌套资源

Rails – link_to,路由和嵌套资源

由于我对嵌套资源的理解,在边缘Rails上,不应该 link_to ‘User posts’, @user.posts 指向 /users/:id/posts ? routes.rb文件包含 map.resources :users, :has_many => :posts 如果这不是默认行为,那么可以通过其他方式完成吗?

导入csv文件时,如何在2个模型中进行质量分配?

我可以使用单个模型中的属性导入CSV文件并创建一个新对象(在本例中列出)。 我把它放在了Listing模型中 accepts_nested_attributes_for :address 其中address是关联模型(地址有很多列表,列表属于地址)。 我以为我能够在导入CSV文件时从地址模型中批量分配属性,但是我收到错误: Can’t mass-assign protected attributes: unit_number 其中unit_number在地址模型中的一个属性中(它在attr中可访问)。

显示视图中的“上一篇文章”和“下一篇文章”链接(嵌套资源)

在我的应用程序中,我想在文章显示视图的底部添加“上一篇文章”和“下一篇文章”链接。 这是我到目前为止,但我收到此错误: undefined method `article_path’ for #<#:0x007fd7cb8e5968> 我知道路径必须看起来像这样(但我很难实现它) myapp/users/1/article/1 新的铁路请帮助… ROUTES resources users do resources articles end 楷模 class User < ActiveRecord::Base attr_accessible :name, :photo has_many :articles end class Article ?”, id).order(“id ASC”).first end def prev user.articles.where(“id < ?", id).order("id DESC").first end end VIEWS 文章显示页面appname / users / 1 / articles / 1 CONTROLLER class […]

Rails 3.1 has_one嵌套资源:路由不生成“所有”路径

我有一个has_one关系: # supplier.rb has_one :presentation … # presentation.rb belongs_to :supplier … 以及它们的以下嵌套路由: # routes.rb resources :suppliers do resource :presentation end 运行rake routes给出: supplier_presentation POST … {:action=>”create”, :controller=>”presentations”} new_supplier_presentation GET … {:action=>”new”, :controller=>”presentations”} edit_supplier_presentation GET … {:action=>”edit”, :controller=>”presentations”} GET … {:action=>”show”, :controller=>”presentations”} PUT … {:action=>”update”, :controller=>”presentations”} DELETE … {:action=>”destroy”, :controller=>”presentations”} 为什么show_helper没有show动作? 我可以覆盖问题,例如: resources :suppliers do resource […]

Rails 3 – 在控制器中处理嵌套资源查询的最佳方法?

如果我对Rails 3有一点了解,那就是如果我很难做某事,我可能做错了。 所以我正在寻求帮助。 我有一些与多对多关系相关的模型。 我能够在没有问题的情况下在模型中创建关联。 我的问题在于如何构建控制器以使用这些关系。 如果你没看到我要去的地方,我会试着举个例子。 例如… class Account < ActiveRecord::Base has_many :locations end class Contact < ActiveRecord::Base has_many :locations end class Location < ActiveRecord::Base has_and_belongs_to_many :accounts has_and_belongs_to_many :contacts end 假设我有上述型号。 这将是我的资源…… resources :accounts do resources :locations end resources :contacts do resources :locations end resources :locations do resources :accounts resources :contacts end 所以只是为了缩短这一点,让我们说我想要一个帐户所有位置的列表。 上述路线可能是账号/ […]