0
我有三個嵌套資源模型。Rails嵌套路由
class User < ActiveRecord::Base
has_many :basiccases
end
class Basiccase < ActiveRecord::Base
belongs_to :user
has_one :form3_c
end
class Form3C < ActiveRecord::Base
belongs_to :basiccases
end
resources :basiccases do
resources :form3_cs
end
我想給basiccase發送到使用
<%= link_to "ADD Form3C", new_basiccase_form3_c_path(@basiccase)%>
新的形式,並在方法的form_for新視圖使用
<%= form_for([@form3c, @basiccase]) do |f| %>
獲取形式的新值。 我的控制器代碼
class Form3CsController < ApplicationController
#before_filter :authenticate
def new
@title = "New Form 3C"
@basiccase = Basiccase.find_by_id(params[:id])
@form3c = Form3C.new if signed_in?
end
def create
@form3c = @basiccase.build_form3_c(params[:form3c])
if @form3c.save
flash[:success] = "Form created!"
redirect_to current_user
else
flash[:warning] ="Failed to create a Form"
render 'users/show'
end
end
end
我的主要目的是讓在form3_c控制器的創建方法basiccase_id並指定外鍵的屬性。我的方法是否正確?
我得到一個錯誤
undefined method `model_name' for NilClass:Class
Extracted source (around line #2):
1: <h1>Add Form 3C </h1>
2: <%= form_for(@basiccase) do |f| %>
3: <%= render 'shared/error_messages', :object => f.object %>
4:
5: <div class="field">
'的error'。什麼錯誤? – Dogbert 2012-03-25 16:57:26
未定義方法'[]'爲零:NilClass – 2012-03-25 16:58:25
哪條線拋出該錯誤? – Dogbert 2012-03-25 17:17:45