Ruby on rails Rails 5-使用多态关联-呈现视图
我正在尝试学习如何在Rails 5应用程序中使用多态关联。我最近问了一个问题,但我编辑了很多次来展示我所尝试的所有东西,结果变得一团糟 我有一个叫做组织、提案和包的模型::Bip 这些协会是: 组织机构Ruby on rails Rails 5-使用多态关联-呈现视图,ruby-on-rails,ruby,views,polymorphic-associations,Ruby On Rails,Ruby,Views,Polymorphic Associations,我正在尝试学习如何在Rails 5应用程序中使用多态关联。我最近问了一个问题,但我编辑了很多次来展示我所尝试的所有东西,结果变得一团糟 我有一个叫做组织、提案和包的模型::Bip 这些协会是: 组织机构 has_many :bips, as: :ipable, class_name: Package::Bip accepts_nested_attributes_for :bips, reject_if: :all_blank, allow_destroy: true 提议 has_m
has_many :bips, as: :ipable, class_name: Package::Bip
accepts_nested_attributes_for :bips, reject_if: :all_blank, allow_destroy: true
提议
has_many :bips, as: :ipable, class_name: Package::Bip
accepts_nested_attributes_for :bips, reject_if: :all_blank, allow_destroy: true
包::Bip
belongs_to :ipable, :polymorphic => true, optional: true #, inverse_of: :bip
包::Bip可以与组织或提案关联。我正在努力弄清楚如何在我的提案展示中展示只属于提案的包::Bips,以及组织的包::Bips
My package::bip表有以下两列:
# ipable_id :integer
# ipable_type :string
ipable_类型设置为提案或组织
在我的提案中,我有:
<% if @proposal.bips.present? %>
<%#= render @proposal.bips %>
<%= link_to proposal_package_bips_path(@proposal) do %>
<% end %>
<% 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实例
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]
我的目标是,提案展示视图或组织展示视图应仅显示属于该具体提案的BIP
Package::Bip.pluck(:ipable_id)
(0.8ms) SELECT "package_bips"."ipable_id" FROM "package_bips"
=> [15, 13, 16, 1]
但是,当我提交我的提案时,显示的是:
<%= link_to proposal_package_bips_path(@proposal) do %>
在控制台中,我可以使用要应用的过滤器进行搜索
p = Proposal.last
Proposal Load (4.8ms) SELECT "proposals".* FROM "proposals" ORDER BY "proposals"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Proposal id: 16, user_id: 4, title: "test tenor", description: "test tenor", created_at: "2016-11-08 21:58:38", updated_at: "2016-11-08 21:58:38">
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"]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Package::Bip id: 19, identifier: "test tenor", description: nil, conditions: "test tenor", ipable_id: 16, ipable_type: "Proposal", created_at: "2016-11-08 21:58:38", updated_at: "2016-11-08 21:58:38", title: "test tenor", status: nil, classification: "Copyright">]>
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
结束
bip = Package::Bip.new
Package::Bip.ipable = parent
# @bip = Package::Bip.new(bip_params)
# authorize @bip
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
Package::Bip.all.pluck(:ipable_type)
(0.9ms) SELECT "package_bips"."ipable_type" FROM "package_bips"
=> ["Proposal", "Proposal", "Proposal", "Organisation", "Proposal"]
当我重新启动服务器并重试时,会收到一条错误消息,上面显示:
undefined method `package_bip_path' for #<#<Class:0x007fbce68389c0>:0x007fbcfdc31c18>
undefined method `empty?' for #<Class:0x007ff20920e218>
这显示了一个类似的问题,可以通过向路径中添加“[]”来解决
当我尝试时:
<%= link_to polymorphic_path([@proposal, Package::Bip]) do %>
日志似乎没有显示任何迹象表明它正在将提案或提案id筛选器应用于包bips
这对我来说没有任何意义,因为我的路线只有前缀“Organization”或“proposal”才可用:
rake routes | grep package_bip
organisation_package_bips GET /organisations/:organisation_id/package/bips(.:format) package/bips#index
new_organisation_package_bip GET /organisations/:organisation_id/package/bips/new(.:format) package/bips#new
edit_organisation_package_bip GET /organisations/:organisation_id/package/bips/:id/edit(.:format) package/bips#edit
organisation_package_bip GET /organisations/:organisation_id/package/bips/:id(.:format) package/bips#show
proposal_package_bips GET /proposals/:proposal_id/package/bips(.:format) package/bips#index
new_proposal_package_bip GET /proposals/:proposal_id/package/bips/new(.:format) package/bips#new
edit_proposal_package_bip GET /proposals/:proposal_id/package/bips/:id/edit(.:format) package/bips#edit
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]) %>
在控制台中,我尝试:
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
当我尝试这一点时,我可以探索提案参数:
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
我认为这是正确的
这些消息很奇怪,因为我还可以浏览BYEBug语句中的每个白名单项,并得到正确的响应。例如:
@proposal.id
15
当我尝试在Package::bips controller、index action中使用bye bug进行探索时,我可以看到:
params.inspect
"<ActionController::Parameters {\"controller\"=>\"package/bips\", \"action\"=>\"index\", \"proposal_id\"=>\"15\"} permitted: false>"
params.inspect
“\”程序包/bips\“,\”操作\“=>”索引\“,\”建议id \“=>”15\“}允许:false>”
allowed:false属性似乎是我代码中的一个问题
这是我的下一个问题。答案如下:
技巧似乎在于本文中所示的load_commentable方法。这对我来说太复杂了,弄不懂它的意思,但它是有效的
非常感谢那些乐于分享他们所知的人们所给予的帮助。谢谢Taryn&Bala。好的,所以您目前没有向我们展示您如何在控制器中设置
@proposal
。(这可能不相关,但我们拭目以待)。在您的控制台中,如果您确实执行控制器将执行的操作,这将是一件好事-例如,不要执行p=Proposal.last
doparams={:Proposal\u id=>0}
then@Proposal=Proposal.find(params[:Proposal\u id])
(或控制器中的任何内容)。然后尝试@proposal.bips
。我将在控制器中进行更多的日志记录,并尝试查找bip fetch的源代码。谢谢你的建议,我放弃了。我将为提案/组织BIP提供两个单独的资源,然后继续。这太难理解了,如果我坚持努力学习,我就不会在另一个黑暗的世界里迷失。“非常感谢你仍然试图帮助我。”@TarynEast-我几乎不敢相信,但我找到了答案。在这篇文章中,你可能会像我一样对load_commentable方法有更多的了解。再次非常感谢您在这方面的帮助。我讨厌放弃一切。:)
Started GET "/proposals/15/package/bips" for ::1 at 2016-11-13 11:24:32 +1100
Processing by Package::BipsController#index as HTML
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)
rake routes | grep package_bip
organisation_package_bips GET /organisations/:organisation_id/package/bips(.:format) package/bips#index
new_organisation_package_bip GET /organisations/:organisation_id/package/bips/new(.:format) package/bips#new
edit_organisation_package_bip GET /organisations/:organisation_id/package/bips/:id/edit(.:format) package/bips#edit
organisation_package_bip GET /organisations/:organisation_id/package/bips/:id(.:format) package/bips#show
proposal_package_bips GET /proposals/:proposal_id/package/bips(.:format) package/bips#index
new_proposal_package_bip GET /proposals/:proposal_id/package/bips/new(.:format) package/bips#new
edit_proposal_package_bip GET /proposals/:proposal_id/package/bips/:id/edit(.:format) package/bips#edit
proposal_package_bip GET /proposals/:proposal_id/package/bips/:id(.:format) package/bips#show
<% @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#.where(ipable_type: 'Proposal')
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
def proposal_params
params.require(:proposal).permit(:title, :description, :byline, :nda_required, :randd_maturities_list,
#Package
bips_attributes: [:id, :status, :classification, :identifier, :conditions, :title, :_destroy,
tenor_attributes: [:id, :express_interest, :commencement, :expiry, :enduring, :repeat, :frequency, :_destroy]
],
)
end
@proposal.id
15
params.inspect
"<ActionController::Parameters {\"controller\"=>\"package/bips\", \"action\"=>\"index\", \"proposal_id\"=>\"15\"} permitted: false>"