を使用してフォームをscaffold
作成し、作成Models
するために作成しましたnested model
が、ブラウザでエラーが発生し、解決できません。ここでヘルプを探しています。そのエラーが発生しています:
NameError in Clients#new
line #33 raised:
undefined local variable or method `city' for #<#<Class:0xc4fb5bc>:0xb704f94>
Extracted source (around line #33):
30: <% end %>
31: </div>
32: <div class="field">
33: <%= city.fields_for :street do |street| %>
34: <%= street.label :street %>
35: <%= street.text_field :name %>
36: <% end %>
Client.rb
class Client < ActiveRecord::Base
attr_accessible :email, :name
has_one :city
accepts_nested_attributes_for :city
end
都市.rb
class City < ActiveRecord::Base
attr_accessible :client_id, :name
belongs_to :client
has_many :streets
accepts_nested_attributes_for :streets
end
ストリート.rb
class Street < ActiveRecord::Base
attr_accessible :city_id, :name
belongs_to :city
end
client_controller.rb [ scaffold で生成]
def new
@client = Client.new
@city = @client.build_city
@street = @city.build_street # I don't know should I add this line or not
respond_to do |format|
format.html # new.html.erb
format.json { render json: @client }
end
end
フォーム
<%= form_for(@client) do |f| %>
<% if @client.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@client.errors.count, "error") %> prohibited this client from being saved:</h2>
<ul>
<% @client.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.fields_for :city do |city| %>
<%= city.label :city %>
<%= city.text_field :name %>
<%= city.fields_for :street do |street| %>
<%= street.label :street %>
<%= street.text_field :name %>
<% end %>
<% end %>
</div>
<div class="field">
</div>
<div class="actions">
<%= f.submit "Submit Client", class: "btn btn-large btn-primary" %>
</div>
<% end %>
ルート.rb
resources :clients do
resources :cities do
resources :streets
end
end