Interpolating ERB as an HTML value attribute (Rails) - ruby-on-rails

I am attempting to pass data as params in a hidden input tag's value attribute:
<input name="quote[destination]" value= <%= "#{quote["InboundLeg"]["OriginCity"]}" %> type="hidden" />
When the form is submitted, the params contains the first word of the string being interpolated and drops everything after the first space.
If I decide to pass through an ordinary string to params through value like so:
<input name="quote[destination]" value= "foo bar buzz" type="hidden" />
the entirety of the string passes through unlike the former case. Can anyone shed some light on why this may be and some possible solutions?

Change that line to:
<input name="quote[destination]" value="<%= quote["InboundLeg"]["OriginCity"] %>" type="hidden" />
Note that the quotes are outside of the ERB statement.
Or you might want to use the hidden_field_tag form helper which creates such hidden input fields and reads nicer:
<%= hidden_field_tag 'quote[destination]', quote["InboundLeg"]["OriginCity"] %>
I would always use a helper if I have a choice because IMHO is a bad practice to mix HTML and ERB like in the first example.

Replace your input tag with
<input name="quote[destination]" value= "<%= quote['InboundLeg']['OriginCity'] %>" type="hidden" />
You have to apply quotes to the value

Related

Convert html input to erb

How can I convert this:
<input type="file" id="file" name="file">
To a Rails input, if I can say it that way and if I change it to a Ruby input do I have to change anything in my JS code? Is there any benefits on using the code this way or can I use the html way?
Try this
<%= file_field_tag 'file' %>
It will generate this HTML
<input id="file" name="file" type="file" />
More info - Rails Form Helper file_field_tag
You can use it in raw html as well, but when you are using Rails, its recommended to follow the conventions.

How change simple_form input html tag

Hollow, I need to change input html tag for vue component.
In default config simple_form work like this
= f.input_field :name
<input class="string required" id="user_name" maxlength="255" name="user[name]" size="255" type="text">
But i need change to
= f.input_field :name
<vue-input class="string required" id="user_name" maxlength="255" name="user[name]" size="255" type="text"></vue-input>
Have you considered just using raw HTML in your view? You are not required to use f.input_field :name.
You could just directly write:
<vue-input class="string required" id="user_name" maxlength="255" name="user[name]" size="255" type="text"></vue-input>
You could also write your own HTML helper, see this question.
I don't think that will be possible easily as they are predefined to be used with Rails. You'll have to create new or monkey patch these methods to behave as intended.

Why in Rails when checkbox saving, just last element saved correctly

With Form_for:
- #questions.each do |secret_question|
= f.check_box :question, {multiple: true}, secret_question.id, 0
= f.label :question, secret_question.body
While saving, if I've checked last element - in base saved right value,
but if I've checked others elements (from first till penultimate) - in base saved '0'
What is wrong?
Thank you
Update1:
in controller get params:
private
def contact_params
params.require(:contact).permit(:name,... :secret_question)
end
Update2:
Result form:
<form id="new_contact" class="new_contact" method="post" accept-charset="UTF-8" action="/contacts" enctype="multipart/form-data"
<input type="hidden" value="0" name="contact[secret_question][]"></input>
<input id="secret_question_1" type="checkbox" name="contact[secret_question][]" value="1"></input>
<label for="secret_question_1">1</label>
<input type="hidden" value="0" name="contact[secret_question][]"></input>
<input id="secret_question_2" type="checkbox" name="contact[secret_question][]" value="2"></input>
<label for="secret_question_2">2</label>
<input type="hidden" value="0" name="contact[secret_question][]"></input>
<input id="secret_question_3" type="checkbox" name="contact[secret_question][]" value="3"></input>
<label for="secret_question_3">3</label>
<input type="hidden" value="0" name="contact[secret_question][]"></input>
<input id="secret_question_4" type="checkbox" name="contact[secret_question][]" value="4"></input>
<label for="secret_question_4">4</label>
Change your
def contact_params
params.require(:contact).permit(:name,... :secret_question)
end
to
def contact_params
params.require(:contact).permit(:name,... :secret_question => [])
end
to declare that parameter should be array.
Your input elements have the same name attribute: contact[secret_question][]. When submitting the form the browser sends all input elements in the order they are found inside the DOM, but Rails takes the last key-value pair it receives, if there are keys with the same name, and saves this value. This behavior conflicts with the way Rails treats checkboxes.
You can either use the option include_hidden: false for every check_box except of the first or you can use the check_box_tag instead of check_box. check_box_tag won't generate the hidden checkboxes which are causing the trouble.

How do I tell my simple_form object to return an array with just the selected IDs and not any empty values?

So I have a simple_form object that looks like this:
<%= f.input :parents, collection: (#node.family_tree.nodes - #node.parents - [#node]).uniq, as: :check_boxes, label: "Parent 1" %>
This produces HTML that looks like this:
<div class="node_parents">
<label>Parent 1</label>
<input id="node_parents_13" name="node[parents][]" type="checkbox" value="13" /><label>Jack</label>
<input id="node_parents_35" name="node[parents][]" type="checkbox" value="35" /><label>Testy</label>
<input id="node_parents_37" name="node[parents][]" type="checkbox" value="37" /><label>Resty</label>
<input id="node_parents_36" name="node[parents][]" type="checkbox" value="36" /><label>Mesty</label>
<input name="node[parents][]" type="hidden" value="" />
</div>
When I fill out 2 of the 4 objects, my log looks like this:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"F1HGLwIjDG3VaIYlnsu7NbROEiWBO8xqJtjh5MreI9E=", "node"=>{"name"=>"Lesty", "parents"=>["13", "35", "", ""], "family_tree_id"=>"2"}, "commit"=>"Update Node", "id"=>"38"}
Rather than it sending the params[:parents] as a 4-element array with 2 empty/nil values, I would rather it just send a 2-element array.
How do I do that?
Try this
params[:node][:parents].reject!{|p| p == ""}
or
params[:node][:parents].reject!(&:blank?)
I don't know if you really need to change what is sent by the form, or if you can just filter out the blank values in the controller before doing anything with them?
If you do want to avoid sending empty values then you will need to do something with javascript on form submit, to alter the params from the form before they are submitted. It's definitely much simpler to let the form submit the blanks and filter them out in the controller, however.
To do it with javascript, you'll need to do something like this: i'm assuming you're using jQuery here. The approach i have chosen is to change the submit button so it calls a function instead of submitting the form. The function disables the empty inputs (thus not including them in the form data) and then submits the form.
Add a js function to the page
<script type="text/javascript">
var processForm = function(){
//disable unchecked checkboxes
$("#myForm input[type=checkbox]").not(":checked").attr("disabled", "disabled");
$("#myForm").submit();
};
</script>
then replace your submit button with this:
<%= button_to_function "Submit", "processForm();" %>

Assign ViewBag value to input checkbox?

I am trying to assign ViewBag value in Boolean to an HTML Input checkbox. It is throwing the following error: Cannot resolve symbol '<%= ViewBag.Solicitation %>'
<input id="chkSolicitation" type="checkbox" name="chkSolicitation"
checked="<%= ViewBag.Solicitation %>" />
I recommend using this, assuming you are using Razor:
#Html.CheckBox("chkSolicitation", (bool)ViewBag.Solicitation)
If you are not using Razor, use this:
<%: Html.CheckBox("chkSolicitation", (bool)ViewData["Solicitation"]) %>

Resources