0

私はかなり長い間SOとインターネットを精査してきましたが、これを理解することができませんでした. 常に 1 つの TimerSetting を持つモデル プロジェクトがあります。結合テーブルは含まれません。

新しいプロジェクト フォームでは、ネストされた属性を使用して TimerSetting レコードを作成しようとしています。この部分が解りません。

モデル内の関連コード:

プロジェクト モデル:

class Project < ActiveRecord::Base
    attr_accessible :timer_setting_id

    has_one :timer_setting
    accepts_nested_attributes_for :timer_setting
end

TimerSetting モデル:

class TimerSetting < ActiveRecord::Base
    attr_accessible :rounding_method, :round_to

    belongs_to :project
end

プロジェクト コントローラーで:

def new
    @project.new
    @project.build_timer_setting
end

ビューで:

<%= form_for @project, {remote: true, format: 'json'} do |f| %>
    ... other stuff ...
    <%= f.fields_for :timer_setting do |ts| %>
      Rounding Method <%= ts.check_box :rounding_method %>
      Round To <%= ts.text_field :round_to %>
    <% end %>
<% end %>

フォーム (projects/new) をレンダリングするルートを呼び出すと、Rails は次のように言っていますunknown attribute: project_id。プロジェクトコントローラーの行をコメントアウトすると

#@project.build_timer_setting

f.fields_for :timer_settingフォームはレンダリングされますが、ブロック内のフィールドは出力されません。

アドバイス/ヘルプをいただければ幸いです。

4

1 に答える 1

0

TimerSetting には project_id が必要です。Rails は TimerSetting モデルの project_id を介して見つけるため、プロジェクトに timer_settings_id は必要ありません。

于 2013-03-05T09:56:00.300 に答える