0

私が何を台無しにしたのかわかりません... 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 %>

エラー: 送信時にすべての属性が nil のように見えます:

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

LibraryHour の属性を attr_accessible として宣言する必要があります。

class LibraryHour
    attr_accessible :day, :open_time, etc
end

そうしないと、ビルドを使用して属性を一括で割り当てることができません。

于 2012-12-13T16:33:03.923 に答える
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 に答える