首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >嵌套rails类的问题&路由

嵌套rails类的问题&路由
EN

Stack Overflow用户
提问于 2011-06-12 21:55:42
回答 1查看 423关注 0票数 0

我正试图在一段时间后回到rails中,并且很难以嵌套的方式连接两个简单的脚手架构建的资源。父控制器工作,但孩子通常会爆炸。我一直在寻找这个问题的答案,但没有成功。

对于属于产品父级的特定注释子节点,请路由“/products/1/注释/1”

错误消息

如果没有ID应用程序/控制器/注释_控制器.in:25:in‘`show’,

无法找到注释

参数:

{"product_id"=>"1",“id”“=>”1“}

这里是comments_controller "show"的相关代码

代码语言:javascript
运行
复制
def show
@product = Product.find(params[:product_id])
@comment = @product.comments.find(params[:comment_id])

(如果我将:comment_id更改为just :id,新的错误是:)

找不到ID=1的评论(comments.product_id = 1)

{"product_id"=>"1",“id”“=>”1“}

注释索引的:/products/1/comments

错误信息:

用于Fixnum的

未定义方法`model_name‘:类参数:{"product_id"=>"1"}

**索引视图中的相关代码**

代码语言:javascript
运行
复制
18:     <td><%= link_to 'Show', [@product, comment.id] %></td>
19:     <td><%= link_to 'Edit', edit_product_comment_path(@product, comment) %></td>
20:     <td><%= link_to 'Destroy', [@product, comment], :confirm => 'Are you sure?',           :method => :delete %></td>

我花了几天时间搞砸这件事,但没有用。我一直在检查简单的东西,比如:id到:(名词)_id,以及在我的观点中在@product、comment和@product、comment.id之间切换链接。

对于如何使这项工作有效,任何建议都会受到极大的赞赏。这看起来应该很简单,我很喜欢“书”。这方面的问题是,我的rails文本( Rails方法和一本带有几章rails的小红宝石入门书)充其量都是基于rails 2的,而且web资源还没有完全更新。

更新: *Routes.rb* Party2::Application.routes.draw do

代码语言:javascript
运行
复制
resources :comments

resources :products do
  resources :comments
end

注释索引中的错误

用于Fixnum:Class的

未定义方法`model_name‘

注释索引中的相关代码(第18行出错)

代码语言:javascript
运行
复制
18:     <td><%= link_to 'Show', [@product, comment.id] %></td>
19:     <td><%= link_to 'Edit', edit_product_comment_path(@product, comment) %></td>
20:     <td><%= link_to 'Destroy', [@product, comment], :confirm => 'Are you sure?', :method => :delete %></td>

另一个更新: *Models*

代码语言:javascript
运行
复制
class Product < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :product
end

再次感谢,

卡梅伦

(对我来说,这不应该是很奇怪的,因为我一直在学习教程。*/)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-12 22:35:46

如果一个注释只能属于一个产品,那么您应该能够在comments_controller.rb中执行这样的操作:

代码语言:javascript
运行
复制
def show
  @comment = Comment.find(params[:id])
  # @product = @comment.product
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6324953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档