1

I have...

/config/routes.rb:

Testivate::Application.routes.draw do
  resources :areas do
    resources :heuristics    
  end
end

/app/models/heuristic.rb:

class Heuristic < ActiveRecord::Base
  attr_accessible :area_id
  belongs_to :area
end

/app/models/area.rb:

class Area < ActiveRecord::Base
  has_many :heuristics
end

/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:

%h1 New heuristic
= render 'form'
= link_to 'Back', area_heuristics_path(@area)

/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.

Why then am I getting the following error at 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|
4

2 に答える 2

5

ネストされたリソースのURLの生成について詳しくは、 http: //edgeguides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objectsをご覧ください。

simple_form_for [@area, @heuristic] do |f|

オプションよりはましだと思いurlます。

于 2012-11-20T13:32:16.977 に答える
3

ネストされたルートが使用されているため、パスは単一のリソースとしては好まれません。実行rake routesして、ヒューリスティックリソースのパスを確認できます。

変更してみてください:

simple_form_for @heuristic do |f|

に:

simple_form_for @heuristic, url: area_heuristics_path do |f|
于 2012-11-20T11:28:44.533 に答える