Rails, Polymorphic Association - Display only related instances

I am trying to learn how to use polymorphic associations in my Rails 5 application.

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, inverse_of: :bip

Package :: Bip can be associated with either the Organization or the proposal. I am struggling to figure out how to show the package :: Bips, which belong 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

The type is set either in the proposal or in the organization.

In my sentence sentence, I have:

<% if @proposal.bips.present? %>
    <%= link_to package_bips_path(@proposal) do %> 
<% end %>

, (@proposal) package_bip . . , , package_bips ( , ).

:

    def show
    @images = @proposal.images
    @bips = @proposal.bips.where(ipable_type: 'Proposal')
  end

, , , , ipable_type, .

, if Package:: Bips, , ipable_type. , , ( , , ).

Package:: Bips :

def index


    if params[:ipable_type]
      @bips = Package::Bip.where(ipable_type: 'Proposal')
    elsif params[:ipable_type]
      @bips = Package::Bip.where(ipable_type: 'Organisation')
    else
       @bips = Package::Bip.all
    end

    # authorize @bips
  end

. , , , .

/ - :

<%= link_to package_bips_path(@proposal) do %>

/package/bips/index.html. :

  <% @bips.each do |ip| %>. 

( , ). , @ / , bips () . .

:

 <%= @proposal.bips.first.title %> 

, , .

, ( ) , . , -, , ?

//show.html.erb

<% if @proposal.bips.present? %>
    <%= link_to package_bips_path(@proposal) do %> 
      <h6 style="margin:2%; "> Intellectual Property </h6>
    <% end %>
<% end %>

/Bips/index.html.erb

<% @bips.each do |ip| %>
   <%= ip.title.titleize %>
   <%= ip.classification.humanize %>
<% end %>

/Bips/show.html.erb

<%= @bip.title.titleize %></h5><p><%= @bip.status %>

def index
    @proposals = Proposal.all
    # @bips = @proposal.bips
    # authorize @proposals
  end

  def show
    @images = @proposal.images
    @bips = @proposal.bips#.where(ipable_type: 'Proposal')
  end

bips

def index
    # if params[:status]
    #   @bips = Package::Bip.where(:status => params[:status])
    # else
    #   @bips = Package::Bip.all
    # end  

    if params[:ipable_type]
      @bips = Package::Bip.where(ipable_type: 'Proposal')
    elsif params[:ipable_type]
      @bips = Package::Bip.where(ipable_type: 'Organisation')
    else
       @bips = Package::Bip.all
    end

    # authorize @bips
  end

:

http://rails-bestpractices.com/posts/2010/09/23/generate-polymorphic-url/

, "", - , -.

, :

resources :proposals do 
    namespace :package do
      resources :materials
      resources :insights
      resources :facilities
      resources :participants
      resources :fundings
      resources :facts
      resources :bips 
    end

... .

. , , , . ?

:

http://www.codequizzes.com/rails/learn-rails/polymorphism

. - , ?

Package:: Bips:

def index

    if Package::Bip.ipable_type: 'Proposal'
      @bips = Package::Bip.where(ipable_type: 'Proposal')
    elsif Package::Bip.ipable_type: 'Organisation'
      @bips = Package::Bip.where(ipable_type: 'Organisation')
    else
       @bips = Package::Bip.all
    end

, .

, :

undefined method `package_bips_path' for #<#<Class:0x007fa72ea9b730>:0x007fa72ea93ee0>

:

<% if @proposal.bips.present? %>
    <%= link_to package_bips_path(@proposal) do %> 
    <h6 style="margin:2%; "> Intellectual Property </h6>
<% end %>

bips, :

 if params[:ipable_type] == 'Proposal'
      @bips = Package::Bip.where(ipable_type: 'Proposal')
    else params[:ipable_type] ==  'Organisation'
      @bips = Package::Bip.where(ipable_type: 'Organisation')
    # else
    #    @bips = Package::Bip.all
    end

:

<% if @proposal.bips.present? %>
    <%= link_to proposal_package_bips_path(@proposal) do %> 
    <% end %>
<% end %>

/Bips/

<div class="container-fluid" style="margin-bottom: 50px">
  <h2 style="text-align: center">Intellectual Property Assets</h2>

    <div class="row">
      <div class="col-sm-10 col-sm-offset-1">
        <div class="row" style="margin-top:50px; margin-bottom:50px">
          <div class="col-md-8 col-md-offset-2">
              <span style="margin-right:50px; padding-left:50px"> 
                <%= link_to "All", action: :index %> 
              </span>  
              <span style="color:grey">ยท</span>
                <span style="margin-right:50px; padding-left:50px"> 
                  <%#= link_to "Intellectual Property we can offer", package_bips_path(:status => "Offered") %> 
                </span>
                <span style="color:grey">ยท</span>
                <span style="margin-right:50px; padding-left:50px">
                  <%#= link_to "Intellectual Property sought", package_bips_path(:status => "Sought") %>
                </span>
          </div>
        </div>
      </div>
    </div> 


    <div class="row">
      <div class="col-xs-10 col-xs-offset-1">
        <div class="table-responsive" style="margin-left:30px; margin-top:15px">
          <table class="table table-bordered">
            <tr>
              <td> <h5>Title</h5> </td>
              <td> <h5>Asset</h5> </td>
              <td> <h5>Added</h5> </td>

             <%# if policy(@package_ips).update? %> 
                <td> <h5>Manage this asset</h5></td> 
             <%# end %>

            </tr>
            <% @bips.each do |ip| %>


            <tr>

              <td>
                  <%= ip.title.titleize %>
              </td>
              <td>
                  <%= ip.classification.humanize %>
              </td>
              <td>
                  <%= ip.created_at.try(:strftime, '%e %B %Y') %>
              </td>

              <%# if policy(@package_ips).update? %>
                <td>  
                   <%#= link_to 'More details', package_bip_path(ip) %> <span style="color:grey">ยท</span>
                   <%#= link_to "Edit", edit_package_ip_organistion_path(@organisation) %>
                   <!-- <span style="color:grey">ยท</span> -->
                   <%#= link_to 'Destroy', ip, method: :delete, data: { confirm: 'Are you sure?' } %>
                </td>  
              <%# end %>   

            </tr> 
            <% end %> 
          </table>
        </div>
      </div>
    </div>          
</div>

(- ), , , . : status params show bips, , / - .

:

 if params[:ipable_type] == "Proposal"
      @bips = Package::Bip.where(:ipable_type == 'Proposal')
    else params[:ipable_type] ==  'Organisation'
      @bips = Package::Bip.where(:ipable_type == 'Organisation')

, ( ).

!

:

p = Proposal.last
  Proposal Load (6.1ms)  SELECT  "proposals".* FROM "proposals" ORDER BY "proposals"."id" DESC LIMIT $1  [["LIMIT", 1]]
 => #<Proposal id: 15, user_id: 4, title: "testing filter",  created_at: "2016-11-08 00:59:09", updated_at: "2016-11-08 00:59:09"> 
2.3.1p112 :123 > p.bips
  Package::Bip Load (0.5ms)  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",  ipable_id: 15, ipable_type: "Proposal", created_at: "2016-11-08 00:59:09", updated_at: "2016-11-08 00:59:09"]> 

. :

o = Organisation.first
  Organisation Load (1.1ms)  SELECT  "organisations".* FROM "organisations" ORDER BY "organisations"."id" ASC LIMIT $1  [["LIMIT", 1]]
 => #<Organisation id: 1, title: "bhjhghdddd",  created_at: "2016-10-21 05:20:39", updated_at: "2016-11-06 21:36:30"> 
2.3.1p112 :125 > o.bips
  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", 1], ["ipable_type", "Organisation"]]
 => #<ActiveRecord::Associations::CollectionProxy []> 

.

, .

: http://www.codequizzes.com/rails/learn-rails/polymorphism

:

# views/articles/show.html.erb
<%= render @article.comments %>

# views/comments/_comment.html.erb
<%= comment.body %><br />
The render @article.comments in the show page automatically knows to load a file called views/comments/_comment.html.erb. This is Rails magic, so just memorize this.

, views/package/bips _bips.html.erb.

, :

<%= render @proposal.bips %>

:

Missing partial package/bips/_bip with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in:
  * "/Users/d/cv/cflive/app/views"

. , - . :

resources :proposals do 
    namespace :package do
       resources :bips 
    end

- , ?

0

Source: https://habr.com/ru/post/1660361/


All Articles