公司 ID 未通过 simple_form_for

company ID not passing through simple_form_for

我已经被这个关系问题困扰了一段时间,我想知道是否有人能发现我哪里出错了。

我有用户-公司-介绍关系(类似于患者-医生-预约关系)。

我已将其设置为 simple_form_for CompanyIntroduction 显示为弹出模式,通过部分呈现。

问题 是当我转到 submit/save 表单时,出现错误

ActiveRecord::RecordNotFound in User::CompanyIntroductionsController#create

Couldn't find Company with 'id'=

>@introduction.company = Company.find(params[:company_id])

为什么我的 company_id 没有通过表单?

迁移

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

User.rb

class User < ApplicationRecord
 has_many :company_introductions, dependent: :destroy
 has_many :companies, through: :company_introductions
end

Company.rb

class Company < ApplicationRecord
 has_many :company_introductions
 has_many :users, through: :company_introductions
 accepts_nested_attributes_for :company_introductions   
end 

CompanyIntroduction.rb

class CompanyIntroduction < ApplicationRecord
 belongs_to :user
 belongs_to :company 
end

User/CompanyIntroductionController

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">&times;</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 %>
        

由于您将实体传递给您的部分,您可以在表单中为 compnay_id 添加一个隐藏字段并将其值分配给实体 ID。我对部分的更改如下:

<%= 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 %>

在控制器中,将 :company_id 添加到允许的参数列表中,并在创建方法中注释掉这一行。

#@introduction.company = Company.find(params[:company_id])

希望对您有所帮助。