Jar*_*rom 0 html-table ruby-on-rails primary-key
我是红宝石的新手,比我想要的慢一点.我正在使用ruby 3.0.对于我创建的数据库中的一个表,我希望主键显示在"索引"页面上.我很难做到这一点.我手动将id放在索引视图中,但它继续说"未定义的方法`venture_round_id'"
这就是我的index.html对于表的看法:
<h1>Listing venture_rounds</h1>
<table>
<tr>
<th>id</th>
<th>company</th>
</tr>
<% @venture_rounds.each do |venture_round| %>
<tr>
<td><%= venture_round.venture_round_id %></td>
<td><%= venture_round.company_id %></td>
<td><% link_to 'show', venture_round %></td>
<td><%= link_to 'Edit', edit_venture_round_path(venture_round) %></td>
<td><%= link_to 'Destroy', venture_round, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
Run Code Online (Sandbox Code Playgroud)
我手动输入了"venture_round_id"和"id".
这是我的控制器对索引的看法:
def index
@venture_rounds = VentureRound.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @venture_rounds }
end
end
Run Code Online (Sandbox Code Playgroud)
我已经研究了这个问题两天了,但却找不到太多关于它的问题.我想这个问题与在自己的表中访问密钥有关.我使用venture_round_id作为外键的数据库结构的其他部分工作正常.任何指针或提示将不胜感激!
尝试venture_round.id而不是venture_round.venture_round_id
来自MrDanA的编辑:
外键通常采用"table_id"格式,但表自己的主键通常只是"id".如果您想查看数据库表的外观,可以打开db/schema.rb来查看模式.您也可以启动一个Rails控制台,如果您只输入您的类名称,如VentureRound,它将显示其属性,如VentureRound(id:integer,company_id:integer等)