I'm trying to learn how to use polymorphic associations in my Rails 5 application. I recently asked this question, but I edited it so many times to show everything I tried, it got dirty
I have models called Organization, Offer and Package :: Bip.
Associations:
Organization
has_many :bips, as: :ipable, class_name: Package::Bip
accepts_nested_attributes_for :bips, reject_if: :all_blank, allow_destroy: true
Sentence
has_many :bips, as: :ipable, class_name: Package::Bip
accepts_nested_attributes_for :bips, reject_if: :all_blank, allow_destroy: true
Package :: Beep
belongs_to :ipable, :polymorphic => true, optional: true
Package :: Bip can be associated with either the Organization or the proposal. I'm struggling to figure out how to show the :: Bips package, which apply only to the proposal in my proposal, and the same goes for the organization.
My package :: beep table has these 2 columns:
# ipable_id :integer
# ipable_type :string
Ipable_type is set to either Offer or Organization.
In my sentence sentence, I have:
<% if @proposal.bips.present? %>
<%
<%= link_to proposal_package_bips_path(@proposal) do %>
<% end %>
<% end %>
. , , : :
<% if @proposal.bips.present? %>
<%= render @proposal.bips %>
<% end %>
, (@proposal) package_bip . . , , package_bips ( , ).
:
def index
@proposals = Proposal.all
end
def show
@bips = @proposal.bips
end
:: BipsController
def index
if params[:ipable_type] == "Proposal"
@bips = Package::Bip.where(:ipable_type == 'Proposal')
else params[:ipable_type] == 'Organisation'
@bips = Package::Bip.where(:ipable_type == 'Organisation')
end
, , .
4 Package:: Bip db.
Package::Bip.pluck(:ipable_type)
(0.8ms) SELECT "package_bips"."ipable_type" FROM "package_bips"
=> ["Proposal", "Proposal", "Proposal", "Organisation"]
3 , 1 .
3, , .
Package::Bip.pluck(:ipable_id)
(0.8ms) SELECT "package_bips"."ipable_id" FROM "package_bips"
=> [15, 13, 16, 1]
, Bips, .
, , :
<%= link_to proposal_package_bips_path(@proposal) do %>
, /package/bips/index.html.erb :
<% @bips.each do |ip| %>
<%= ip.title.titleize %>
<%
, , 1 package_bip. 4 (2 .bips , ). .
, bips/index.html.erb bips/show.html.erb. , :
<%= link_to 'More details', package_bip_path(ip) %> <
, :
undefined method `package_bip_path' for #<#<Class:0x007fbce68389c0>:0x007fbcfdc31c18>
, , , , .
:
resources :proposals do
namespace :package do
resources :bips
end
resources :organisations do
namespace :package do
resources :bips
end
, .
p = Proposal.last
Proposal Load (4.8ms) SELECT "proposals".* FROM "proposals" ORDER BY "proposals"."id" DESC LIMIT $1 [["LIMIT", 1]]
=>
2.3.1p112 :199 > p.bips
Package::Bip Load (0.3ms) SELECT "package_bips".* FROM "package_bips" WHERE "package_bips"."ipable_id" = $1 AND "package_bips"."ipable_type" = $2 [["ipable_id", 16], ["ipable_type", "Proposal"]]
=>
2.3.1p112 :200 > p.bips.count
(3.5ms) SELECT COUNT(*) FROM "package_bips" WHERE "package_bips"."ipable_id" = $1 AND "package_bips"."ipable_type" = $2 [["ipable_id", 16], ["ipable_type", "Proposal"]]
=> 1
.
- , , , , (, /), /?
UPDATE
.
, :
create :
def create params [: organisation_id] parent = Organisation.find(params [: organisation_id]) elsif params [: offer_id] parent = Proposal.find(params [: offer_id])
bip = Package::Bip.new
Package::Bip.ipable = parent
respond_to do |format|
if @bip.save
format.html { redirect_to @bip }
format.json { render :show, status: :created, location: @bip }
else
format.html { render :new }
format.json { render json: @bip.errors, status: :unprocessable_entity }
end
end
:
, , :: Bips .
, :: Bips, .
Package::Bip.all.pluck(:ipable_type)
(0.9ms) SELECT "package_bips"."ipable_type" FROM "package_bips"
=> ["Proposal", "Proposal", "Proposal", "Organisation", "Proposal"]
, :
undefined method `empty?' for #<Class:0x007ff20920e218>
:
<%= polymorphic_path(@proposal, Package::Bip) %>
:
Started POST "/__better_errors/3f34303f5f5c670f/variables" for ::1 at 2016-11-13 10:58:13 +1100
Package::Bip Load (0.4ms) SELECT "package_bips".* FROM "package_bips" WHERE "package_bips"."ipable_id" = $1 AND "package_bips"."ipable_type" = $2 [["ipable_id", 15], ["ipable_type", "Proposal"]]
, '[]' .
:
<%= link_to polymorphic_path([@proposal, Package::Bip]) do %>
( , ).
, , ( - ).
Started GET "/proposals/15/package/bips" for ::1 at 2016-11-13 11:24:32 +1100
Processing by Package::BipsController
Parameters: {"proposal_id"=>"15"}
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 4], ["LIMIT", 1]]
Rendering package/bips/index.html.erb within layouts/application
Package::Bip Load (0.6ms) SELECT "package_bips".* FROM "package_bips"
Rendered package/bips/index.html.erb within layouts/application (5.4ms)
, package_bips.
, :
rake routes | grep package_bip
organisation_package_bips GET /organisations/:organisation_id/package/bips(.:format) package/bips
new_organisation_package_bip GET /organisations/:organisation_id/package/bips/new(.:format) package/bips
edit_organisation_package_bip GET /organisations/:organisation_id/package/bips/:id/edit(.:format) package/bips
organisation_package_bip GET /organisations/:organisation_id/package/bips/:id(.:format) package/bips
proposal_package_bips GET /proposals/:proposal_id/package/bips(.:format) package/bips
new_proposal_package_bip GET /proposals/:proposal_id/package/bips/new(.:format) package/bips
edit_proposal_package_bip GET /proposals/:proposal_id/package/bips/:id/edit(.:format) package/bips
proposal_package_bip GET /proposals/:proposal_id/package/bips/:id(.:format) package/bips
, show , /package/bips/index.html.erb :
<% @bips.each do |ip| %>
<%= link_to polymorphic_path([@ipable, ip]) %>
:
undefined method `package_bip_path' for #<#<Class:0x007f9e8ac29248>:0x007f9e939c9508>
. :
def set_proposal
@proposal = Proposal.find(params[:id])
end
:
params = {:proposal_id => 15}
=> {:proposal_id=>15}
2.3.1p112 :031 > @proposal = Proposal.find(params[:proposal_id])
Proposal Load (0.7ms) SELECT "proposals".* FROM "proposals" WHERE "proposals"."id" = $1 LIMIT $2 [["id", 15], ["LIMIT", 1]]
=> #<Proposal id: 15, user_id: 4, title: "testing filter", description: "testing filter", byline: "testing filter", nda_required: true, created_at: "2016-11-08 00:59:09", updated_at: "2016-11-08 00:59:09">
2.3.1p112 :033 > @proposal.bips
Package::Bip Load (0.7ms) SELECT "package_bips".* FROM "package_bips" WHERE "package_bips"."ipable_id" = $1 AND "package_bips"."ipable_type" = $2 [["ipable_id", 15], ["ipable_type", "Proposal"]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Package::Bip id: 17, identifier: "testing filter", description: nil, conditions: "testing filter", ipable_id: 15, ipable_type: "Proposal", created_at: "2016-11-08 00:59:09", updated_at: "2016-11-08 00:59:09", title: "testing filter", status: nil, classification: "Patent">, #<Package::Bip id: 21, identifier: "dd", description: nil, conditions: "dd", ipable_id: 15, ipable_type: "Proposal", created_at: "2016-11-10 22:47:54", updated_at: "2016-11-10 22:47:54", title: "ldsjflkjklsdjfl", status: nil, classification: "Design">]>
2.3.1p112 :034 > @proposal.bips.count
(6.3ms) SELECT COUNT(*) FROM "package_bips" WHERE "package_bips"."ipable_id" = $1 AND "package_bips"."ipable_type" = $2 [["ipable_id", 15], ["ipable_type", "Proposal"]]
=> 2
.
, , :
def show
@images = @proposal.images
byebug
puts 'testing proposal controller'
@bips = @proposal.bips
end
, :
params
<ActionController::Parameters {"controller"=>"proposals", "action"=>"show", "id"=>"15"} permitted: false>
(byebug) proposal_params
*** ActionController::ParameterMissing Exception: param is missing or the value is empty: proposal
nil
, , "require" .
_ :
def proposal_params
params.require(:proposal).permit(:title, :description, :byline, :nda_required, :randd_maturities_list,
bips_attributes: [:id, :status, :classification, :identifier, :conditions, :title, :_destroy,
tenor_attributes: [:id, :express_interest, :commencement, :expiry, :enduring, :repeat, :frequency, :_destroy]
],
)
end
, .
, , whitelisted . :
@proposal.id
15
Package:: bips, , :
params.inspect
"<ActionController::Parameters {\"controller\"=>\"package/bips\", \"action\"=>\"index\", \"proposal_id\"=>\"15\"} permitted: false>"
: false .
.