Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么我的have_one关系不起作用_Ruby On Rails_Associations_Has One - Fatal编程技术网

Ruby on rails 为什么我的have_one关系不起作用

Ruby on rails 为什么我的have_one关系不起作用,ruby-on-rails,associations,has-one,Ruby On Rails,Associations,Has One,我有一些型号,颜色和类型。分数只有一个权限、颜色和类型。权限、颜色和类型都属于分数。我在为分数创建表单时遇到问题 我希望用户根据分数创建这种关系,而每个人似乎只处理从权限、颜色、类型和表单创建的反向场景。我的具体意思是,例如: Authorities - foo - bar Colors - red - blue 我希望使用进入分数,选择创建新分数,然后从下拉列表中选择foo,从颜色列表中选择blue,输入分数并提交。权限、颜色和类型不会有太大变化,用户可以从中选择分数 当前执行此操

我有一些型号,颜色和类型。分数只有一个权限、颜色和类型。权限、颜色和类型都属于分数。我在为分数创建表单时遇到问题

我希望用户根据分数创建这种关系,而每个人似乎只处理从权限、颜色、类型和表单创建的反向场景。我的具体意思是,例如:

Authorities
 - foo
 - bar

Colors
 - red
 - blue
我希望使用进入分数,选择创建新分数,然后从下拉列表中选择foo,从颜色列表中选择blue,输入分数并提交。权限、颜色和类型不会有太大变化,用户可以从中选择分数

当前执行此操作时,会出现以下错误:

ScoreAuthority(#2248480380) expected, got String(#2155957040) 
score/_form.html.erb


我不知道我在这里做错了什么,任何帮助都将不胜感激。谢谢

您应该在表单中使用:score\u authority\u id而不是:score\u authority

<%= f.collection_select :score_authority_id, ScoreAuthority.order(:name), :id, :name, {}, {:class=>'chosen'} %>

 <%= f.collection_select :score_color_id, ScoreColor.order(:name), :id, :name, {}, {:class=>'chosen'} %>

 <%= f.collection_select :score_color_id, ScoreColor.order(:name), :id, :name, {}, {:class=>'chosen'} %>
请参阅此以了解更多详细信息

或者你可以试试

class Score
  has_one :score_authority
  accepts_nested_attributes_for :score_authority
end

<%= form_for @score ... do |f| %>
  ...
  <%= f.fields_for :score_authority do |b| %>
    <%= b.collection_select :id, ScoreAuthority.all, :id, :name %>
    ...
  <% end %>
  ...
<% end %>

我以前尝试过,但是加载编辑分数页面会导致未定义的方法“分数\权限\ id”错误没有错误,但是表单的下拉列表应该是空白的。我觉得我应该使用.build方法,对吗?但我还是不太明白。是的。在新的页面上。你必须构建它们。如何?我尝试了def create@score=score.newparams[:score]@score.score\u authority.build@score.score\u type.build@score.score\u color.build,但相同result@score.build_score_authority//@score.build\u score\u type//@score.build\u score\u color
<%= f.collection_select :score_authority_id, ScoreAuthority.order(:name), :id, :name, {}, {:class=>'chosen'} %>

 <%= f.collection_select :score_color_id, ScoreColor.order(:name), :id, :name, {}, {:class=>'chosen'} %>

 <%= f.collection_select :score_color_id, ScoreColor.order(:name), :id, :name, {}, {:class=>'chosen'} %>
class Score
  has_one :score_authority
  accepts_nested_attributes_for :score_authority
end

<%= form_for @score ... do |f| %>
  ...
  <%= f.fields_for :score_authority do |b| %>
    <%= b.collection_select :id, ScoreAuthority.all, :id, :name %>
    ...
  <% end %>
  ...
<% end %>