Reputation: 118
Not sure what I screwed up... can't create a library_hour that belongs_to a library_location. On submit, library_hour is nil for all attributes except the library_location_id.
Routes
resources :library_locations do
resources :library_hours
end
class LibraryLocation < ActiveRecord::Base
has_many :library_hours
has_many :library_contacts
validates_presence_of :title, :image_thumbnail_url, :latitude, :longitude
validates_uniqueness_of :title
end
class LibraryHour < ActiveRecord::Base
belongs_to :library_location
validates_associated :library_location
validates_presence_of :day, :open_time, :close_time
validates :day, :numericality => { :only_integer => true }
validates_inclusion_of :day, :in => 0..6
end
before_filter :get_library_location
def get_library_location
@library_location = LibraryLocation.find(params[:library_location_id])
end
def create
@library_hour = @library_location.library_hours.build(params[:library_location])
respond_to do |format|
if @library_hour.save
format.html { redirect_to @library_location }
format.json { render json: @library_hour }
else
format.html { render action: "new" }
format.json { render json: @library_hour.errors }
end
end
end
<%= form_for([@library_location, @library_hour]) do |f| %>
<%= f.select :day, Date::DAYNAMES.each_with_index.collect { |day,i| [day,i] } %>
<%= f.time_select :open_time, {:minute_step => 15, :ignore_date => true} %>
<%= f.time_select :close_time, {:minute_step => 15, :ignore_date => true} %>
<%= f.hidden_field :library_location %>
<%= f.submit %>
<% end %>
ERRORS: All attributes appear to be nil on submit:
5 errors prohibited this library_hour from being saved:
Day can't be blank
Day is not a number
Day is not included in the list
Open time can't be blank
Close time can't be blank
What am I doing wrong? Thanks for any help/suggestions.
Rails Shell
Started POST "/library_locations/110/library_hours" for....
Processing by LibraryHoursController#create as HTML
Parameters:
{"utf8"=>"✓", "authenticity_token"=>"9x8fda9faj0q9e01=", "library_hour"=>
{"day"=>"3", "open_time(4i)"=>"05", "open_time(5i)"=>"00", "close_time(4i)"=>"16",
"close_time(5i)"=>"00", "library_location"=>"#<LibraryLocation:0x008fc24b46c7570>"},
"commit"=>"Submit", "library_location_id"=>"110"}
LibraryLocation SELECT "library_locations".*
FROM "library_locations"
WHERE "library_locations"."id" = ? LIMIT 1 [["id", "110"]]
begin transaction
LibraryLocation Load
SELECT "library_locations".*
FROM "library_locations"
WHERE "library_locations"."id" = 110 LIMIT 1
LibraryLocation Exists
SELECT 1 FROM "library_locations"
WHERE ("library_locations"."title" = 'Test Library'
AND "library_locations"."id" != 110) LIMIT 1
rollback transaction
Upvotes: 0
Views: 192
Reputation: 1929
You need to declare the attributes of LibraryHour to be attr_accessible.
class LibraryHour
attr_accessible :day, :open_time, etc
end
Otherwise you can't mass assign the attributes using build.
Upvotes: 1
Reputation: 13
Try to change these lines in controller and view:
@library_hour = @library_location.library_hours.build(params[:library_hour])
and
<%= form_for(@library_hour, :url => [@library_location, @library_hour]) do |f| %>
Upvotes: 1