my routes
TerritoryManagement::Application.routes.draw do
get "new" => "territories#new", :as => "new"
root :to => "territories#new"
resources :territories
resources :users
end
my model
class Territory < ActiveRecord::Base
validates :name, :presence => true, :uniqueness => true
attr_accessor :name
end
my controller
class TerritoriesController < ApplicationController
def index
@territories = Territory.all
end
def show
@territory = Territory.find(params[:id])
end
def new
@territory = Territory.new
end
def create
@territory = Territory.new(params[:territory])
if @territory.save
redirect_to root_url, :notice => "Product successfully created!"
else
render "new"
end
end
end
my view
<%= form_for(@territory) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>`
my database for some reason the app is generating empty records...
sqlite> select * from territories;
1||2011-08-21 09:44:43.611946|2011-08-21 09:44:43.611946
Any ideas what's going wro开发者_JS百科ng?
You're confusing attr_accessor with attr_accessible.
EDIT:
attr_accessor is a method that creates getter and setter for the given input symbols. Using it in an ActiveRecord models surely is not a good idea if you're rewriting database properties. I can't say off the top of my head what could happen but ActiveRecord surely misbehaves with it. Your name property is a field of your territories table I think and with rails 3 you have to declare which fields you want to be accessibile with mass-assignment. See docs : http://api.rubyonrails.org/classes/ActiveModel/MassAssignmentSecurity/ClassMethods.html#method-i-attr_accessible
I hope this edit helps ;)
I'm not sure but try to remove the attr_accessor
, probably you don't need that.
精彩评论