Search code examples
ruby-on-railsform-fornested-form-for

Rails: One_to_one relationship and form_for sending format instead of ID and params


I'm pretty new to rails and I'm having a bit of a though time to register my employeur.

Here are my routes:

  resources :users do
  resource :prestataire
  resource :employeur
  end

I have a has_one relationship between my employeur and user resources:

class User < ActiveRecord::Base
  has_one :prestataire
  has_one :employeur
  has_secure_password
end

class Employeur < ActiveRecord::Base
  belongs_to :user
  validates :siren, :societe, :code_postal, presence: true
end

And here's where I think the issue is:

<%= form_for [@user,@employeur], url: user_employeur_path(@user) do |f| %>
  <% if @employeur.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@employeur.errors.count, "error") %> prohibited this employeur from being saved:</h2>

      <ul>
      <% @employeur.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :siren, 'Siren: ' %><br>
    <%= f.text_field :siren %>
  </div>
.
.
.
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

When I fill in this issue, I'm redirected to /users/193/employeur.84 and I get this error: Couldn't find Employeur without an ID

Those are the only two params that are send after the form, even though I've indicated :siren, :societe, :code_postal {"user_id"=>"193", "format"=>"84"}

I guess that this might also come from my Emmployeur controller:

class EmployeursController < ApplicationController
  before_filter :set_user

  def index
    @employeurs = @user.employeur.all
  end


  def show
    @employeur = Employeur.find(params[:id]) #This is where the error happens since no id is sent.
  end

  def new
    @employeur = @user.build_employeur
  end

  def edit
    @employeur = Employeur.find(params[:id])
  end


  def create
    @employeur = @user.build_employeur(employeur_params)
    respond_to do |format|
      if @employeur.save
        format.html { redirect_to [@user, @employeur], notice: 'Employeur was successfully created.' }
      else
        format.html { render action: 'new' }
        format.json { render json: @employeur.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
  @employeur = Employeur.find(params[:id])
    respond_to do |format|
      if @employeur.update_attributes(employeur_params)
        format.html { redirect_to [@user, @employeur], notice: 'Employeur was successfully created.' }
        format.json { render action: 'show', status: :created, location: @employeur }
      else
        format.html { render action: 'new' }
        format.json { render json: @employeur.errors, status: :unprocessable_entity }
      end
    end
  end


def destroy
  @employeur = Employeur.find(params[:id])
  @employeur.destroy
  respond_to do |format|
    format.html { redirect_to @user }
    format.json { head :no_content }
  end
end

private
  def set_user
    @user = User.find(params[:user_id])
  end

  def employeur_params
    params.require(:employeur).permit(:siren, :societe, :code_postal)
  end

end 

Any help would be more then welcome.


In order to give an example of singular and nested resource working, I'll add a little more of my code:

class Employeur < ActiveRecord::Base
  model_name.instance_variable_set(:@route_key, 'employeur')
  belongs_to :user
  has_many :projets, as: :projetable
  has_many :prestataires, through: :projets
  has_many :offres, through: :projets
  has_many :feedbacks, through: :projets
  validates :siren, :societe, :code_postal, presence: true, uniqueness: true
  validates :code_postal, presence: true
  validates_associated :user
end

Here's mu User controller that leads me from the user form to the employeur once filled:

class UsersController < ApplicationController

#TODO index user doit être suprimé quand inutile pour dev
  def index
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
    @user = User.find(params[:id])
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        if params[:commit] == 'Employeur'
        format.html { redirect_to new_user_employeur_path(user_id: @user), notice: "Renseignez vos informations d'employeur" }
        format.json { render action: 'show', status: :created, location: @user }
        else 
        format.html { redirect_to new_user_prestataire_path(user_id: @user), notice: "Renseignez vos informations de prestataire" }
        format.json { render action: 'show', status: :created, location: @user }
        end
      else
        format.html { render action: 'new' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    @user = User.find(params[:id])
    respond_to do |format|
      if @user.update(user_params)
        if params[:commit] == 'Prestataire'
        format.html { redirect_to new_user_prestataire_path(user_id: @user), notice: 'User was successfully updated.' }
        format.json { head :no_content }
        else 
        format.html { redirect_to new_user_employeur_path(user_id: @user), notice: "User was successfully updated." }
        format.json { head :no_content }
        end
      else
        format.html { render action: 'edit' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user = User.find(params[:id])
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url }
      format.json { head :no_content }
    end
  end

private
  def user_params
    params.require(:user).permit(:email, :password, :password_confirmation, :surname, :forename, :civility, :phone)
  end

end

And finally, my User form:

<%= form_for(@user) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :civility, 'Titre de civilité: ' %><br>
    <%= f.text_field :civility %>
  </div>

  <div class="field">
    <%= f.label :forename, 'Prénom: ' %><br>
    <%= f.text_field :forename %>
  </div>
  <div class="field">
    <%= f.label :surname, 'Nom de famille: ' %><br>
    <%= f.text_field :surname %>
  </div>
  <div class="field">
    <%= f.label :email, 'Email: ' %><br>
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password, 'Mot de passe: ' %><br>
    <%= f.password_field :password, size: 40 %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation, 'Confirmation de mot de passe: ' %><br>
    <%= f.password_field :password_confirmation, size: 40 %>
  </div>
  <div class="field">
    <%= f.label :phone, 'Numéro de téléphone: ' %><br>
    <%= f.text_field :phone %>
  </div>
  <div class="actions">
    <%= f.submit "Employeur" %>
    <%= f.submit "Prestataire" %>
  </div>
<% end %>

Hope this will help someone as much as it would have helped me. Cheers !


Solution

  • You don't have your EmployeursController setup correctly. Since employeur is a singular resource, you cannot reference it by id. Instead you need to change your show action to this:

    def show
      @employeur = User.find(params[:user_id]).employeur
    end
    

    The reason is that user_employeur_path(@user) creates a path like /users/193/employeur where you can access the user id in the controller via params[:user_id]

    Also, since employeur is a singular resource, there is no index action defined for it. You can remove the index action entirely from your controller.