这个快把我逼疯了。我的项目中的两个模型之间存在嵌套关系,我决定不希望它变浅,因为子对象(年)在父对象(节日)的上下文之外没有任何意义。
因此,我在可以找到对它的引用的任何地方都对这种关系进行了去浅化,但我发现自己无法访问该页面来创建新的子对象。
这是我理解的网址:/festivals/1/years/new
来自 routes.rb:
resources :festivals do
resources :years
end
来自years_controller.rb:
# GET festivals/1/years/new
# GET festivals/1/years/new.json
def new
@festival = Festival.find(params[:festival_id])
@year = @festival.years.build
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @year }
end
end
用户按下按钮进入新页面(在父对象的显示页面上):
<%= link_to 'Add Year', new_festival_year_path(@festival), :class => 'btn' %>
这会将用户带到正确的 URL,但我得到:
No route matches {:action=>"show", :controller=>"years", :festival_id=>#<Festival id: 7, name: "Improganza", founded: nil, logo: "", mission: "This is that one that people spend a lot of money t...", city: "Honolulu", state_code: "HI", country_code: "US", created_at: "2013-07-26 14:49:19", updated_at: "2013-07-26 14:49:19">}
我创建了一个新的 Rails 项目并使用 Akria Matsuda 的 nested_scaffold gem 设置了脚手架,只是为了将该输出与我的代码进行比较......生成的文件看起来就像我在这里展示的那样。我不知道我可能会错过什么。
只是为了更好的衡量,我的输出rake routes
:
festival_years GET /festivals/:festival_id/years(.:format) years#index
POST /festivals/:festival_id/years(.:format) years#create
new_festival_year GET /festivals/:festival_id/years/new(.:format) years#new
edit_festival_year GET /festivals/:festival_id/years/:id/edit(.:format) years#edit
festival_year GET /festivals/:festival_id/years/:id(.:format) years#show
PUT /festivals/:festival_id/years/:id(.:format) years#update
DELETE /festivals/:festival_id/years/:id(.:format) years#destroy
festivals GET /festivals(.:format) festivals#index
POST /festivals(.:format) festivals#create
new_festival GET /festivals/new(.:format) festivals#new
edit_festival GET /festivals/:id/edit(.:format) festivals#edit
festival GET /festivals/:id(.:format) festivals#show
PUT /festivals/:id(.:format) festivals#update
DELETE /festivals/:id(.:format) festivals#destroy
GET /festivals(.:format) festivals#index
POST /festivals(.:format) festivals#create
GET /festivals/new(.:format) festivals#new
GET /festivals/:id/edit(.:format) festivals#edit
GET /festivals/:id(.:format) festivals#show
PUT /festivals/:id(.:format) festivals#update
DELETE /festivals/:id(.:format) festivals#destroy