Действие вложенного ресурса #index не работает должным образом - PullRequest
0 голосов
/ 07 мая 2020

У меня есть производители, вложенные в острова, однако мой индекс производителя № показывает все острова. Я ожидаю, что в каждом индексном представлении производителей будут перечислены только производители родительского острова. Пожалуйста, помогите!

routes.rb

Rails.application.routes.draw do
  devise_for :users

  root to: 'islands#index'

  resources :islands do
    resources :producers
  end
end

Islands.rb

class Island < ApplicationRecord
  has_many :producers, dependent: :destroy
  accepts_nested_attributes_for :producers

  validates :island_name, uniqueness: true, presence: true
  validates :island_country, presence: true

  validates_associated :producers
end

productions.rb

class Producer < ApplicationRecord
  belongs_to :user
  belongs_to :island
end

productions_controller.rb

class ProducersController < ApplicationController
  skip_before_action :authenticate_user!, only: [ :index ]
  before_action :set_island
  before_action :set_producer, only: [ :show, :edit, :update, :destroy]

  def index
    @producers = Producer.where(island_id: params[:island_id])
    @producers = policy_scope(Producer)
  end

 ...

  private

  def set_island
    @island = Island.find(params[:island_id])
  end

  def set_producer
    @producer = @island.producers.find(params[:id])
    authorize @producer
  end

  def producer_params
    params.require(:producer).permit(:producer_name, :email, :address1, :address2, :postal_code, :city, :country, :island_id)
  end
end

производители / индекс. html .erb

<div class="container">
  <h1>Producers from <%= @island.island_name %></h1>
  <ul>
    <% @producers.each do |producer| %>
      <div class="container">
        <h5><%= producer.producer_name %></h5>
        <h5><%= link_to "details", island_producer_path(producer.island, producer)%></h5>
      </div>
    <% end %>
  </ul>
</div>

1 Ответ

0 голосов
/ 07 мая 2020

Вам нужно передать область в policy_scope:

class Island < ApplicationRecord
  has_many :producers
end
class ProducersController < ApplicationController
  def index
    @producers = policy_scope(@island.producers)
  end
end
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...