Ruby on Rails嵌套资源未定义方法

时间:2012-07-02 04:44:44

标签: ruby-on-rails ruby-on-rails-3

我在这里看到了类似的例子。但它对我不起作用。 我想为帖子创建/提交活动。在帖子下有几天。活动在特定日期之下。见下面的型号。

型号:

class Post < ActiveRecord::Base
  has_many :dayinposts
  has_many :activitys, :through => :dayinposts
end

class Dayinpost < ActiveRecord::Base
  belongs_to :post
  has_many :activitys
end

class Activity < ActiveRecord::Base
  belongs_to :dayinpost
end

路线:

resources :posts do
  resources :dayinposts  do
resources :activitys
  end 
end

rake routes

post_dayinpost_activitys GET /posts/:post_id/dayinposts/:dayinpost_id/activitys(.:format)          activitys#index
                         POST /posts/:post_id/dayinposts/:dayinpost_id/activitys(.:format)          activitys#create

show.html.erb

<% @post.dayinposts.each do |dayinpost| %>

<% dayinpost.activitys.each do |activity| %>
  <p>
  <b>Action:</b>
  <%= activity.action %>
  </p>
<% end %>

<%= form_for([@post, dayinpost, dayinpost.activitys.build]) do |f| %>
  <div class="field">
  <%= f.label :action %><br />
  <%= f.text_field :action %>
  </div>

<div class="actions">
<%= f.submit %>
</div>
<% end %>

<% end %>

错误

undefined method `post_dayinpost_activities_path' for #<#<Class:0x40cb6d8>:0x40c9890>

但是我在rake路线上看到了它...... 提前谢谢!

1 个答案:

答案 0 :(得分:1)

“活动”的正确复数是“活动”。

config/routes.rb你有“活动”,这是错误的。