ruby on rails - undefined local variable or method `city' ROR -
i created form using scaffold
, creating models
make nested model
error in browser , cannot solve , looking here, getting error :
nameerror in clients#new line #33 raised: undefined local variable or method `city' #<#<class:0xc4fb5bc>:0xb704f94> extracted source (around line #33): 30: <% end %> 31: </div> 32: <div class="field"> 33: <%= city.fields_for :street |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
city.rb
class city < activerecord::base attr_accessible :client_id, :name belongs_to :client has_many :streets accepts_nested_attributes_for :streets end
street.rb
class street < activerecord::base attr_accessible :city_id, :name belongs_to :city end
clients_controller.rb [ generated scaffold ]
def new @client = client.new @city = @client.build_city @street = @city.build_street # don't know should add line or not respond_to |format| format.html # new.html.erb format.json { render json: @client } end end
the form
<%= form_for(@client) |f| %> <% if @client.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@client.errors.count, "error") %> prohibited client being saved:</h2> <ul> <% @client.errors.full_messages.each |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 |city| %> <%= city.label :city %> <%= city.text_field :name %> <%= city.fields_for :street |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 %>
routes.rb
resources :clients resources :cities resources :streets end end
you have change model:
class city < activerecord::base attr_accessible :client_id, :name belongs_to :client has_many :streets accepts_nested_attributes_for :streets # add "s"! end
and have modify _form.html.erb
<%= city.fields_for :streets |street| %> ## add "s" <%= street.label :street %> <%= street.text_field :name %> <% end %>
and have modify controller mind.blank said in answer.
btw maybe in general understand accepts_nested_attributes_for bit better..
Comments
Post a Comment