So, the thing that i want to do, is to have a button, to add more fields into a form. Let's say i have this:
<div class="row">
<div class="form-group string required">
<div class="cep_filter">
<div class="col-md-3 traffic-columns">
<%= f.input :condition, collection: @traffic_columns, label: "Monitor (*)" %>
</div>
<div class="col-md-1">
<%= f.input :operator, collection: @operator, label: "Op (*)" %>
</div>
<div class="col-md-2">
<%= f.input :filter, label: "Value (*)",input_html: {type: "text" } %>
</div>
</div>
<div class="col-md-3">
<%= f.input :window, collection: @window, label: "Window Type", input_html: {type: "text"} %>
</div>
<div class="col-md-3">
<%= f.input :window2, label: "Window Value", input_html: {type: "text"} %>
</div>
</div>
</div>
Then, suppose i have a button like this:
<%= link_to content_tag(:i, nil, class: "fa fa-plus"),
'', style: 'color: green', class: "btn btn-small add-new-condition", 'data-backdrop' => "static" %>
What i want to do, is that when you click that button, you call this:
$(document).on("click", ".add-new-condition", function(e) {
var html =
"<div class='col-md-3 traffic-columns'> \
<%= f.input :new_param, collection: @traffic_columns, label: 'Monitor (*)' %>\
</div>
"
});
and then append that html to some div element. From what i've read, its not possible to inject ruby from javascript. Its something related to one being server side and the other client side. I already try it and the ruby code appears as a string. I could build those new fields by using the pure html, like and so, but the important thing here, is that i want to add new fields that later i can access in a controller, for example the new field, with params[:new_param].
Is there a way to add new fields only with html so i can access later the entered value in my rails controller? Thank you.
<%=andf.input. Remember that when you send the view HTML from your server, it is going through a Ruby/ERB preprocessor step, and that doesn't happen hereparams[:name]whenever you submit the form. I assume you wouldn't need the data in your controller before submission too.