在我的 controller.rb 中,我有以下代码:
@teams = Team.all
以下是一些示例输出:
<Team id: 1, name: "Eastside", nick: "Bunnies", api_team_id: 3235, created_at: "2013-01-18
21:19:24", updated_at: "2013-01-18 21:19:24">
<Team id: 2, name: "Westside", nick: "Turtles", api_team_id: 3235, created_at: "2013-01-18
21:19:24", updated_at: "2013-01-18 21:19:24">
<Team id: 3, name: "Northside", nick: "Birds", api_team_id: 3235, created_at: "2013-01-18
21:19:24", updated_at: "2013-01-18 21:19:24">
如何创建一个下拉列表,显示每个项目的Team.name和Team.nick? 另外,我如何link_to团队的各自team.id?
答案 0 :(得分:1)
我认为你需要先阅读一些Rails教程。
对于下拉菜单:
#in your view
<%= select_tag 'team_id', options_for_select(@teams.map{|team| ["#{team.name} #{team.nick}", team.id] }) %>
关于link_to
团队的展示页面,我假设您在routes.rb中宣布了您的资源。如果是这样,您应该有4个CRUD操作(创建检索更新删除)。
然后如果一切都设置正确,你可以这样做:
<% team = Team.first %>
<%= link_to team.name, team_path(team) %>