Search code examples
ruby-on-railsstrong-parameters

rails additional params to controller


I want to pass a value which the model does not have

<div class="form-group">
    <%= form_for(@car) do |f| %>
      <%= f.label :name, "Add New Tags:" %>
      <%= f.text_field :name, class: "form-control" %>
      <%= hidden_field_tag :additional_parms, value: 'some_value' %>
      <%= f.submit "Add Car",:id => 'tag_btn', class: "btn btn-primary" %>
    <% end %>
</div>

I am using hidden field to the add the value the to params but when I look at the log, the additional_params is not in the params


Solution

  • Try this:

    <%= f.hidden_field :additional_params, value: 'some_value' %>

    Note that you'll need to add this as a virtual field in the model via attr_acessor and whitelist the parameter the controller:

    Model

    class Car < ActiveRecord::Base
    
       attr_accessor :additional_params
       ...
    

    Controller:

    def car_params
      params.require(:car).permit( ..., :additional_params)
    end