ste*_*ble 1 ruby-on-rails associations simple-form
我有...
/config/routes.rb:
Testivate::Application.routes.draw do
resources :areas do
resources :heuristics
end
end
Run Code Online (Sandbox Code Playgroud)
/app/models/heuristic.rb:
class Heuristic < ActiveRecord::Base
attr_accessible :area_id
belongs_to :area
end
Run Code Online (Sandbox Code Playgroud)
/app/models/area.rb:
class Area < ActiveRecord::Base
has_many :heuristics
end
Run Code Online (Sandbox Code Playgroud)
/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
Run Code Online (Sandbox Code Playgroud)
/app/views/heuristics/new.html.haml:
%h1 New heuristic
= render 'form'
= link_to 'Back', area_heuristics_path(@area)
Run Code Online (Sandbox Code Playgroud)
/app/views/heuristics/_form.html.haml:
= simple_form_for @heuristic do |f|
= f.input :foo
= f.button :submit
Run Code Online (Sandbox Code Playgroud)
我没有明确地打电话heuristics_path,因为这当然不存在.
为什么我会收到以下错误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|
Run Code Online (Sandbox Code Playgroud)
您可以在此处阅读有关为嵌套资源生成URL的更多信息 - http://edgeguides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects
simple_form_for [@area, @heuristic] do |f|
Run Code Online (Sandbox Code Playgroud)
我认为这比url选项更好.
| 归档时间: |
|
| 查看次数: |
2492 次 |
| 最近记录: |