0
votes

In my app I have lots of model: (products belongs_to creator, creators belongs_to compagnie, compagnies belongs_to SEO, SEOs belongs_to user)

in my first Seoscontroller I have that:

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

(Controller work find!)

In the Compagniescontroller I think (but not work:[ )

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

and in my simple_form....

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

I search a solution for associate my model id to the controller and the simple_form_for

(my rails error: RuntimeError at /compagnies/new Association :seo_id not found)

EDIT

  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

User model

class User < ApplicationRecord

  has_many  :seos
  has_many  :compagnies
end

Seo model

class Seo < ApplicationRecord

    belongs_to  :user
    has_many    :compagnies

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

compagnie model

class Compagnie < ApplicationRecord

     belongs_to :user
     belongs_to :seo


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

form for Seo

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

form for compagnie

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

Thanks

1
You should be having seo_id in compagnies table. - Pavan
yes I have an seo_id in my compagnie table - Boboss
Thaks for your time, I have add the two first table up - Boboss
Can you post your models? - Pavan
Ok I add the models - Boboss

1 Answers

0
votes

I think it should raise an syntax error at this line

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

but somehow it does not. If you want to pass the seo.id to the form url, you have to put it in the :url param. Rails FormHelper uses polymorphic_url to generate form action url. Take a look at it's document and you will find why your code doesn't work. In your case, if your routes file is something like:

resources :compagnies

then the form should be

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

If your routes is something like this (recommended)

resources :seos do
  resources :compagnies
end

then the form should be

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

Hope this help.