【问题标题】:Displaying a string and not a integer on an index.html.erb file在 index.html.erb 文件上显示字符串而不是整数
【发布时间】:2015-05-01 22:51:53
【问题描述】:

在我将“program_id”链接到帐户表后,rails generate migration AddProgramIDToAccounts program_id:interger 并添加 <td><%= account.program_id %> 在 index.hrml.erb 文件中显示结果。但是,我得到的是 ID,而不是像在 _forms.htlm.erb 文件中创建的下拉菜单中那样的“程序名称”。

<div class="form-group">
    <%= f.label :program %><br>
    <%= f.collection_select :program_id, Program.all, :id, :program, {prompt: "Choose a Program"}, {class: "btn btn-default dropdown-toggle"} %>
  </div>

我感觉我创建的迁移不正确,但我不确定。

Rails 4.1.8

ruby 2.1.5p273(2014-11-13 修订版 48405)[x86_64-linux]

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3


    【解决方案1】:

    有了这个,你在说(评论)

    <%= f.collection_select 
       :program_id, #the parameter name
       Program.all, #the list of objects to use in the select
       :id,  #the parameter value
       :program, #the method you call on each object to get the text you want to display in the select
       {prompt: "Choose a Program"}, {class: "btn btn-default dropdown-toggle"} %>
    

    因此,select 将显示在每个 Program 对象上调用 .program 的结果。这对我来说感觉不对,但你没有提供你的架构的任何细节,所以我不能肯定地说。

    【讨论】:

    • 这是有道理的。我只是无法处理语法在我脑海中的样子。但我知道参数和方法会给出最终结果。总是让你发疯的小事。感谢您的补充意见。
    【解决方案2】:

    你需要写

    account.program.name
    

    在您的 index.html.erb 文件中,而不是 account.program_id

    【讨论】:

    • 完美运行。谢谢
    • @M.TDavis 欢迎您。请接受我的回答,因为它解决了您的问题
    猜你喜欢
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多