简体   繁体   中英

Rails, polymorphic associations, and form helpers

For some reason, I cannot use a polymorphic association and nested forms. Here are my User , Company , and Subscription models:

#app/models/user.rb
class User < ApplicationRecord
    has_many subscriptions, dependent: :destroy, as: :imageable
end

.

#app/models/company.rb
class Company < ApplicationRecord
    has_many :subscriptions, dependent: :destroy, as: :imageable
end

.

#app/models/subscription.rb
class Subscription < ApplicationRecord
    belongs_to :imageable, polymorphic: true
end

and here is what I have in my Company controller:

def company_params
  params.require(:company).permit(:full_name, :subscriptions_attributes => {})
end

When I try to submit the form with a nested subscription selected, this is the error that I get:

Processing by ComaniesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"daP0snbHyLNm4KlzoO3YnkBrU/pD6ksUuOFp4icB74h0Uf929UT+4TAMxbuTBCwu5w+HWH3zD0gP3TwXcAmrHQ==", "company"=>{"full_name"=>"Test", "subscriptions_attributes"=>{"0"=>{"name"=>"", "start_date"=>"", "stop_date"=>""}}}}
  User Load (0.5ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
   (0.4ms)  BEGIN
Completed 500 Internal Server Error in 89ms (ActiveRecord: 6.4ms)

but if I go to the rails console, I can successfully type Company.subscriptions.create(name: "Random")

What am I doing wrong? It was working fine prior to me implementing polymorphic associations, but now I can't figure this out.

EDIT

Upon further investigation, it seems that the reason it is failing is because the Company's subscription does not contain an integer for imageable_id

In my Company controller, I have the following:

  # GET /companies/new
  def new
    @company = Company.new
    @company.subscriptions.build
  end

and when I inspect @company.subscription, I can see the imageable_type is already preset to "Company", but imageable_id is nil and is never filled out.

Where exactly is this supposed to be filled out?

EDIT

Here's the form:

<%= form_with(model: @company, local: true) do |form| %>
...
    <tbody>
        <%= form.fields_for :subscriptions do |subscription| %>
        <tr>
            <td><%= subscription.text_field :name, :subscription_name, class: "form-control" %></td>
            <td><%= subscription.date_field :start_date, as: :date, value: subscription.object.try(:strftime,"%m/%d/%Y"), class: 'form-control' %></td>
            <td><%= subscription.date_field :stop_date, as: :date, value: subscription.object.try(:strftime,"%m/%d/%Y"), class: 'form-control' %></td>
            <td><%= link_to "<i class='fas fa-trash'></i>".html_safe, '#', class: "btn btn-xs btn-danger delete_row" %></td>
        </tr>
        <% end %>
    </tbody>
...
<% end %>

Wow. After countless hours, I just simply needed to make the association optional:

belongs_to :imageable, polymorphic: true, optional: true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM