2

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.

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.

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

<%= 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 %>

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
apneadiving
  • 114,565
  • 26
  • 219
  • 213
jyanks
  • 2,356
  • 1
  • 19
  • 36

1 Answers1

5

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| %>

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

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]

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 %>

You'd do something like:

<%= 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]

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.

Jaryl
  • 2,561
  • 1
  • 24
  • 33
  • The individual fields don't exist in the model/database, only the cashstart field, which is expected to be a decimal value. That's my real issue- how do I access the quarters, nickels, dimes, etc after the user hits the submit button? – jyanks Aug 09 '11 at 06:24
  • Added on a potential solution. – Jaryl Aug 09 '11 at 07:40
  • Edit: Solved! I used an if check to see if params.has_key?(:quarters) to make my controller's update work for my countcash form as well as others. Thank you so much for your help! – jyanks Aug 11 '11 at 21:11