Rails - 在表单中显示外键引用
[英] Rails - Displaying Foreign Key References in a form
本文介绍了Rails - 在表单中显示外键引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用两个模型做一个简单的练习.运动和团队,定义为
<前>rails g 脚手架运动名称:整数rails g 脚手架 团队名称:整数 粉丝:整数运动:参考
(注意:我使用脚手架的原因是快速原型制作,所以我可以学习/实验我还不熟悉的部分)
问题是我的运动"(即外键引用)显示如下
所以它有那个奇怪的 #
符号......
<%= form_for(@team) do |f|%><% 如果@team.errors.any?%><div id="error_explanation"><h2><%=pluralize(@team.errors.count, "error") %>禁止保存该团队:</h2><ul><% @team.errors.full_messages.each 做 |msg|%><li><%=msg%></li><%结束%>
<%结束%><div class="field"><%= f.label :name %><br/><%= f.text_field :name %>
<div class="field"><%= f.label :fans%><br/><%= f.number_field :fans %>
<div class="field"><%= f.label :sport %><br/><%= f.text_field :sport %>
<div class="actions"><%= f.submit %>
<%结束%>
我已经尝试将一行更改为 @team.sport.name
但它导致错误 undefined method 'Ice Hockey' for #<Team:0x3e7e040>代码>...任何想法如何从这里正确显示名称??
解决方案
您正在使用 text_field
来引用现有对象,使用 Sports 作为选项的 select
将是在这里更合适.
这是必须改变的地方:
<%= f.label :sport %><br/><%= f.text_field :sport %>
致:
<%= f.label :sport %><br/><%= f.select :sport_id, options_for_select(Sport.all.map{|s|[s.name, s.id]}) %>
f.select
将在 HTML 中生成一个选择框,选项将包含您数据库中的所有运动.
关于它的一些文档:
更简洁的方法是在您的控制器中设置一个变量 @sports
然后在您的视图中调用它:
# 在控制器中定义编辑@sports = Sport.scoped#...# 在编辑视图中<div class="field"><%= f.label :sport %><br/><%= f.select :sport_id, options_for_select(@sports.map{ |s| [s.name, s.id] }) %>
<小时>
附加信息:如果你想为选择预选"一个选项,你必须将它作为options_for_select
助手的第二个参数传递:
options_for_select(@sports.map{ |s| [s.name, s.id] }, params[:sport_id])# 这将默认选择与 params[:sport_id] 的值匹配的选项
I'm doing a simple exercise with two models. Sport and Teams, defined as
rails g scaffold sport name:integer
rails g scaffold team name:integer fans:integer sport:references
(Note: The reason I'm using scaffold is rapidly prototyping so I can learn/experiment with the parts I'm not familiar with yet)
Problem is that my "sport" (i.e. the foreign key reference) is showing like the following
So it's got that weird #<blahl blah>
notation to it...
<%= form_for(@team) do |f| %>
<% if @team.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@team.errors.count, "error") %> prohibited this team from being saved:</h2>
<ul>
<% @team.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :fans %><br />
<%= f.number_field :fans %>
</div>
<div class="field">
<%= f.label :sport %><br />
<%= f.text_field :sport %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I've tried changing the one line to @team.sport.name
but it results in an error undefined method 'Ice Hockey' for #<Team:0x3e7e040>
... Any ideas how to properly display name from here??
解决方案
You are using a text_field
for referencing an existing object, a select
with Sports as options would be more appropriate here.
This is where it has to be changed:
<div class="field">
<%= f.label :sport %><br />
<%= f.text_field :sport %>
</div>
To:
<div class="field">
<%= f.label :sport %><br />
<%= f.select :sport_id, options_for_select(Sport.all.map{|s|[s.name, s.id]}) %>
</div>
The f.select
will generate a select box in HTML, the options will me all the sports in your DB.
Some documentation about it:
A cleaner way would be to set a variable @sports
in your controller and call it then in your views:
# in controller
def edit
@sports = Sport.scoped
#...
# in edit view
<div class="field">
<%= f.label :sport %><br />
<%= f.select :sport_id, options_for_select(@sports.map{ |s| [s.name, s.id] }) %>
</div>
Additionnal information: If you want to "pre-select" an option for the select, you have to pass it as the second argument of the options_for_select
helper:
options_for_select(@sports.map{ |s| [s.name, s.id] }, params[:sport_id])
# this will select by default the option that matches the value of params[:sport_id]
这篇关于Rails - 在表单中显示外键引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文