小编a3u*_*uge的帖子

Rails 3 link_to路由(编辑)嵌套资源

对不起,如果有人在其他地方询问,但我无法弄清楚这一点.我有一个论坛,其中包含部分,主题和回复.我正在尝试编辑和删除show topic视图中的回复.这是结构:

resources :sections do
  resources :topics do
    resources :replies
  end
end
Run Code Online (Sandbox Code Playgroud)

所以我做了一个rake路线,看看我在哪里链接我的编辑回复.我看到它的edit_section_topic_reply和我的link_to中我添加了_path.现在这是我无法弄清楚的.我通过什么参数?不应该是:

<%= link_to 'Edit', edit_section_topic_reply_path(@reply, @topic, @section) %>
Run Code Online (Sandbox Code Playgroud)

我得到一个ActionController::RoutingErrorTopics#show我这样做的时候.

No route matches {:topic_id=>#<Topic id: 2, section_id: 2, user_id: nil, subject: "subject", body: "body", created_at: "2011-03-04 08:37:37", updated_at: "2011-03-04 21:37:16">, :controller=>"replies", :action=>"edit", :section_id=>nil, :id=>#<Section id: 2, name: "Section", description: "Section Description", created_at: "2011-03-04 07:50:56", updated_at: "2011-03-04 07:50:56">}
Run Code Online (Sandbox Code Playgroud)

好像它不是传递ID,而是之前的巢,我的新主题运行正常

new_section_topic_reply_path(@topic, @section)
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-routes ruby-on-rails-3

8
推荐指数
1
解决办法
8523
查看次数

Ruby on Rails 3简单的分组和计数

假设我有一个'Car'对象,其中包含'color'列.在我的视图中,我想要一个颜色列表及其出现的次数.

---Car----Color---
Explorer Black
Charger  Yellow
Prius    Black
Jetta    Black
Ferrari  Red 
Pinto    Yellow
Run Code Online (Sandbox Code Playgroud)

在我看来,我想:

--Color--Count--
Black      3
Yellow     2
Red        1
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我尝试制作如下列表:

@colorcount = Car.all.count(:group => :color)
Run Code Online (Sandbox Code Playgroud)

然后在我看来我有类似的东西

<%= @colorcount.each do |car, count| %>
   <%= car.color %>, <%= count %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

但我收到的错误如下:

undefined method `each' for 0:Fixnum
Run Code Online (Sandbox Code Playgroud)

还有更多吗?谢谢你的帮助.

ruby grouping ruby-on-rails count ruby-on-rails-3

2
推荐指数
1
解决办法
3539
查看次数