【问题标题】:Commas between between embedded ruby expression tags嵌入的 ruby​​ 表达式标签之间的逗号
【发布时间】:2017-01-28 14:56:52
【问题描述】:

我的 rails 应用程序包含一个供用户设置个人资料的表单。表单的一部分允许用户从长列表中选择服务,他们最多可以选择 5 种不同的服务。

<div class="form-group">
    <%= f.label :services_1 %>
    <%= f.select :services_1, ['Service 1', 'Service 2', 'Service 3', 'Service 4', 'Service 5', 'Service 6', 'Service 7', 'Service 8', 'Service 9', 'Service 9', ], {}, class: 'form-control' %>
  </div>
      
      <div class="form-group">
    <%= f.label :services_2 %>
    <%= f.select :services_2, ['Service 1', 'Service 2', 'Service 3', 'Service 4', 'Service 5', 'Service 6', 'Service 7', 'Service 8', 'Service 9', 'Service 9', ], {}, class: 'form-control' %>
  </div>
      
      <div class="form-group">
    <%= f.label :services_3 %>
    <%= f.select :services_3, ['Service 1', 'Service 2', 'Service 3', 'Service 4', 'Service 5', 'Service 6', 'Service 7', 'Service 8', 'Service 9', 'Service 9', ], {}, class: 'form-control' %>
  </div>
      
      <div class="form-group">
    <%= f.label :services_4 %>
    <%= f.select :services_4, ['Service 1', 'Service 2', 'Service 3', 'Service 4', 'Service 5', 'Service 6', 'Service 7', 'Service 8', 'Service 9', 'Service 9', ], {}, class: 'form-control' %>
  </div>
      
      <div class="form-group">
    <%= f.label :services_5 %>
    <%= f.select :services_5, ['Service 1', 'Service 2', 'Service 3', 'Service 4', 'Service 5', 'Service 6', 'Service 7', 'Service 8', 'Service 9', 'Service 9', ], {}, class: 'form-control' %>
  </div>

我想像这样在他们的个人资料页面上列出这些

Service 1, Service 2, Service 3, Service 4, Service 5

但是,如果用户选择的服务少于 5 个,例如只有 2 个,则会导致这种情况

Service 1, Service 2, , ,

这是我在视图文件中包含的内容

<p><%= @user.profile.services_1 %>, <%= @user.profile.services_2 %>, <%= @user.profile.services_3 %>, <%= @user.profile.services_4 %>, <%= @user.profile.services_5 %></p>

我应该如何更正它以删除多余的逗号?

【问题讨论】:

    标签: ruby-on-rails list comma embedded-ruby


    【解决方案1】:

    我认为您需要将选定的服务收集到一个数组中,然后组合加入它们。

    # in the controller
    @services = []
    @services << @user.profile.services_1 if @user.profile.services_1
    @services << @user.profile.services_2 if @user.profile.services_2
    @services << @user.profile.services_3 if @user.profile.services_3
    @services << @user.profile.services_4 if @user.profile.services_4
    @services << @user.profile.services_5 if @user.profile.services_5
    
    # in the view
    <p><%= @services.join(', ') %></p>
    

    如果您无法在此基础上进行简化,最好将此登录名放入模型中。

    # in the model
    def selected_services
      services = []
      services << services_1 if services_1.present?
      services << services_2 if services_2.present?
      services << services_3 if services_3.present?
      services << services_4 if services_4.present?
      services << services_5 if services_5.present?
      services
    end
    
    # in the controller
    @services = @user.profile.selected_services
    

    【讨论】:

    • 非常感谢哈林松鼠!您在模型中的第二个建议是正确的。
    • 不客气!如果这对你有用,你可以投票给我的答案。
    • 我已经完成了,但我收到一条消息说它不会影响公众分数,因为我的声誉低于 15。
    • 哦,我明白了,别担心,你会到达那里的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 2021-07-18
    相关资源
    最近更新 更多