사용자가 contest_questions 몇 가지를 예측하는 항목에 대한 양식을 작성하려고합니다.3 차 모델을 기반으로 중첩 된 폼 질문 구성
즉, 필자는 tipster-contest와 관련된 contest_questions를 동적으로 가져 와서 사용자가 해당 질문 각각에 대한 예측으로 항목을 등록하게하려는 것입니다.
어떻게하면됩니까? 지금부터 나는 분야는 contest_question 블록 내에서 실행되지 않습니다 <%= f.fields_for :predictions do |prediction| %>
때문에 게재되지 않습니다 내가 contest_question.fields_for ...
로 변경하면 내가 얻을 #
tipster_contest에 대한 '
정의되지 않은 메서드`fields_for .rb
class TipsterContest < ActiveRecord::Base belongs_to :bookmaker belongs_to :game has_many :entries has_many :contest_questions extend FriendlyId friendly_id :name, use: [:slugged, :history] enum status: { "Scheduled" => 0, "Open" => 1, "Closed" => 2, "Graded" => 3 } scope :published?, -> { where(published: :true) } end
entry.rb
,class Entry < ActiveRecord::Base belongs_to :user belongs_to :tipster_contest has_many :predictions accepts_nested_attributes_for :predictions end
contest_question.rb
class ContestQuestion < ActiveRecord::Base belongs_to :tipster_contest has_many :predictions end
prediction.rb
class Prediction < ActiveRecord::Base belongs_to :entry has_one :contest_question end
_form.html.erb
,<%= form_for(@entry) do |f| %> <% @contest.contest_questions.order(name: :asc).each do |contest_question| %> <%= f.fields_for :predictions do |prediction| %> <div class="field"> <%= prediction.label contest_question.name %> <%= prediction.select(:prediction, @contest.teams) %> </div> <div class="field"> <%= prediction.label :wager_amount %> <%= prediction.input :wager_amount %> </div> <% end %> <% end %> <div class="actions"> <%= f.submit "Save entry", :class => "btn btn-success" %> <%= link_to 'Back', bets_path, :class => "btn btn-danger" %> </div> <% end %>
그리고 내 schema.rb의 관련 부분은 해당이 필요할 것이다 경우 :
create_table "contest_questions", force: true do |t| t.integer "tipster_contest_id" t.string "name" t.string "result" t.integer "min_wager" t.integer "max_wager" t.datetime "created_at" t.datetime "updated_at" end create_table "entries", force: true do |t| t.integer "user_id" t.integer "tipster_contest_id" t.integer "bankroll" t.integer "won" t.string "currency" t.boolean "entry_valid" t.datetime "created_at" t.datetime "updated_at" end create_table "predictions", force: true do |t| t.integer "entry_id" t.integer "contest_question_id" t.string "prediction" t.integer "wager_amount" t.datetime "created_at" t.datetime "updated_at" end create_table "tipster_contests", force: true do |t| t.integer "bookmaker_id" t.integer "game_id" t.string "name" t.string "tournament" t.integer "status", default: 0 t.text "rules" t.integer "prizepool" t.string "currency" t.text "payout_structure" t.integer "tipster_wallet" t.string "teams", default: [], array: true t.datetime "registration_open" t.datetime "registration_close" t.boolean "published", default: false t.datetime "created_at" t.datetime "updated_at" t.string "logo" t.text "description" t.string "slug" end add_index "tipster_contests", ["slug"], name: "index_tipster_contests_on_slug", using: :btree