我有一个深层嵌套的关系,我想创建一个表单来访问不同的部分

时间:2013-10-21 15:26:25

标签: ruby activerecord ruby-on-rails-4 activeview

user_relation.rb

 14 class UserRelation < ActiveRecord::Base
 15   belongs_to :user
 16   belongs_to :with_user, :class_name => "User", :foreign_key => :with_user_id
 17   belongs_to :group
 18   has_many :evaluations
 19   has_many :ratings, :through => :evaluations
 20   has_many :user_relation_skills  
 21   has_many :user_relation_skills_in_progress, -> { where state: "in_progress" }, class_name: 'UserRelationSkill' 
 22   alias_method :skills, :user_relation_skills
 23 
 24   has_many :progresses, :through => :skills

user_relation_skill.rb

 13 class UserRelationSkill < ActiveRecord::Base
 14   belongs_to :skill
 15   belongs_to :user_relation
 16   has_many :progresses
 17   has_many :ratings, :as => :scope
 18   has_many :videos, :class_name => "Video", :as => :viewable
 19 
 20   accepts_nested_attributes_for :ratings, :progresses
 21 
 22   accepts_nested_attributes_for :videos, :allow_destroy => true

rating.rb

 19 class Rating < ActiveRecord::Base
 20   belongs_to :scope, :polymorphic => true
 21   belongs_to :ratable, :polymorphic => true
 22   belongs_to :rater, :class_name => "User"
 23   belongs_to :evaluation

_form.html.erb

 17 <%= form_for(@user_relation) do |f| %>
 18     <%= f.fields_for :user_relation_skill do |ff| %> 
 19         <%= ff.select(:rating, options_for_select(
 20             (1..8),
 21             (ff.rating.try(:value) || 0))) %>   
 22     <% end%>
 23 <% end%>

@ user_relation.first.user_relation_skills.first.ratings.first.value提取我想要的数据 我只是好奇我如何将它变成一个表单,以便我可以使用提交按钮更新和编辑值

目前form_for代码只返回一个

undefined method `rating'
(ff.rating.try(:value) || 0))) %> 

1 个答案:

答案 0 :(得分:0)

我认为您需要使用ff.object.rating代替ff.rating

至少应该从ff为你提供一个值,但我不是100%你是如何创建选择的,所以这可能不是解决问题所需的唯一方法。