我有以下型号:
class SequenceResult < ActiveRecord::Base
has_many :ratings, dependent: :destroy
accepts_nested_attributes_for :ratings
end
class Rating < ActiveRecord::Base
belongs_to :sequence_result
belongs_to :rating_prototype
end
class RatingPrototype < ActiveRecord::Base
has_many :ratings
end
每个序列结果都可以有多个评级。评级结构由评级原型定义,例如原型给出了一个问题“你的评价是多少?”答案本身存储在Rating
类中。
因此,当我构建表单时,我已经使用原型填充ratings
:
def new_ratings
@sequence_result = SequenceResult.find(params[:sequence_result_id])
# create empty ratings
RatingPrototype.all.each do |rating_prototype|
@sequence_result.ratings.new(rating_prototype: rating_prototype)
end
end
然后我创建我的表单:
<%= form_for @sequence_result do |f| %>
<%= f.fields_for :ratings do |rating_form| %>
<%= rating_form.text_area :answer %>
<% end %>
<%= f.submit %>
<% end %>
现在,当我提交此内容时,我的参数如下:
"sequence_result" => {
"ratings_attributes" => {
"0" => { "answer" => "test" },
"1" => { "answer" => "2" }
}
},
"sequence_result_id" => "1"
如您所见,我不知道RatingPrototype
每个Rating
属于哪个RatingPrototype
。这意味着在我的控制器中,当从这些参数保存时,我手动必须再次将params[:sequence_result][:ratings_attributes].each_value do |rating_attr|
@sequence_result.ratings.new(rating_attr)
end
分配给这些新模型。
保存时是否有更简单的方法来保持关联,所以我需要做的就是:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
</configSections>
<appSettings>
<add key="log4net.Config" value="log4netConfig.xml" />
<add key="proxyaddress" value="192.168.130.5"/>
</appSettings>
<system.net>
<defaultProxy enabled ="true" useDefaultCredentials = "true">
<proxy autoDetect="false"
bypassonlocal="true"
proxyaddress="192.168.130.6"
scriptLocation="https://ws.mycompany.com/proxy.dat"
usesystemdefault="true"
/>
</defaultProxy>
</system.net>
</configuration>
var proxy= ConfigurationManager.AppSettings["proxyaddress"];
答案 0 :(得分:0)
这很简单,但我仍然感到惊讶,没有更简单的方法来传递这个。
在表单中,我必须包含一个rating_prototype_id
字段,稍后会将其传递给参数进行保存。
<%= rating_form.hidden_field :rating_prototype_id, value: rating_form.object.rating_prototype.id %>