[英]Why is simple_form generating a non-existent route?
I have... 我有...
/config/routes.rb: /config/routes.rb:
Testivate::Application.routes.draw do
resources :areas do
resources :heuristics
end
end
/app/models/heuristic.rb: /app/models/heuristic.rb:
class Heuristic < ActiveRecord::Base
attr_accessible :area_id
belongs_to :area
end
/app/models/area.rb: /app/models/area.rb:
class Area < ActiveRecord::Base
has_many :heuristics
end
/app/controllers/heuristics_controller.rb: /app/controllers/heuristics_controller.rb:
class HeuristicsController < ApplicationController
def new
@area = Area.find(params[:area_id])
@heuristic = @area.heuristics.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @heuristic }
end
end
end
/app/views/heuristics/new.html.haml: /app/views/heuristics/new.html.haml:
%h1 New heuristic
= render 'form'
= link_to 'Back', area_heuristics_path(@area)
/app/views/heuristics/_form.html.haml: /app/views/heuristics/_form.html.haml:
= simple_form_for @heuristic do |f|
= f.input :foo
= f.button :submit
At no point do I explicitly call heuristics_path
, as this of course does not exist. 我绝对不会明确调用
heuristics_path
,因为这当然不存在。
Why then am I getting the following error at http://localhost:3000/areas/1/heuristics/new
? 为什么然后在
http://localhost:3000/areas/1/heuristics/new
出现以下错误?
NoMethodError in Heuristics#new
Showing /Users/steven/Dropbox/testivate/app/views/heuristics/_form.html.haml where line #1 raised:
undefined method `heuristics_path' for #<#<Class:0x007fea3b2ac1a0>:0x007fea3d027608>
Extracted source (around line #1):
1: = simple_form_for @heuristic do |f|
You can read more about generating urls for nested resources here - http://edgeguides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects 您可以在此处详细了解有关为嵌套资源生成网址的信息-http: //edgeguides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects
simple_form_for [@area, @heuristic] do |f|
I think it's better than url
option. 我认为它比
url
选项要好。
You are used nested routes, so the path will not like as single resources. 您使用的是嵌套路由,因此路径不会像单个资源一样。 You can run
rake routes
to check your path of heuristic resources. 您可以运行
rake routes
以检查启发式资源的路径。
Try change: 尝试更改:
simple_form_for @heuristic do |f|
To: 至:
simple_form_for @heuristic, url: area_heuristics_path do |f|
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.