I am in the process of integrating my custom shopping cart with a payment gateway(Paypal).
I need to submit my items with the use of hidden fields, but when I submit the form the hidden fields seems to be missing. (According to the the payment gateway my cart is empty)
I know it might be a simple problem, but I've been going in circles for the last couple of hours.
The code that I generate the hidden fields with:
<% for(i=0,l=products.length;i<l;++i) { p = products[i]; %>
<input type="hidden" name= "item_name_"+<% i %> value="beach ball">
<input type="hidden" name="amount_1" value="15">
<% } %>
Any help would be appreciated.
Thanks
Yeah, it's quite simple :)
<input type="hidden" name="item_name_<%= i %>" value="beach ball" />
FYI:
<% i %> evaluate
<%= i %> interpolate
<%- i %> escape
Related
With rails 5.1.5
I have this in a view:
<%= form_for(:date_filter, method: 'get') do |f| %>
<%= f.date_field(:travel_date , value: #travel_date) %>
<%= f.submit 'Time Travel now' %>
<% end %>
The genrated html is:
<form action="/games" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓">
<input value="2018-03-16" type="date" name="date_filter[travel_date]" id="date_filter_travel_date">
<input type="submit" name="commit" value="Time Travel now" data-disable-with="Time Travel now">
</form>
Everythins is working correctly but when I press submit I have this url
http://localhost:3000/games?utf8=%E2%9C%93&date_filter%5Btravel_date%5D=2018-03-16&commit=Time+Travel+now
Why utf8 and commit=Time+Travel+now are there?
How to remove it?
Not sure why you need to remove these two params from your form. These are creaed by rails from_for and submit tag.
utf-8 here to support Internet Explorer 5 and encourage it to use UTF-8 for all forms. For more clarification you can look at here.
If to still want to remove utf-8 params, you need to create your own html form without using rails form_for helper.
Removing commit params is more easier. Just change your submit tag from:
<%= f.submit 'Time Travel now' %>
to
<%= f.submit 'Time Travel now', :name => nil %>
I am trying to pass a value which was retrieved from database to a jsp page using a HTML form, but i think am failing to pass the result set value into the hidden input tag which sends to next page.i have included code related to the question.please help
inv.jsp: This is the code which retrieves data from data base and it displays table table perfectly no problem there.
while(resultset.next()){ %>
<TR> <% int gen=resultset.getInt(1); %>
<TD> <%= gen %></TD>
<TD> <form action="invgen.jsp" method="get"><input name="gen" type="hidden"><input value="Generate" type="submit"></form></TD>
</TR>
invgen.jsp: This part is failing to get the parameter passed by form.
String invno = request.getParameter("gen");
if(invno!=null){
String query="SELECT invno,invdate,mode,candidate,invamnt,sapid,DOJ,fctc,locn,band,skill,srvctax,sbc,total,gtotal from allinvoice where invno='"+invno+"'";
statement=conn.createStatement();
resultset=statement.executeQuery(query);
change code like this.
<TD>
<form action="invgen.jsp" method="get">
<input name="gen" type="hidden" value="<%=resultset.getInt(1)%>">
<input value="Generate" type="submit"></form>
</TD>
You must have to pass value in hidden field to get value in form action page as below.
<TR>
<% int gen=resultset.getInt(1); %>
<TD> <%= gen %></TD>
<TD>
<form action="invgen.jsp" method="get">
<input name="gen" type="hidden" value="hiddenvalue">
<input value="Generate" type="submit">
</form>
</TD>
</TR>
I cannot type code but you should put the resultset in the request as a parameter.
In the next page you have access through request.getParameter("nameOfObject");
By the way: use a servlet to Query a Database an use the dispatcher to forward the result to the specific page.
Hope this helps.
Thanks to #AJ93 solved my problem. just had to do a minor tweak inside for
<input name="gen" type="hidden" value="<%= resultset.getInt(1) %>">
it works perfectly.
I am trying to build a form that is going to suggest medicine name from database but will send ID of chosen medicine to create relation in my database.
I used autocomplete gem which works great, I also used their hint for getting ID out of elements name and this works fine as well but only to the point where there is ONE INPUT element on form. In my case I need 5 inputs and because of my code - it keeps overwritting element called my_medicine_id which causes that only one - last - element is being saved. Can you guys think of any solution for dynamically changing field name?
My PrescriptionsController
[...]
def new
#prescription =Prescription.new
5.times { #prescription.relations.build }
end
[...]
My view
[...]
<ol>
<%= f.fields_for :relations do |builder| %>
<%= builder.hidden_field :medicine_id, :id => "my_medicine_id" %>
<%= builder.autocomplete_field :medicine_name, autocomplete_medicine_name_relations_path, :id_element => '#my_medicine_id' %>
<% end %>
</ol>
[...]
generates final html:
<input id="my_medicine_id" type="hidden" rows="5" name="prescription[relations_attributes][0][medicine_id]"></input>
<input id="prescription_relations_attributes_0_medicine_name" type="text" rows="5" name="prescription[relations_attributes][0][medicine_name]" data-id-element="#my_medicine_id" data-autocomplete="/relations/autocomplete_medicine_name"></input>
<input id="my_medicine_id" type="hidden" rows="5" name="prescription[relations_attributes][1][medicine_id]"></input>
<input id="prescription_relations_attributes_1_medicine_name" type="text" rows="5" name="prescription[relations_attributes][1][medicine_name]" data-id-element="#my_medicine_id" data-autocomplete="/relations/autocomplete_medicine_name"></input>
<input id="my_medicine_id" type="hidden" rows="5" name="prescription[relations_attributes][2][medicine_id]"></input>
<input id="prescription_relations_attributes_2_medicine_name" type="text" rows="5" name="prescription[relations_attributes][2][medicine_name]" data-id-element="#my_medicine_id" data-autocomplete="/relations/autocomplete_medicine_name"></input>
<input id="my_medicine_id" type="hidden" rows="5" name="prescription[relations_attributes][3][medicine_id]"></input>
<input id="prescription_relations_attributes_3_medicine_name" type="text" rows="5" name="prescription[relations_attributes][3][medicine_name]" data-id-element="#my_medicine_id" data-autocomplete="/relations/autocomplete_medicine_name"></input>
<input id="my_medicine_id" type="hidden" rows="5" name="prescription[relations_attributes][4][medicine_id]"></input>
<input id="prescription_relations_attributes_4_medicine_name" type="text" rows="5" name="prescription[relations_attributes][4][medicine_name]" data-id-element="#my_medicine_id" data-autocomplete="/relations/autocomplete_medicine_name"></input>
So as you can see each time it overwrites element data-id-element="#my_medicine_id".
Solution by OP.
Solution found - moved inside of fields_for to a partial and used f.options[:child_index].
Fixed view file:
[...]
<%= f.fields_for :relations do |builder| %>
<%= render 'child_form', :f => builder %>
<% end %>
[...]
And partial _child_form.html.erb
<% #it=f.options[:child_index] %>
<%= f.hidden_field :medicine_id, :id => "my_medicine_id#{#it}" %>
<%= f.autocomplete_field :medicine_name, autocomplete_medicine_name_relations_path, :id_element => "#my_medicine_id#{#it}" %>
I want to add a data attribute to a text field tag
<%= text_field_tag(:address) %>
which produces
<input id="address" type="text" name="address") %>
to make it have the HTML of
<input id="address" type="text" name="address" data-clob="ccc") %>
so I tried
<%= text_field_tag(:address, data:{clob: 'ccc'}) %>
but it was added as the actual value instead of an attribute, i.e.
<input id="address" type="text" value="{:data=>{:clob=>"ccc"}}" name="address"></input>
even though for a text field label I had used
<%= label_tag(:address, t("ui.reservations.between_now_and_param",
param: #start_date.strftime( time_format)), data:{blob: 'bbb'})%>
as detailed in How to add HTML5 data- attributes to a rails form label tag?
How can I add it as an attribute?
The answer was that a second parameter was required for the value and using "" or ,, didn't work but but using nil did - caused no "value=" attribute to be generated, e.g.
<%= text_field_tag(:address, nil, data:{clob: 'ccc'}) %>
Though I need to see if that works if this form is redisplayed on error...
I am reading the guide on form helpers http://guides.rubyonrails.org/form_helpers.html but I'm not sure whether I actually need to write my webpages this way..
I have an HTML form like this written in new.html.erb
<form action="/posts" method="post">
<label for="title">Test</label>
<input id="title"/>
<input type="submit" value="submit"/>
</form>
(The posts page is from the Getting Started guide)
When I submit the request, the server throws an ActionController::InvalidAuthenticityToken exception.
So I changed it to
<%= form_for :post, url: {action: "create"} do |f| %>
<label for="title">Test</label>
<input id="title"/>
<input type="submit" value="submit"/>
<% end %>
And now I have the same form, except the server now accepts the request.
Are form helpers the proper way to develop forms in Rails views?
Generally, yes.
"Rails way" would have you rewrite this as:
<%= form_for Post.new do |f| %>
<%= f.label :title, "Test" %>
<%= f.input :title %>
<%= f.submit %>
<% end %>
Your attempt to use the straight HTML tag was prevented by the CSRF protection that Rails uses on all of its non-GET requests. You must use the Rails form tags or remove CSRF protection to avoid that error.