Rails使用多个字段更新单个属性 [英] Rails Update Single Attribute with Multiple Fields

查看:79
本文介绍了Rails使用多个字段更新单个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试创建一种称为countcash的表格,该表格允许用户输入四分之一,一角硬币,镍币,几美分,二十,十,五,一的实际数量,然后输入现金中任何其他项目的货币价值框,然后当他们单击提交"时,我要总计这些值并设置当前班次记录的cashstart字段.

I'm currently trying to make a form called countcash that allows users to input the actual number of quarters, dimes, nickels, pennies, twenties, tens, fives, ones and then the monetary value of any other items in the cash box and then when they hit submit, I want to total those values and set the cashstart field of my current shift record.

真正的问题是,在这种情况下,我不确定我的表单应该是什么样子以及应该如何设置控制器/模型.

The real issue is that I'm not sure what my form is supposed to look like and how the controller/model are supposed to be setup in this instance.

我在session[:shift_id]变量中保存了一个shift_id,具有id的班次是我要使用计算出的cashstart修改的班次.

I have a shift_id saved in a session[:shift_id] variable and the shift with that id is the shift that I want to modify with the calculated cashstart.

如果要正确遵循MVC,<%= form_for %>标记应该是什么样子?如何设置控制器/模型以使用计算出的值更新数据库?我应该将路线设置为什么?我在堆栈溢出或其他Rails论坛上都找不到类似的东西(尽管您认为通过多个文本字段修改单个属性会很简单)

What does the <%= form_for %> tag need to look like if I want to follow the MVC correctly and how do I set up the controller/model to update the database with the calculated value? And what should I be setting the route to? I can't find anything like this on either stack overflow or other rails forums (Although you'd think that modifying a single attribute through multiple text fields would be pretty straight forward)

仅供参考,这是我以前使用的表格(无济于事)

Just as a reference, this is the form I was using previously (To no avail)

app/views/countcash.html.erb

app/views/countcash.html.erb

<%= form_for @shift do |f| %>   

<table>
  <tr>  
    <th>Coins</th>
    <th></th>
  </tr>

  <tr>
    <td>Quarters: </td>
    <td><%= f.text_field :quarters %><br /></td>
  </tr>

  <tr>
    <td>Dimes: </td>
    <td><%= f.text_field :dimes %><br /></td>
  </tr>

  <tr>
    <td>Nickels: </td>
    <td><%= f.text_field :nickels %><br /></td>
  </tr>

  <tr>
    <td>Pennies: </td>
    <td><%= f.text_field :pennies %><br /></td>
  </tr>

  <tr>
    <th>Bills</th>
    <th></th>
  </tr>

  <tr>
    <td>Twenties: </td>
    <td><%= f.text_field :twenties %><br /></td>
  </tr>

  <tr>
    <td>Tens: </td>
    <td><%= f.text_field :tens %><br /></td>
  </tr>

  <tr>
    <td>Fives: </td>
    <td><%= f.text_field :fives %><br /></td>
  </tr>

  <tr>
    <td>Ones: </td>
    <td><%= f.text_field :ones %><br /></td>
  </tr>

  <tr>
    <th>Other</th>
    <th></th>
  </tr>

  <tr>
    <td>Value (in $): </td>
    <td><%= f.text_field :others %><br /></td>
  </tr>


</table>

<div class="actions">
    <%= f.submit %>
</div>
<% end %>

这是我的shifts_controller暂时不执行任何操作,因为我不确定单击提交按钮后如何去更新属性

And here's my shifts_controller that doesn't do anything at the moment, since I'm not sure how I need to go about updating the attribute after the submit button is clicked

def countcash
    @shift = Shift.find(session[:shift_id])

    redirect_to(login_path)
end

推荐答案

我想您已经快到了,大概您有一个命名路由,可以通过它来进行表单声明:

I think you are almost there, presumably you have a named route which allows you to do this as your form declaration:

<%= form_for @shift, :url => countcash_shift_path(@shift) do |f| %>

如有疑问,请在控制台中运行rake路由以确定是否存在此类路由.如果不是,那么您可能想探索RESTful路由,在您的routes.rb中应该看起来像这样:

If in doubt, run rake routes in your console to determine if such routes exist. If it doesn't, perhaps you would want to explore RESTful routes, which should look something like this in your routes.rb:

resources :shifts

在控制器中的countcash操作中,只需执行以下操作即可:

Inside the countcash action in your controller, it's just a matter of doing this:

@shift = Shift.find(session[:id])
@shift.update_attributes(params[:shift]

这应该在假设您已为班次资源设置RESTful路由的情况下起作用.另外,我假设您的模型/数据库设置有季度,角点等作为单独的字段(否则,您将需要比update_attributes更高级的字段).

This should work assuming that you have you have set up RESTful routes for your shift resource. Also, I am assuming that your model/database is set up with quarters, dimes, etc as individual fields (otherwise you will require some more advanced than update_attributes).

由于数据库中没有这些单独的字段(根据您的评论),因此不应将其作为模型表单的一部分.因此,代替:

Since you are not having these individual fields in the database (as per your comment), you should not be putting as part of the model's form. So instead of:

<%= f.text_field :dimes %>

您将执行以下操作:

<%= text_field_tag :dimes, params[:dimes] %>

在控制器内部,您可以像这样访问它们:

And inside your controller, you can just access them like so:

params[:dimes] + params[:quarters] # multiply as per currency logic

如果您想花更多的钱,可以做的是编写采用这些值的方法,因此您仍将表单保留为当前状态,但添加采用这些值并对其进行处理的方法像这样(对于所有类型的货币,不只是一角钱):

If you want to get a little more fancy, what you could do is write methods that take in these values, so you still leave the form as your current state, but add methods that take in the values and do something with it like so (for all types of currency, not just dimes):

def dimes=(value)
  @running_total += value
end

在您的控制器中,执行以下操作:

In your controller, do this instead:

@shift.attributes = params[:shift]

所以发生的是,当您传递模型的属性哈希值时,它将尝试根据符号名称分配值.因此,参数中的角点值将被传递,就像被称为:

So what happens is that when you pass your model the hash of attributes, it will try to assign the values based on the symbol names. So the value of dimes in params will be passed in as if this was being called:

@shift.dimes = params[:shift][:dimes]

假设您使用@running_total做某事,我认为这应该对您有用.您可以使用@amount或其他东西(如果您已将其用作模型属性).

I think that should work for you, assuming that you use @running_total for something. You could use @amount or something if that's what you already have as a model attribute.

这篇关于Rails使用多个字段更新单个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆