我现在因为这种关系问题而被困了一段时间,我想知道是否有人可以发现我出错的地方。
我有一个用户 - 公司介绍关系(类似于患者 - 医生 - 约会关系)。
我进行了设置,以便simple_form_for CompanyIntroduction显示为弹出模式,通过部分渲染。
问题是当我提交/保存表单时,我收到错误
User :: CompanyIntroductionsController #create中的ActiveRecord :: RecordNotFound
无法找到公司 的 'id'=
>@introduction.company = Company.find(params[:company_id])
为什么heck不是我的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
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 %>
答案 0 :(得分:0)
由于您将实体传递给partial,因此您可以在表单中为compnay_id添加隐藏字段,并将其值分配给实体ID。我对partial的更改如下:
<%= 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添加到允许的参数列表中,并在create方法中注释掉该行。
#@introduction.company = Company.find(params[:company_id])
希望有所帮助。