Joe Essey Joe Essey - 15 days ago 4
Ruby Question

Rails - How to use hidden field on form to pass a value of an object related to the one created on a form?

I am trying to pass an

rfid_tag
from a form that creates a
device
from a
hidden_field
. Device
has_many rfids
. The
rfid
already exists in the db.

Here is the code from the form:

<th>RFID Tag #</th>
<td>
<%= f.label(:@passed_rfid_tag, @passed_rfid_tag) %>
<%= f.hidden_field :rfid_tag, :value => @passed_rfid_tag %>
</td>
</tr>


Here is the code from the devices_controller:

def create
@cabinet = Cabinet.find(params[:device][:cabinet_id])
@device = Device.create(params[:device])
@device.rfids << Rfid.where('rfid_tag' => params[:rfid_tag]).first
@device.row_id = @cabinet.row_id
@device.save


I get the following error because
rfid_tag
is not an attribute of
device
. It is an attribute of
rfid
:

Can't mass-assign protected attributes: rfid_tag

app/controllers/devices_controller.rb:182:in `create'


Thanks.

Answer

The mass-assign error is happening because you are passing :rfid_tag as part of the params[:device] attribute hash.

In your example code, the :rfid_tag field needs to be provided using the hidden_field_tag form helper. This will keep it from being included in the params[:device] hash.

<tr>
  <th>RFID Tag #</th>
  <td>
    <%= label_tag 'rfid_tag', 'RFID Tag' %>
    <%= hidden_field_tag 'rfid_tag', @passed_rfid_tag %>
  </td>
</tr>

You can then access it via params[:rfid_tag]. Your create method should still work.