Rails simple_form object_id 协会

Rails simple_form object_id association

在我的应用程序中,我有很多模型: (产品 belongs_to 创作者、创作者 belongs_to 公司、公司 belongs_to SEO、SEO belongs_to 用户)

在我的第一个 Seoscontroller 中,我有:

def create
 @seo = current_user.seos.build(seo_params)
   if @seo.save
    redirect_to search_path
   else
    render :new
   end
end

(找到控制器工作!)

我认为在 Compagniescontroller 中(但不起作用:[ )

def create
  seo = Seo.find(params[:seo_id])
   @compagnie = seo.compagnies.build(compagnie_params)
  if @compagnie.save
   redirect_to search_path
  else
   render :new
  end
end

在我的 simple_form.....

<%= simple_form_for @compagnie(seo_id: seo.id) do |f| %>
<% end %>

我搜索了一个解决方案,将我的模型 ID 关联到控制器和 simple_form_for

(我的 rails 错误:/compagnies/new 处的运行时错误 关联:seo_id 未找到)

编辑

  create_table "seos", force: :cascade do |t|
   t.string   "name"
   t.string   "surname"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
   t.integer  "user_id"
   t.index ["user_id"], name: "index_seos_on_user_id", using: :btree
  end

  create_table "compagnies", force: :cascade do |t|
    t.string   "compagnie_name"
    t.string   "address"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "seo_id"
    t.integer  "user_id"
    t.index ["seo_id"], name: "index_compagnies_on_seo_id", using: :btree
    t.index ["user_id"], name: "index_compagnies_on_user_id", using: :btree
  end

用户模型

class User < ApplicationRecord

  has_many  :seos
  has_many  :compagnies
end

SEO模型

class Seo < ApplicationRecord

    belongs_to  :user
    has_many    :compagnies

    validates :name,        presence: true
    validates :surname,      presence: true
end

公司型号

class Compagnie < ApplicationRecord

     belongs_to :user
     belongs_to :seo


    validates :address,                 presence: true
    validates :compagnie_name,          presence: true
end

搜索引擎优化表格

            <%= simple_form_for @seo do |f| %>
        <div class="search__form--left split-panel--left">
            <div class="search__title">
                Seo:
            </div>


                <div class="search__section search__section--od">

                    <!--Name-->
                    <div class="search__field search__departure grouped-input--top">
                        <%= f.input :name, placeholder: "name", :input_html => { :class => "add__name-input ember-text-field textfield station-text-field empty station-text-field-- add__input empty" }, label: false %>
                    </div>

                    <!--Surname-->
                    <div class="search__field search__arrival grouped-input--bottom">
                        <%= f.input :surname, placeholder: "prenom", :input_html => { :class => "add__name-input ember-text-field textfield station-text-field empty station-text-field-- search__input empty" }, label: false %>
                    </div>
                </div>

                <!--Button-->
                <div class="search__button progress-button">
                    <%= f.button :submit, "Add", class: "button progress-button--button" %>
                </div> 

        </div>
    <% end %>

公司表格

    <%= simple_form_for @compagnie(seo_id: seo.id) do |f| %>
        <div class="search__form--left split-panel--left">
            <div class="search__title">
                Seo:
            </div>


                <div class="search__section search__section--od">

                    <!--Name-->
                    <div class="search__field search__departure grouped-input--top">
                        <%= f.input :address, placeholder: "address", :input_html => { :class => "add__name-input ember-text-field textfield station-text-field empty station-text-field-- add__input empty" }, label: false %>
                    </div>

                    <!--Surname-->
                    <div class="search__field search__arrival grouped-input--bottom">
                        <%= f.input :compagnie_name, placeholder: "compagnie_name", :input_html => { :class => "add__name-input ember-text-field textfield station-text-field empty station-text-field-- search__input empty" }, label: false %>
                    </div>
                </div>

                <!--Button-->
                <div class="search__button progress-button">
                    <%= f.button :submit, "Add", class: "button progress-button--button" %>
                </div> 

        </div>
    <% end %>

谢谢

我认为它应该在这一行引发语法错误

<%= simple_form_for @compagnie(seo_id: seo.id) do |f| %> 

但不知何故它没有。如果要将 seo.id 传递给 url 形式,则必须将其放在 :url 参数中。 Rails FormHelper 使用 polymorphic_url 生成表单操作 url。看看它的文档,您就会发现为什么您的代码不起作用。在您的情况下,如果您的路由文件类似于:

resources :compagnies

那么表格应该是

<%= simple_form_for @compagnie, url: compagnies_path(seo_id: seo.id) do |f| %>

如果你的路线是这样的(推荐)

resources :seos do
  resources :compagnies
end

那么表格应该是

<%= simple_form_for [@seo, @compagnie] do |f| %>

希望对您有所帮助。