从 rails 应用程序中的 f.select options_for_select 获取值 [英] grabbing values from f.select options_for_select in rails app

查看:31
本文介绍了从 rails 应用程序中的 f.select options_for_select 获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个脚手架,用户可以选择不同类型的纸张并为每种类型添加纸张重量.然后我在 ApplicationController 中为每个用户计算 paper_weight 的运行总和.

我想更具体地向用户展示他们使用的每种 paper_type 的平均重量.我有点迷茫,因为我对 Rails 比较陌生,这里有人可以帮助我吗??

提前致谢

以下是我的观点.

在纸/new.html.erb

<%= f.label :paper_type, class: 'control-label' %><br><div class="控件"><%= f.select(:paper_type, options_for_select([['Skrifstofupappír', 'Skrifstofupappír'], ['Dagblaðapappír', 'Dagblaðapappír'], ['Glans_&_tímaritapappír','GlanAnnað', 'Annað']])) %>

<div class="control-group"><%= f.label :paper_weight, class: 'control-label' %><br><div class="控件"><%= f.number_field :paper_weight %>

在 ApplicationController.rb 中

@paper_weight_per_capita = current_user.papers.sum(:paper_weight)/(current_user.profile.staff)

解决方案

首先进行调整.

在您的 Paper 类中,您希望放置纸张类型的可接受值数组.这样,您将来可以验证可能不仅仅是这种形式的输入.它还允许您测试该代码并将数组放在视图中,这样并不是最佳实践.

class PaperPAPER_TYPES = ['Skrifstofupappír', 'Dagblaðapappír', 'Glans_&_tímaritapappír', 'Annað']...

然后在你看来,改成这个.

<%= f.select(:paper_type, options_for_select(Paper::PAPER_TYPES)) %>

这并不能回答你的问题,只是为了让你的代码更简洁和可测试.

然后在你的 User 模型中

def average_paper_typesself.papers.group(:paper_type).sum(:paper_weight)结尾

在你的部分:

<头><tr><% current_user.average_paper_types.each do |pair|%><th><%=pair[0]%></th><%结束%></tr></thead><tr><% current_user.average_paper_types.each do |pair|%><td><%=pair[1]%></td><%结束%></tr></tbody>

Hi in my apps I have a scaffold, were users can select different type of paper and add a paper weight for each type. Then I calculate the running total of paper_weight for each user in the ApplicationController.

I want to be more specific and show the users the average weight of each paper_type they are using. I´m a bit lost since I ´m rather new to rails, can anyone here help me??

thanks in advance

below are my views.

in paper/new.html.erb

<div class="control-group"> 
    <%= f.label :paper_type, class: 'control-label' %><br>
        <div class="controls">
            <%= f.select(:paper_type, options_for_select([['Skrifstofupappír', 'Skrifstofupappír'], ['Dagblaðapappír', 'Dagblaðapappír'], ['Glans_&_tímaritapappír', 'Glans_&_tímaritapappír'], ['Annað', 'Annað']])) %>
      </div>
</div>  


<div class="control-group"> 
    <%= f.label :paper_weight, class: 'control-label' %><br>
        <div class="controls">
            <%= f.number_field :paper_weight %>
        </div>
</div>  

in ApplicationController.rb

@paper_weight_per_capita = current_user.papers.sum(:paper_weight) / (current_user.profile.staff) 

解决方案

First a tweak.

In your Paper class is where you want to put the array of acceptable values for the paper type. That way you can validate input from perhaps more than just this form in the future. It also allows you to test that code plus putting an array right in the view like that is just not the best practice.

class Paper
  PAPER_TYPES = ['Skrifstofupappír', 'Dagblaðapappír', 'Glans_&_tímaritapappír', 'Annað']
  ...

Then in your view, change to this.

<%= f.select(:paper_type, options_for_select(Paper::PAPER_TYPES)) %>

That doesn't answer your question, just a tweak to make your code cleaner and testable.

Then in your User model

def average_paper_types
  self.papers.group(:paper_type).sum(:paper_weight)
end

And in your partial:

<table>
  <thead>
    <tr>
      <% current_user.average_paper_types.each do |pair| %>
        <th><%= pair[0] %></th>
      <% end %>
    </tr>
  </thead>
  <tbody>
    <tr>
      <% current_user.average_paper_types.each do |pair| %>
        <td><%= pair[1] %></td>
      <% end %>
    </tr>
  </tbody>
</table>

这篇关于从 rails 应用程序中的 f.select options_for_select 获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆