代码之家  ›  专栏  ›  技术社区  ›  Brian O Halloran

公司ID未通过simple\u form\u for

  •  0
  • Brian O Halloran  · 技术社区  · 6 年前

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

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

    我对它进行了设置,以便simple\u form\u for CompanyIntroduction显示为弹出模式,并通过部分呈现。

    问题所在 当我去提交/保存表单时,我得到了错误

    ActiveRecord::RecordNotFound in User::CompanyIntroductionController#create

    找不到与的公司 'id'=

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

    为什么我的公司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
    

    使用者rb型

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

    公司rb型

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

    公司简介。rb型

    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">&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 %>
            
    
    1 回复  |  直到 4 年前
        1
  •  0
  •   alwesam    6 年前

    由于要将实体传递给partial,因此可以在表单中为compnay\u id添加一个隐藏字段,并将其值分配给entity 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\u id添加到允许的参数列表中,并在create方法中注释掉这一行。

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

    希望这有帮助。