0

不知道我搞砸了什么...无法创建属于 library_location 的 library_hour。提交时,library_hour 对于除 library_location_id 之外的所有属性都是 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

图书馆时间控制器

    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

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

添加

导轨外壳

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
4

2 回答 2

1

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

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

<%= form_for(@library_hour, :url => [@library_location, @library_hour]) do |f| %>
于 2012-12-13T10:57:35.370 回答
1

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

class LibraryHour
    attr_accessible :day, :open_time, etc
end

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

于 2012-12-13T16:33:03.923 回答