关联模型的“创建”中的所有属性均为零

时间:2012-12-13 10:43:55

标签: ruby-on-rails ruby ruby-on-rails-3

不确定我搞砸了什么......无法创建属于library_location的library_hour。在提交时,除了library_location_id之外的所有属性,library_hour都是nil。

路线

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

LibraryHoursController

    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

表格“/ library_locations /:id / library_hours / new”

    <%= 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 %>

错误:提交时所有属性似乎都为零:

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

我做错了什么?感谢您的任何帮助/建议。

ADDED

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

2 个答案:

答案 0 :(得分:1)

尝试在控制器和视图中更改这些行:

@library_hour = @library_location.library_hours.build(params[:library_hour])

<%= form_for(@library_hour, :url => [@library_location, @library_hour]) do |f| %>

答案 1 :(得分:1)

您需要将LibraryHour的属性声明为attr_accessible。

class LibraryHour
    attr_accessible :day, :open_time, etc
end

否则,您无法使用build来批量分配属性。