Absolutnie zablokowany, próbując utworzyć zagnieżdżone skojarzenie w formie szyn z has_many poprzez

Wysłałem wcześniejsze pytanie na ten temat i zalecono mi przeczytanie wielu istotnych informacji. Przeczytałem go i próbowałem wdrożyć około 30 różnych rozwiązań. Nic z tego nie zadziałało.

Oto, co mam.

Mam model Miniatures. Mam model producenta. Miniatury mają wielu producentów POPRZEZ model Productions.

Powiązania wydają się być poprawnie skonfigurowane, ponieważ mogę je wyświetlać w swoich widokach i tworzyć za pomocą konsoli. Tam, gdzie mam problem, pozwalam, aby miniatury NEW i EDIT tworzyły i aktualizowały tabelę Productions.

W konsoli polecenie@miniature.productions.create(manufacturer_id: 1) działa, co prowadzi mnie do przekonania, że ​​powinienem być w stanie zrobić to samo w formie.

MYŚLĘ, że mój problem jest zawsze w kontrolerze miniatur, a konkretnie w funkcji CREATE. Wypróbowałem tam mnóstwo rozwiązań dla innych ludzi i nikt tego nie zrobił. Możliwe jest również, że moje pole dla rzeczy w mojej formie jest błędne, ale wydaje się mniej skomplikowane.

Utknąłem na tym od wielu dni i choć są inne rzeczy, nad którymi mogę pracować, jeśli to skojarzenie nie jest możliwe, musiałbym przemyśleć całą moją aplikację.

Formularz tworzy teraz linię w tabeli Productions, ale nie zawiera wszystkich ważnych ID producenta.

Każda pomoc BARDZO ceniona.

Moja nowa miniatura

<% provide(:title, 'Add miniature') %>
<h1>Add a miniature</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(@miniature) do |f| %>
      <%= render 'shared/error_messages', object: f.object %>
      <%= f.label :name %>
      <%= f.text_field :name %>
       <%= f.fields_for :production do |production_fields| %>
      <%= production_fields.label :manufacturer_id, "Manufacturer" %>
      <%= production_fields.select :manufacturer_id, options_from_collection_for_select(Manufacturer.all, :id, :name) %>
      <% end %>
      <%= f.label :release_date %>
      <%= f.date_select :release_date, :start_year => Date.current.year, :end_year => 1970, :include_blank => true %>

      <%= f.submit "Add miniature", class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
</div>

Kontroler miniatur

class MiniaturesController < ApplicationController
   before_action :signed_in_user, only: [:new, :create, :edit, :update]
   before_action :admin_user,     only: :destroy

  def productions
    @production = @miniature.productions
  end

  def show
    @miniature = Miniature.find(params[:id])
  end

  def new
    @miniature = Miniature.new 
  end

  def edit
    @miniature = Miniature.find(params[:id])
  end

  def update
    @miniature = Miniature.find(params[:id])
    if @miniature.update_attributes(miniature_params)
      flash[:success] = "Miniature updated"
      redirect_to @miniature
    else
      render 'edit'
    end
  end
  def index
    @miniatures = Miniature.paginate(page: params[:page])
  end

  def create
    @miniature = Miniature.new(miniature_params)
    if @miniature.save
      @production = @miniature.productions.create
      redirect_to @miniature
    else
      render 'new'
    end
  end

  def destroy
    Miniature.find(params[:id]).destroy
    flash[:success] = "Miniature destroyed."
    redirect_to miniatures_url
  end

private
    def miniature_params
      params.require(:miniature).permit(:name, :release_date, :material, :scale, :production, :production_attributes)
    end

    def admin_user
      redirect_to(root_url) unless current_user.admin?
    end

    def signed_in_user
      unless signed_in?
        store_location
        redirect_to signin_url, notice: "Please sign in."
      end
    end
end

Miniaturowy model

class Miniature < ActiveRecord::Base
  has_many :productions, dependent: :destroy
  has_many :manufacturers, :through => :productions
  accepts_nested_attributes_for :productions

    validates :name, presence: true, length: { maximum: 50 }
    validates :material, presence: true
    validates :scale, presence: true
    validates_date :release_date, :allow_blank => true

  def name=(s)
    super s.titleize
  end

end

Model produkcji

class Production < ActiveRecord::Base
    belongs_to :miniature
    belongs_to :manufacturer



end

Model producenta

class Manufacturer < ActiveRecord::Base
    has_many :productions
    has_many :miniatures, :through => :productions
    validates :name, presence: true, length: { maximum: 50 }
    accepts_nested_attributes_for :productions
end

questionAnswers(1)

yourAnswerToTheQuestion