所以我在这方面已经有一段时间了,并且尝试了很多不同的方法。本质上,我有一个包罗万象的 property
模型和一个关联的 deed
模型。从 properties#show view
我让它显示所有相关的 deeds
。我的 property view
中有一个按钮,它显示一个模式(使用 AJAX)以创建与所述模型关联的 deed
。所有步骤都有效(甚至在属性 View 中使用 AJAX 进行“编辑”和“更新”),但在尝试保存 new deed
时我收到一条错误消息:
This form contains 1 error. Property must exist
我试过以不同的方式传递 @properties.id
,但我似乎无法得到它。我将列出我拥有的所有相关信息,如果需要更多信息,请告诉我。
属性模型
class Property < ApplicationRecord
has_many :deeds, dependent: :destroy
accepts_nested_attributes_for :deeds
end
契约模型
class Deed < ApplicationRecord
belongs_to :property
accepts_nested_attributes_for :property
end
契约 Controller (包括编辑和更新操作)
def new
@deeds = Deed.new
@deeds.build_property
respond_to do |format|
format.html { render 'new'}
format.js
end
end
def create
@properties = Property.find(params[:deed][:property_id])
@deeds = @properties.deeds.new(deed_params)
if @deeds.save
flash[:success] = "Deed created successfully!"
respond_to do |format|
format.html { redirect_to deeds_path }
format.js
end
else
render 'new'
end
end
def edit
@deeds = Deed.find(params[:id])
respond_to do |format|
format.html { @deeds.save }
format.js
end
end
def update
@deeds = Deed.find(params[:id])
@properties = Property.find(@deeds.property.id)
@deeds.update_attributes(deed_params)
respond_to do |format|
format.html { redirect_to deeds_path }
format.js
end
end
private
def deed_params
params.require(:deed).permit(:property_id, :deed_number, :deed_context, :consideration, :recorded_date, :grantor, :grantee, :trustee)
end
new deeds
链接来自 properties#show
view
<%= link_to fa_icon("plus", text: "Add Deed"), new_property_deed_path(@properties.id), remote: true, class: "btn btn-primary btn-large btn-ouline pull-right", locals: { property_id: @properties } %>
new.js.erb
$('#deeds-modal').modal("show");
$('#deeds-modal').html('<%= j render partial: "deeds/deeds_modal", locals: { property_id: @properties } %>');
_deeds_modal.html.erb partial
表格
<div class="modal-body">
<%= bootstrap_form_for(@deeds, layout: :horizontal, remote: true) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.hidden_field :property_id, value: property_id %>
<%= f.text_field :deed_number, class: 'form-control' %>
<%= f.text_field :deed_context, class: 'form-control' %>
<%= f.text_field :consideration, class: 'form-control' %>
<%= f.text_field :recorded_date, class: 'form-control' %>
<%= f.text_field :grantor, class: 'form-control' %>
<%= f.text_field :grantee, class: 'form-control' %>
<%= f.text_field :trustee, class: 'form-control' %>
<%= f.form_group do %>
<%= f.submit "Save", class: "btn btn-primary" %>
<% end %>
<% end %>
</div>
服务器响应 deeds_modal 表单
POST
Started GET "/properties/99/deeds/new" for 127.0.0.1 at 2017-05-30
23:24:48 -0400
Processing by DeedsController#new as JS
Parameters: {"property_id"=>"99"}
Rendering deeds/new.js.erb
Rendered shared/_error_messages.html.erb (0.4ms)
Rendered deeds/_deeds_modal.html.erb (5.6ms)
Rendered deeds/new.js.erb (7.3ms)
Completed 200 OK in 14ms (Views: 10.6ms | ActiveRecord: 0.0ms)
Started POST "/deeds" for 127.0.0.1 at 2017-05-30 23:24:55 -0400
Processing by DeedsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"eaj9K...==", "deed"=>{"property_id"=>"", "deed_number"=>"69696969", "deed_context"=>"Bagel slaps", "consideration"=>"Considered", "recorded_date"=>"2017-05-31", "grantor"=>"", "grantee"=>"NipLips", "trustee"=>""}, "commit"=>"Save"}
(0.1ms) BEGIN
(0.1ms) ROLLBACK
Rendering deeds/new.js.erb
Rendered shared/_error_messages.html.erb (0.7ms)
Rendered deeds/_deeds_modal.html.erb (12.1ms)
Rendered deeds/new.js.erb (20.4ms)
Completed 200 OK in 32ms (Views: 30.6ms | ActiveRecord: 0.2ms)
如您所见,我尝试从 new link
-> new.js.erb
-> deeds modal 传递局部变量form
但我似乎无法将 property_id
拉入 new deeds 对象
。
对于我正在尝试做的事情,这是否是正确的方法,或者我只是遗漏了什么?希望这不是一篇太长的文章,并且我提供了足够的信息……大声笑。
*在写这篇文章时,我点击了一些关于 SO 的“类似问题”,这是我得到 @properties = Property.find(params[:deed][:property_id])
行,但在提交时我仍然得到 ActiveRecord::RecordNotFound (Couldn't find Property with 'id'=):
。另外,感谢所有花时间阅读本文的人。
rails routes
更新(为简洁起见仅包含相关路线)
deeds GET /deeds(.:format) deeds#index
POST /deeds(.:format) deeds#create
new_deed GET /deeds/new(.:format) deeds#new
edit_deed GET /deeds/:id/edit(.:format) deeds#edit
deed GET /deeds/:id(.:format) deeds#show
PATCH /deeds/:id(.:format) deeds#update
PUT /deeds/:id(.:format) deeds#update
DELETE /deeds/:id(.:format) deeds#destroy
...
property_deeds GET /properties/:property_id/deeds(.:format) deeds#index
POST /properties/:property_id/deeds(.:format) deeds#create
new_property_deed GET /properties/:property_id/deeds/new(.:format) deeds#new
edit_property_deed GET /properties/:property_id/deeds/:id/edit(.:format) deeds#edit
property_deed GET /properties/:property_id/deeds/:id(.:format) deeds#show
PATCH /properties/:property_id/deeds/:id(.:format) deeds#update
PUT /properties/:property_id/deeds/:id(.:format) deeds#update
DELETE /properties/:property_id/deeds/:id(.:format) deeds#destroy
最佳答案
这应该是你的新
Action
def new
@deeds = Deed.new
@deeds.build_property
@properties = Property.find_by_id(params[:property_id]) # add this line
respond_to do |format|
format.html { render 'new'}
format.js
end
end
更新:
如果您没有在其他任何地方使用 associated
属性,则不需要传递 locals
并且我不建议 hidden_field
因为它可以很容易地修改,或者你可以做..
def new
@deeds = Property.find_by_id(params[:property_id]).deeds.new # add this line
@deeds.build_property
respond_to do |format|
format.html { render 'new'}
format.js
end
end
关于ruby-on-rails - 为关联模型创建新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44275417/