13

How do I get values to prepopulate a new device form when navigating to it from rfid partial of view/cabinet/show page? device has_one rfid.

The link from cabinet/show:

<%= link_to "Create New Device (non-functional)", new_device_path({:id => @rfid.id, cabinet_id: @cabinet.id}), :class => "btn btn-primary" %>

The devices_controller, I want to have the create method work when either 0 or 2 params are passed:

def create(options)

if options[:cabinet_id] and options[:id]
  @rfid = Rfid.find(params[:id])
  @device = Device.new(params[:device])
  @device.rfid = @rfid
  @device.cabinet_id = :cabinet_id

else
  @device = Device.new(params[:device])
  @cabinet = Cabinet.find(params[:device][:cabinet_id])
  @device.row_id = @cabinet.row_id
end
  respond_to do |format|
    if @device.save and @cabinet.refresh_capacity_used and @cabinet.refresh_consumption
      format.html { redirect_to @device, notice: 'Device was successfully created.' }
      format.json { render json: @device, status: :created, location: @device }
    else
      format.html { render action: "new" }
      format.json { render json: @device.errors, status: :unprocessable_entity }
    end
  end

end

The cabinet_id and rfid portion of new device/_form:

<tr>
        <th>Cabinet</th>
        <td><%= f.select :cabinet_id, options_from_collection_for_select(Cabinet.order(:name).all, "id", "name", @device.cabinet_id) %></td>
</tr>


<tr>
        <th>RFID</th>
        <td><%= f.text_field :rfid, :value => params[:rfid]%></td> 
</tr>

Thank you.

3
  • Are you only trying to get the RFID field to prepopulate?
    – zeantsoi
    Mar 14, 2013 at 15:38
  • No, just taking it one step at a time. Hopefully if I figure out rfid, I'll get cabinet on my own.
    – Joe Essey
    Mar 14, 2013 at 15:48
  • I've answered with code that should pre-populate both fields. Can you tell me if it works?
    – zeantsoi
    Mar 14, 2013 at 17:00

2 Answers 2

15

If you have:

<%= link_to "Create New Device (non-functional)", new_device_path({:id => @rfid.id, :cabinet_id => @cabinet.id}), :class => "btn btn-primary" %>

you can get prepopulate the rfid and cabinet_id fields using:

<%= f.select :cabinet_id, options_from_collection_for_select(Cabinet.order(:name).all, "id", "name", params[:cabinet_id]) %>

<%= f.text_field :rfid, :value => params[:id] %>

Note that your params key must match the value passed into the hash in your link_to tag.

6
  • Awesome! I had to fiddle a bit but you definitely got me on the right path.
    – Joe Essey
    Mar 14, 2013 at 17:14
  • 2
    Except that _form.rb is also used by the edit action, and now editing those values is broken.
    – aidan
    Mar 9, 2016 at 3:52
  • @aidan, you've gotta be kidding; if your _form.rb values are being affected, you have them incorrectly mapped. Also, providing some context around the problem you're experiencing would be helpful, particularly in following up to an answer that is three years old. What version Rails are you even using? The version has been bumped several times since this question was answered.
    – zeantsoi
    Mar 9, 2016 at 5:56
  • 2
    @zeantsoi I'm assuming you're suggesting he replaces <%= f.text_field :rfid %> with <%= f.text_field :rfid, :value => params[:id] %> in _form.html.erb? If he does that, then yes, the value will default to params[:id]. But if _form.html.erb is being included by edit.html.erb because the user is trying to edit an existing @device, then the rfid value of that @device will be clobbered.
    – aidan
    Mar 11, 2016 at 1:23
  • 1
    I finally found a way to prevent this: change <%= f.select :cabinet_id, options_from_collection_for_select(Cabinet.order(:name).all, "id", "name", params[:cabinet_id]) %> to <%= f.select :cabinet_id, options_from_collection_for_select(Cabinet.order(:name).all, "id", "name", params[:cabinet_id] || @device.cabinet_id) %>. This way, if params[:cabinet_id] is empty, then @device.cabinet_id (the existing value) will be used instead. Sep 2, 2016 at 22:26
6

@zeantsoi's answer as posted mostly works, but it breaks the editing functionality. As a fix, I propose the following minor modification:

<%= f.select :cabinet_id, options_from_collection_for_select(Cabinet.order(:name).all, "id", "name", params[:cabinet_id] || @device.cabinet_id) %>

<%= f.text_field :rfid, :value => params[:rfid] || @device.rfid %>

This way, if params[:cabinet_id] is absent, @device.cabinet_id (the existing value) will persist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.