Недопустимые параметры с вложенными атрибутами вложенных атрибутов - PullRequest
0 голосов
/ 12 февраля 2019

У меня есть три модели, одна вложена в другую.

Модель магазина

class Shop < ApplicationRecord
  has_many :opening_days, dependent: :destroy
  accepts_nested_attributes_for :opening_days, allow_destroy: true
end

Модель открытия дня

class OpeningDay < ApplicationRecord
  belongs_to :shop, optional: true
  has_many :opened_intervals, dependent: :destroy
  accepts_nested_attributes_for :opened_intervals, allow_destroy: true
end

И модель открытия_интервала

class OpenedInterval < ApplicationRecord
  belongs_to :opening_day, optional: true
end

У меня есть simple_form с вложенными атрибутами:

<%= simple_form_for @shop do |f| %>
    <p>opening days</p>
    <div class="opening-days-fields">
      <% i = 0 %>
      <%= f.simple_fields_for :opening_days do |builder_opening_day| %>
        <%= render 'shops/opening_day_fields', builder_opening_day: builder_opening_day, i: i %>
        <% i = i + 1 %>
      <% end %>
    </div>

    <%= f.submit "Update shop", class: "btn-blue-whole-screen" %>
  <% end %>

Партиал с открытием_day_fields.rb

<div class="opening-day">
  <div class="day">
    <%= builder_opening_day.input :day %>
  </div>
  <div class="full-day">
    <%= builder_opening_day.input :full_day %>
  </div>
</div>

<div class="fields">
  <%= builder_opening_day.simple_fields_for :opened_intervals do |builder_opened_interval| %>
    <%= render 'shops/opened_interval_fields', o: builder_opened_interval %>
  <% end %>
  <%= link_to_add_row('Add opened interval', builder_opening_day, :opened_intervals, i, class: 'btn btn-primary') %>
</div>

И второй партиал с открытием_interval_fields.rb

<div class="opened-interval">
  <div class="start">
    <%= o.input :start %>
  </div>
  <div class="end">
    <%= o.input :end %>
  </div>
</div>

В контроллере я разрешаю вложенные атрибуты:

def shop_params
    params.require(:shop).permit(:commercial_name, :legal_name, :company_number, :address, :photo, :photo_cache, :latitude, :longitude, :trading_name, :city, :postcode, :phone, opening_days_attributes: [:id, :day, :full_day], opened_intervals_attributes: [:id, :start, :end])
  end

Но Rails их не принимает:

Started PATCH "/shops/1" for 127.0.0.1 at 2019-02-12 00:22:50 +0100
Processing by ShopsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Fzrejt7v1LGlMqBwm9+WPNvaNui+anu3uaMHqbDqOszfC2uuB/mDG4Au5Ni1e6Qd2utJemsy6vbZOZWxjGV7pg==", "shop"=>{"legal_name"=>"Juan Blanco S.A.", "trading_name"=>"Blanco GmbH", "company_number"=>"1000", "address"=>"Viladomat 1", "city"=>"", "postcode"=>"", "photo_cache"=>"", "opening_days_attributes"=>{"0"=>{"day"=>"0", "full_day"=>"0"}, "1"=>{"day"=>"1", "full_day"=>"0"}, "2"=>{"day"=>"2", "full_day"=>"0"}, "3"=>{"day"=>"3", "full_day"=>"0"}, "4"=>{"day"=>"4", "full_day"=>"0"}, "5"=>{"day"=>"5", "full_day"=>"0"}, "6"=>{"day"=>"6", "full_day"=>"1"}, "1549927366547"=>{"opened_intervals_attributes"=>{"1549927366547"=>{"start(1i)"=>"2019", "start(2i)"=>"2", "start(3i)"=>"11", "start(4i)"=>"13", "start(5i)"=>"22", "end(1i)"=>"2019", "end(2i)"=>"2", "end(3i)"=>"11", "end(4i)"=>"23", "end(5i)"=>"22"}}}}}, "commit"=>"Update shop", "id"=>"1"}
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/albert/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
  Shop Load (0.5ms)  SELECT  "shops".* FROM "shops" WHERE "shops"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/shops_controller.rb:35
Unpermitted parameter: :opened_intervals_attributes

Вот как выглядят параметры:

=> <ActionController::Parameters {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"PaCVcyYikcUblOj2dlPTQQr3VtlZNDGNFi5Iar0TluHbhsyNxs7QGCRMDVQzQw9WM4ykDQCiJQ0ZQMAQSQsovA==", "shop"=>{"legal_name"=>"Juan Blanco S.A.", "trading_name"=>"Blanco GmbH", "company_number"=>"1000", "address"=>"Viladomat 1", "city"=>"", "postcode"=>"", "photo_cache"=>"", "opening_days_attributes"=>{"0"=>{"day"=>"0", "full_day"=>"0", "id"=>"1"}, "1"=>{"day"=>"1", "full_day"=>"0", "id"=>"2"}, "2"=>{"day"=>"2", "full_day"=>"0", "id"=>"3"}, "3"=>{"day"=>"3", "full_day"=>"0", "id"=>"4"}, "4"=>{"day"=>"4", "full_day"=>"0", "id"=>"5"}, "5"=>{"day"=>"5", "full_day"=>"0", "id"=>"6"}, "6"=>{"day"=>"6", "full_day"=>"1", "id"=>"7"}, "1549928814708"=>{"opened_intervals_attributes"=>{"1549928814708"=>{"start(1i)"=>"2019", "start(2i)"=>"2", "start(3i)"=>"11", "start(4i)"=>"23", "start(5i)"=>"41549928814708", "end(1i)"=>"2019", "end(2i)"=>"2", "end(3i)"=>"11", "end(4i)"=>"23", "end(5i)"=>"41549928814708"}}}}}, "commit"=>"Update shop", "controller"=>"shops", "action"=>"update", "id"=>"1"} permitted: false>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...