title: How to change value in params array in Ruby on Rails?
body: <p>I have an instance where I am recording prices for water from vendors. My vendor model has <code>:price</code>. However, I want to give users the option to input a price for different volumes, and do the simple division for them rather than having them to do it. In other words, users should be able to input $1.99 per liter or $3.99 for a gallon and so on. To do this, I need a virtual attribute in my form for <code>:unit</code>, since I don't want to be storing units in the table. Everything works well, except that I cannot seem to update <code>vendor_params[:price]</code> before I update the record or create a new record. This seems like it should be a cake walk, but I Googled most of the day and can't figure out how to make it work.<br>
   Here is what I have:</p>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>class Vendor &lt; ActiveRecord::Base
     attr_accessor :unit
  ...
  end
  </code></pre>
  
  <p><strong>Form:</strong> </p>
  
  <pre><code>&lt;%= form_for(@vendor) do |f| %&gt;
  ...
  &lt;div class="field"&gt;
      &lt;%= f.label :price %&gt;&lt;br&gt;
      &lt;%= f.text_field :price %&gt;
      &lt;%= select( "unit", "id", { "1 Liter" =&gt; "1", "Bottle (2 liters)" =&gt; "2", "Jerry Can  (20 liters)" =&gt; "20"}) %&gt;
  &lt;/div&gt;
  ...
  &lt;% end %&gt;
  </code></pre>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>...
  def update 
  
  vendor_params[:price] = vendor_params[:price].to_f/params[:unit][:id].to_f
  
  respond_to do |format|
    if @vendor.update(vendor_params)
      format.html { redirect_to @vendor, notice: 'Vendor was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @vendor.errors, status: :unprocessable_entity }
    end
  end
  ...
  end
  </code></pre>
  
  <p>I know that <code>vendor_params[:price].to_f/params[:unit][:id].to_f</code> returns the correct value. I just can't seem to assign that value to <code>vendor_params[:price]</code> before I update the record. I also tried the following which throws an error:</p>
  
  <pre><code>@vendor_params[:price] = vendor_params[:price].to_f/params[:unit][:id].to_f
  </code></pre>
  
  <p>It seems like this should be trivial! I guess I could use <code>form_tag</code> instead of <code>form_for</code>, but that seems odd when updating the full record. (The edit form has all fields for all the object attributes.) Anywho, I'm open to ideas and suggestions.<br>
  Thanks!!</p>