rails nested_attributes表单找不到id。

问题描述 投票:0回答:1

所以我有两个模型:Internship和Review。我希望Review是一个嵌套的Internship_attributs。这样我就可以创建一个带有Review的实习生。

我的问题是Review表单集成在新的表单internship中,找不到实习生的id。它提出错误'Review internship must exist'。

实习生.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

评论.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

实习中的新形式新

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

所以,这是我在实习控制器中尝试过的内容。

 @intership.reviews.review_internship_id = @internship.id

这样就可以找到实习生的id。错误是 "评论实习生必须存在"。

在 "评论用户 "上也是这样的,解决的方法是 @internship.reviews.first.review_user_id = current_user.id

你知道问题出在哪里吗,我怎么能用别的方法找到实习生的id。我也试过params[:id]。

谢谢你的帮助

:

ruby-on-rails nested-attributes
1个回答
1
投票

你真的不需要在Review上使用user_id外键,因为它可以通过面试获得。

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

而且你绝对不需要为嵌套的记录手动分配父ID

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
© www.soinside.com 2019 - 2024. All rights reserved.