Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails-设置选项列表的格式_Ruby On Rails - Fatal编程技术网

Ruby on rails Rails-设置选项列表的格式

Ruby on rails Rails-设置选项列表的格式,ruby-on-rails,Ruby On Rails,我正在尝试用rails 4制作一个应用程序 在我的展示页面中,我想展示提供项目参与的用户所做选择的输出。有几个选项,用户可以选择其中一个或多个。我将输出格式化为一个句子,这样,如果只选择一个输出,它就能正确读取。有时,会做出多个选择,我希望它们以语法正确的格式显示(因此,在最后一个和第二个选择之间用逗号分隔选择和“and”) 我的格式当前为: <div class="datageneraltext">Participants receive <span>

我正在尝试用rails 4制作一个应用程序

在我的展示页面中,我想展示提供项目参与的用户所做选择的输出。有几个选项,用户可以选择其中一个或多个。我将输出格式化为一个句子,这样,如果只选择一个输出,它就能正确读取。有时,会做出多个选择,我希望它们以语法正确的格式显示(因此,在最后一个和第二个选择之间用逗号分隔选择和“and”)

我的格式当前为:

<div class="datageneraltext">Participants receive

<span>

        <% if @project.scope.try(:result).try(:report) == true  %>

            <%= render :text => "a written project report" %>

        <% end %>



        <% if @project.scope.try(:result).try(:standard_licence) == true  %>

            <%= render :text => "a licence to use the project results (a standard licence)" %>

        <% end %>



        <% if @project.scope.try(:result).try(:bespoke_licence) == true  %>

          <%= render :text => "a licence to use the project results (a bespoke licence)" %>

    <% end %>




    <% if @project.scope.try(:result).try(:option) == true  %>

      <%= render :text => "an option to acquire rights to use the the project results" %>

    <% end %>




    <% if @project.scope.try(:result).try(:assignment) == true  %>

      <%= render :text => "an assignment of rights to commercialise the project results" %>

    <% end %>




    <% if @project.scope.try(:result).try(:consulting) == true  %>

      <%= render :text => "engagement for further research consultation" %>

  <% end %>



    <% if @project.scope.try(:result).try(:other) == true  %>

      <%= @project.scope.try(:result).other_outcome %>

  <% end %>

</span>


  </div>
<div class="datageneraltext">

  <%= @project.scope.try(:result).description %>

  </div>
参与者收到
“书面项目报告”%>
“使用项目成果的许可证(标准许可证)”%>
“使用项目成果的许可证(定制许可证)”%>
“获取项目结果使用权的选项”%>
“项目成果商业化权利转让”%>
“参与进一步研究咨询”%>
我有一个用户用于选择结果的表单,如下所示:

  <div class="col-md-7">
      <div class="response-project" style="margin-left:5%">
        <%= f.simple_fields_for :scope do |results_s| %>
            <%= results_s.simple_fields_for :result do |r| %>
                <%= r.input :report, :as => :boolean, :label => false, inline_label: "A report on project outcomes" %>
                <%= r.input :standard_licence, :as => :boolean, :label => false, inline_label: "A standard licence to use project results" %>
                <%= r.input :bespoke_licence, :as => :boolean, :label => false, inline_label: "A bespoke licence to use project results" %>
                <%= r.input :option, :as => :boolean, :label => false, inline_label: "An option to acquire an interest in the project outcomes" %>
                <%= r.input :assignment, :as => :boolean, :label => false, inline_label: "Assignment of project outcomes" %>
                <%= r.input :consulting, :as => :boolean, :label => false, inline_label: "Available to consult in relation to the outcomes" %>
                <%= r.input :other, :as => :boolean, :label => false, inline_label: "Another outcome (not listed above)" %>

:boolean,:label=>false,内联标签:“项目结果报告”%>
:boolean,:label=>false,内联标签:“使用项目结果的标准许可证”%>
:boolean,:label=>false,内联标签:“使用项目结果的定制许可证”%>
:boolean,:label=>false,inline_label:“获取项目成果利益的选项”%>
:boolean,:label=>false,内联标签:“项目成果分配”%>
:boolean,:label=>false,inline_label:“可就结果进行咨询”%>
:boolean,:label=>false,内联标签:“另一个结果(上面未列出)”%>
有没有人知道如何在这些选项之间写一个循环,这样,如果只有一个选项,它仍然是我的选项,如果有两个选项,它们用“and”分隔,如果有两个以上的选项,逗号分隔所有选项,但最后两个选项用“and”分隔


谢谢

看看你的应用程序是如何建模的会有帮助。您需要在某个地方存储要输出的所需文本,然后可以使用helper方法获取适当的响应并格式化它。一个基本的解决方案是项目模型中的一个散列,它可以在助手方法中使用。以下内容将为您提供一些可供使用的想法

class Project
  def response_text
     {  report: "a written project report",
        standard_license: "a licence to use the project results (a standard licence)"
     }
  end
end
局部

<%= format_response(@project) %>

助手/project_helper.rb

def format_response(project)
   output = []
   project.scope.result.each do |option|
     output << response_text[option] if option
   end
   output.join(', ')
   content_tag(:span, output)
end
def格式_响应(项目)
输出=[]
project.scope.result.each do |选项|

输出这将有助于了解您的应用程序是如何建模的。您需要在某个地方存储要输出的所需文本,然后可以使用helper方法获取适当的响应并格式化它。一个基本的解决方案是项目模型中的一个散列,它可以在助手方法中使用。以下内容将为您提供一些可供使用的想法

class Project
  def response_text
     {  report: "a written project report",
        standard_license: "a licence to use the project results (a standard licence)"
     }
  end
end
局部

<%= format_response(@project) %>

助手/project_helper.rb

def format_response(project)
   output = []
   project.scope.result.each do |option|
     output << response_text[option] if option
   end
   output.join(', ')
   content_tag(:span, output)
end
def格式_响应(项目)
输出=[]
project.scope.result.each do |选项|

输出嗨,玛歌,谢谢你。我只是有一个表单,用户用它来选择结果。这个问题已经用表格更新了,同时,我将更仔细地了解助手的想法。谢谢你好,玛歌,谢谢你。我只是有一个表单,用户用它来选择结果。这个问题已经用表格更新了,同时,我将更仔细地了解助手的想法。谢谢你好,玛歌,谢谢你。我只是有一个表单,用户用它来选择结果。这个问题已经用表格更新了,同时,我将更仔细地了解助手的想法。非常感谢。