【发布时间】: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