0
我不知道如果我corretly构建我的应用程序(我一直在学习Rails的2个月了),但我建立一个非常深度嵌套的应用程序,它看起来像这样:NoMethodError与深度嵌套模式
用户has_many帐户>帐户has_many字符>字符has_many项目
因此,它是4级(至少这是计划)。
我目前在字符,我有麻烦创建形式是抛出此错误:undefined method 'characters' for nil:NilClass
(screenshot)。
这里是在GitHub上的项目:https://github.com/imjp/d2shed
characters_controller.rb
class CharactersController < ApplicationController
def create
@user = User.find(params[:user_id])
@account = Account.find(params[:account_id])
@character = @account.characters.create(params[:character])
redirect_to root_url
end
end
character.rb
class Character < ActiveRecord::Base
attr_accessible :name, :type
belongs_to :account
end
_form.html.erb
<%= form_for([@account, @account.characters.build]) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.radio_button(:type, "SC") %>
<%= f.label(:type, "SC") %>
<%= f.radio_button(:type, "HC") %>
<%= f.label(:type, "HC") %>
<%= f.radio_button(:type, "SCL") %>
<%= f.label(:type, "SCL") %>
<%= f.radio_button(:type, "HCL") %>
<%= f.label(:type, "HCL") %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
嘿男人。我添加了更改,但我仍然收到了'nil:NilClass'错误的未定义方法'字符':/ – imjp
下面是错误的截图:http://i.imgur.com/itL98.png – imjp
因为有没有帐户实例变量。此外,您的表单不正确,应该是用户,帐户...而不是帐户,帐户 –