I've been stuck for a while now with this relationship problem and I was wondering if someone can spot where I'm going wrong.
I have a user-company-introductions relationship (similar to a patient-doctor-appointment relationship).
I have it setup so that the simple_form_for CompanyIntroduction appears as a popup Modal, rendered through a partial.
The problem is that when I go to submit/save the form, I get the error
ActiveRecord::RecordNotFound in User::CompanyIntroductionsController#create
Couldn't find Company with 'id'=
>@introduction.company = Company.find(params[:company_id])
Why the heck isn't my company_id passing through the form?
class CreateCompanyIntroductions < ActiveRecord::Migration[5.0]
def change
create_table :company_introductions do |t|
t.integer :status, null: false, default: 0
t.string :user_name
t.string :user_email
t.text :message
t.references :company, index: true
t.belongs_to :user, index: true
t.timestamps
end
end
end
class User < ApplicationRecord
has_many :company_introductions, dependent: :destroy
has_many :companies, through: :company_introductions
end
class Company < ApplicationRecord
has_many :company_introductions
has_many :users, through: :company_introductions
accepts_nested_attributes_for :company_introductions
end
class CompanyIntroduction < ApplicationRecord
belongs_to :user
belongs_to :company
end
class User::CompanyIntroductionsController < ApplicationController
def index
@introduction = CompanyIntroduction.all
end
def create
@introduction = CompanyIntroduction.create(intro_params)
@introduction.user = current_user
@introduction.company = Company.find(params[:company_id])
if @introduction.save
flash[:success] = "Introduction request sent"
else
flash[:error] = @introduction.errors.full_messages
end
redirect_back(fallback_location: user_companies_path)
end
def intro_params
params.require(:company_introduction).permit(:status, :username, :user_email, :message)
end
end
...
<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#createCompanyIntroduction" >
Create Introduction
</button>
<%= render partial: '/user/company_introductions/create_modal', locals:{entity: @company } %>
...
<%= simple_form_for [:user, CompanyIntroduction.new(company_id: entity.id)] do |f| %>
<div class="modal fade" id="createCompanyIntroduction" tabindex="-1" role="dialog" aria-labelledby="createCompanyIntroduction">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Request introduction with <%= entity.name %>, id: <%=entity.id%></h4>
</div>
<div class="modal-body">
<%= f.label :user_name, "Your name *"%>
<%= f.input :user_name, label: false, placeholder: "#{current_user.full_name}" %>
<%= f.label :user_email, "Email *" %>
<%= f.input :user_email, label: false, placeholder: "#{current_user.email}" %>
<%= f.label :message, "Why do you want to meet? *" %>
<%= f.text_area :message, class:"form-control", rows: 4, label: false, style: 'resize:vertical;', maxlength: '1500' %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<%= f.submit "Save", class: 'btn btn-primary' %>
</div>
</div>
</div>
</div>
<% end %>
Since you're passing the entity to your partial you can add a hidden field for compnay_id in the form and assign its value to entity id. My changes to the partial would be as follows:
<%= simple_form_for [:user, CompanyIntroduction.new] do |f| %>
<!--rest of code -->
<%=f.input :company_id, as: :hidden, input_html: {value: entity.id} %>
<!-- rest of code -->
<% end %>
In the controller, add :company_id to the list of permitted params and comment out this line in the create method.
#@introduction.company = Company.find(params[:company_id])
Hope that helps.