保存belongs_to问题的Rails 4嵌套表单
问题描述:
我正在研究一个新的Rails 4应用程序,并且很难让所有部分正确协同工作。我有一个与另一个模型(lat_long)belongs_to关联的模型(样本),并试图从样本视图(编辑默认生成的视图)对两者执行CRUD操作。问题是lat_long可能是空白的;一切正常,如果它不是,但当它在模型中的验证,所以我认为它试图保存一个空白的lat_long而不是将其设置为零。更新和编辑是问题。保存belongs_to问题的Rails 4嵌套表单
下面是从代码中的相关部分:
型号:
<%= form_for(@sample) do |f| %>
<%= f.fields_for :lat_long do |g| %>
<div class="field">
<%= g.label :north_latitude %><br>
<%= g.text_field :north_latitude %>
</div>
<div class="field">
<%= g.label :west_longitude %><br>
<%= g.text_field :west_longitude %>
</div>
<% end %>
<% end %>
samples_controller.rb:
class Sample < ActiveRecord::Base
belongs_to :lat_long
accepts_nested_attributes_for :lat_long
end
class LatLong < ActiveRecord::Base
validates_each :west_longitude do |record, attr, value|
record.errors.add attr, "must be within range." if (value < 110.0 or value > 115.0)
end
validates_each :north_latitude do |record, attr, value|
record.errors.add attr, "must be within range." if (value < 38.0 or value > 42.0)
end
end
样品/ _form.html.erb在编辑和使用新
class SamplesController < ApplicationController
# GET /samples/new
def new
@sample = Sample.new
@sample.build_lat_long
@sample.build_trap
end
# GET /samples/1/edit
def edit
@sample.lat_long || @sample.build_lat_long
@sample.trap || @sample.build_trap
end
# POST /samples
# POST /samples.json
def create
@sample = Sample.new(sample_params)
puts sample_params
respond_to do |format|
if @sample.save
format.html { redirect_to @sample, notice: 'Sample was successfully created.' }
format.json { render :show, status: :created, location: @sample }
else
format.html { render :new }
format.json { render json: @sample.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /samples/1
# PATCH/PUT /samples/1.json
def update
respond_to do |format|
if @sample.update(sample_params)
format.html { redirect_to @sample, notice: 'Sample was successfully updated.' }
format.json { render :show, status: :ok, location: @sample }
else
format.html { render :edit }
format.json { render json: @sample.errors, status: :unprocessable_entity }
end
end
end
def sample_params
params.require(:sample).permit(...,
lat_long_attributes: [:id,
:west_longitude, :north_latitude])
end
end
我可以在更新之前检查控制器当然,但这是正确的做法吗?它“觉得”应该有一种让Rails处理这个问题的方法,但是看到大量的*问题和文档没有给出任何想法。大多数例子使用has_one或has_many,所以我不知道这是否是问题?根据this question,belongs_to与accep_nested_attributes_for一起工作很新颖,所以也许这只是简单的没有为此设置。
我想知道什么是最好的做法是这种情况下,最“Railsy”的事情。哦,我想我已经包含了所有相关的代码,但是如果还有其他东西需要让我知道。
答
我会做这样的事情:
class Sample < ActiveRecord::Base
belongs_to :lat_long
accepts_nested_attributes_for :lat_long, reject_if: proc { |attributes| attributes['west_longitude'].blank? && attributes['north_latitude'].blank? }
end
class LatLong < ActiveRecord::Base
validates :west_longitude, numericality: { greater_than_or_equal_to: 110,
less_than_or_equal_to: 115,
message: "must be within range" },
allow_nil: true
validates :north_latitude, numericality: { greater_than_or_equal_to: 38,
less_than_or_equal_to: 42,
message: "must be within range" },
allow_nil: true
end
这将导致一个空白lat_long(除ID,的updated_at,created_at空)被保存。我希望sample.lat_long本身为零(因此数据库中的sample.lat_long_id为NULL)。 – Maltiriel
@Maltiriel刚刚修改了答案,看看是否有帮助。我想你也可以去除验证的':allow_nil => true'选项,因为没有空值将以这种方式到达模型。 –
是的,我想像你的答案的第二部分可以工作,但我不知道它是否是最好的设计。但感谢您的答案。至于你对剥离的评论:allow_nil => true,我不知道你在那里做什么。如果该行不在或者如果是:allow_nil => false,那么视图需要这些值。他们被允许是空白的,我只是不希望空白对象被保存到数据库。 – Maltiriel