Lisa El Yacoubi
Lisa El Yacoubi

Reputation: 11

rails nested_attributes form can't find id

So i have two models : Internship and Review. I want the Review to be a nested_attributs of Internship. So that i can create an internship with a review.

My problem is that the review form integrated in the new form internship, doesn't find the id of the internship. It raise the errors 'Review internship must exist'

Internship.rb

  has_many :reviews, foreign_key: "review_internship_id"
  has_many :review_users, foreign_key: 'review_user_id', class_name:"User", through: :reviews

  accepts_nested_attributes_for :reviews, allow_destroy: true
  validates_associated :reviews

Review.rb

     belongs_to :review_user, class_name: "User"
     belongs_to :review_internship, class_name: "Internship"

Internship_controller

  def new
    @internship = Internship.new
    @internship.reviews.new
  end

def create
    @internship = Internship.new(internship_params)
    @internship.user = current_user
    @internship.reviews.first.review_user_id = current_user.id

    respond_to do |format|
      if @internship.save
        format.html { redirect_to @internship, notice: 'Expérience crée avec succès' }
        format.json { render :show, status: :created, location: @internship }
      else
        format.html { render :new }
        format.json { render json: @internship.errors, status: :unprocessable_entity }
      end
    end
  end

private

 def internship_params
      params.require(:internship).permit(:adress, :zipcode, :city, :specialty, :organization, :population, :cursus, :title, :duration, :description, :region, :remuneration, :user_id, reviews_attributes: [:title, :notation, :description, review_internship_id: params[:internship_id], review_user_id: current_user.id])
    end

The new form in internship new

<%= form_with(model: internship, local: true) do |form| %>
....
<!--NESTED ATTRIBUTS REVIEW-->
      <%= form.fields_for :reviews do |p| %>

                  <%= p.label :titre %>
                  <%= p.text_field :title, class:"form-control" %>

                  <%= p.label :note %>
                  <%= p.number_field :notation, min: 1, max: 5, class:"form-control" %>

                  <%= p.label :description %>
                  <%= p.text_area :description, class:"form-control" %>

     <% end %>

   ...
 <% end %>

So this is what i've tried in Internship controller

 @intership.reviews.review_internship_id = @internship.id

So that it can find the id of the internship. The error is "Review Internship must exist".

It did the same with "Review User", which was solved with @internship.reviews.first.review_user_id = current_user.id

Do you have any idea where the problem is, and how i can find the internship id with another way. I've also tried the params[:id]

Thanks for your help

:

Upvotes: 0

Views: 544

Answers (1)

max
max

Reputation: 101811

You don't really need a user_id foreign key on reviews since it can get there through the interview:

class Review
  belongs_to :internship
  has_one :user, through: :interview
end

class Internship
  belongs_to :user
  has_many :reviews
end

class User
  has_many :internships
  has_many :reviews, through: :internships
end

And you definitely don't ever need to be manually assigning parent ids for nested records.

class IntershipsController < ApplicationController

  def new
    @internship = Internship.new
    @internship.reviews.new
  end

  def create
    @internship = Internship.new(internship_params) do |i|
      i.user = current_user
    end

    respond_to do |format|
      if @internship.save
        format.html { redirect_to @internship, notice: 'Expérience crée avec succès' }
        format.json { render :show, status: :created, location: @internship }
      else
        format.html { render :new }
        format.json { render json: @internship.errors, status: :unprocessable_entity }
      end
    end
  end

  private

  def internship_params
    #  use some line-breaks!
    params.require(:internship)
          .permit(
            :adress, :zipcode, :city, :specialty, 
            :organization, :population, :cursus, 
            :title, :duration, :description, 
            :region, :remuneration, :user_id, 
            reviews_attributes: [
              :id, :title, :notation, :description
            ]
          )
  end
end

Upvotes: 1

Related Questions