Rails - 在表单中显示外键引用 [英] Rails - Displaying Foreign Key References in a form

查看:397
本文介绍了Rails - 在表单中显示外键引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用两种模式做一个简单的练习。体育和团队,定义为

 
rails g scaffold体育名称:整数
rails g scaffold球队名称:整数球迷:整数运动:引用

(注意:我使用脚手架的原因是快速原型设计,所以我可以学习/实验我不是熟悉了)



问题是我的运动(即外键引用)显示如下

所以它很奇怪#< ; blahl blah> notation to it ...

 <%= form_for(@team) do | f | %GT; 
<%if @ team.errors.any? %GT;
< div id =error_explanation>
< h2><%= pluralize(@ team.errors.count,error)%>禁止保存此团队:< / h2>

< ul>
<%@ team.errors.full_messages.each do | msg | %GT;
< 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%>

我尝试将一行更改为 @ team.sport.name 但它会导致错误未定义方法'Ice Hockey'for#< Team:0x3e7e040> ...任何想法如何正确显示名称从这里开始

解决方案

您正在使用 text_field 来引用现有的对象,一个选择与运动作为选项在这里更合适。



这是它必须是改变:

 < div class =field> 
<%= f.label:sport%>< br />
<%= f.text_field:sport%>
< / div>

收件人:

 < div class =field> 
<%= f.label:sport%>< br />
<%= f.select:sport_id,options_for_select(Sport.all.map {| s | [s.name,s.id]})%>
< / div>

f.select 会生成一个select在HTML中,这些选项将使我在数据库中的所有运动。




更简洁的方法是在控制器中设置一个变量 @sports ,然后在视图中调用它:

 #在控制器中$#$ b def编辑
@sports = Sport.scoped
#...

#编辑视图
< div class =field>
<%= f.label:sport%>< br />
<%= f.select:sport_id,options_for_select(@ sports.map {| s | [s.name,s.id]})%>
< / div>






附加信息:如果你想为选择预先选择一个选项,你必须将它作为 options_for_select helper的第二个参数传递:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆