【问题标题】:Form associations: Having trouble with controller saving information to 2 models simultaneously表单关联:控制器同时将信息保存到 2 个模型时遇到问题
【发布时间】:2019-01-02 16:29:39
【问题描述】:

对于我的项目,我正在尝试构建一个仪表板,让代理可以查看用户发布的提交并为每个提交添加状态和注释,以便记录他们自己的个人活动,即他们不会更改实际记录,只是留下私人笔记反对它。为此,我创建了一个包含 Agent id 和 Submission id 以及 Status 和 Notes 列的连接表。

我设法创建了一个索引视图,该视图显示提交数据,在我的联接表的每一行末尾有 2 个表单字段,称为状态和注释...问题是当我更新这些字段时他们没有得到保存到我的关节。

索引视图上的表单

  <%= form_with(model: submission, local: true) do |form| %>
      <% form.fields_for :agent_activities do |act| %>

      <td> <div class="field">

        <%= act.text_field :Status %>

      </div>
    </td>

    <td> <div class="field">

      <%= act.text_field :Notes %>
    </div>
  </td>
  <td>
    <div class="actions">
    <%= form.submit %>
  </div>
</td>

    <% end %>
  <% end %>

rb 文件中的模型关联

class Submission < ApplicationRecord
  belongs_to :user, :optional => true

  belongs_to :location, :optional => true

  has_many :agent_activities

end


class AgentActivity < ApplicationRecord

  belongs_to :submission, :optional => true #has submission_id 
  foreign key in table

  belongs_to :agent, :optional => true #has agent_id foreign key in 
  table


end

控制器:

 class SubmissionsController < ApplicationController
 before_action :set_submission, only: [:show, :edit, :update, :destroy]

def index
@submissions = Submission.where(:user_id => current_user.id)

end


def show
end


def new
@submission = Submission.new
end


def edit
end

# POST /submissions
# POST /submissions.json
def create
@submission = Submission.new(submission_params.merge(user_id: current_user.id))

respond_to do |format|
  if @submission.save
    # Tell the UserMailer to send a welcome email after save
    NewSubmissionMailer.submission_email(@submission).deliver_now
    NewSubmissionMailer.matching_agents_email(@submission).deliver_now

    format.html { redirect_to @submission, notice: 'Submission was successfully created.' }
    format.json { render :show, status: :created, location: @submission }
  else
    format.html { render :new }
    format.json { render json: @submission.errors, status: :unprocessable_entity }
  end
  end
 end

 # PATCH/PUT /submissions/1
 # PATCH/PUT /submissions/1.json
  def update
  respond_to do |format|
  if @submission.update(submission_params)
    format.html { redirect_to @submission, notice: 'Submission was successfully updated.' }
    format.json { render :show, status: :ok, location: @submission }
  else
    format.html { render :edit }
    format.json { render json: @submission.errors, status: :unprocessable_entity }
   end
  end

 end

# DELETE /submissions/1
# DELETE /submissions/1.json
def destroy
@submission.destroy
respond_to do |format|
  format.html { redirect_to submissions_url, notice: 'Submission was successfully destroyed.' }
  format.json { head :no_content }
  end
end

 private
  # Use callbacks to share common setup or constraints between actions.
  def set_submission
  @submission = Submission.find(params[:id])
  end

# Never trust parameters from the scary internet, only allow the white list through.
def submission_params
  params.require(:submission).permit(:First_Name, :Last_Name, :Phone, :Email, :Desired_Location, :number_of_beds, :number_of_occupants, :Rent_price_per_month_gbp, :Max_move_in_date, :Tenant_Occupation, :Contact_me_on, :Furnished, :Current_Address, :Property_Requirements)
end
end

不确定我在这里缺少什么:/

更新基于@TOM 的回答

新的控制器参数:

  def submission_params
  params.require(:submission).permit(:First_Name, :Last_Name, :Phone, :Email, :Desired_Location, :number_of_beds, :number_of_occupants, :Rent_price_per_month_gbp, :Max_move_in_date, :Tenant_Occupation, :Contact_me_on, :Furnished, :Current_Address, :Property_Requirements, agent_activities_attributes: [:id, :Status, :Notes, :_destroy])
end

结束

新提交模型rb:

class Submission < ApplicationRecord
 belongs_to :user, :optional => true

 belongs_to :location, :optional => true

 has_many :agent_activities

 accepts_nested_attributes_for :agent_activities

end

Index.html.erb

  <%= form_with(model: submission, local: true) do |form| %>
  <% form.fields_for :agent_activities, @submission.agent_activities.build do |act| %>
      <td> <div class="field">

        <%= act.text_field :Status %>

      </div>
    </td>

    <td> <div class="field">

      <%= act.text_field :Notes %>
    </div>
  </td>
  <td>
    <div class="actions">
    <%= form.submit %>
  </div>
</td>

    <% end %>

【问题讨论】:

  • 你能展示一下你的控制器的其余部分吗?特别是您的强大参数?您是否在允许的参数中添加了新的 status 和 notes 作为键?
  • 现在更新了,不,我没有包括状态和注释包括参数,因为我认为我不会更新提交模型中的任何列?

标签: ruby-on-rails ruby forms html-table associations


【解决方案1】:
  1. 在您的 Submission 模型上添加:accepts_nested_attributes_for :agent_activities (accepts_nested_attributes_for documentation) 这将使 Rails 知道您的表单将为关联模型提供字段。

  2. 添加后,Rails 将在您的强参数中提供参数agent_activities_attributes 中的键,我们可以添加:.permit(..., agent_activities_attributes: [:id, :Status, :Notes, :_destroy]。仅当您计划在嵌套属性调用中使用 allow_destroy: true 时,才需要 :_destroy 键。

附注:大写名称(Status、Notes 等)通常为 Ruby 中的常量保留。您可能需要考虑将属性列名称更改为小写。

【讨论】:

  • 感谢@Tom 非常有帮助,尽管我的索引视图上的表单字段现在似乎已经消失了?
  • 尝试&lt;% form.fields_for :agent_activities, @submission.agent_activities.build do |act| %&gt; 如果没有预先存在的agent_activities,这将提供一个记录对象。或者尝试form_with(model: @submission.. 使用实例变量而不是符号引用。
  • 所以第二个选项似乎有效,但是,我的按钮已从更新更改为保存,现在它要求我保存到完全不同的模型(称为房地产经纪人),因为它不起作用,因为我还没有为其添加所需的参数。
  • 地产代理??这可能是某个地方的错字吗?
  • 它似乎来自我使用实例变量而不是表单中的符号引用
猜你喜欢
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
  • 2020-11-14
  • 2023-03-18
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多