When I submit a remote form in rails, the form POST duplicate data, even though if I manually serialize the form using jQuery shows that there are no duplicates.
What could be causing this?
*Normally this wouldn't be a problem, but one of the form data is an array, as a result there are duplicate value in the submitted array.
Result from chrome's network inspector
Result from jQuery
UPDATE
Here is the gist of my form (removing the html markups):
<% #order.available_payment_methods.each do |method| %>
<%= radio_button_tag "order[payments_attributes][][payment_method_id]", method.id, method == #order.available_payment_methods.first %>
<%= Spree.t(method.name, :scope => :payment_methods, :default => method.name) %>
<%= render :partial => "spree/checkout/payment/#{method.method_type}", :locals => { :payment_method => method } %>
<% end %>
# partial
<% param_prefix = "payment_source[#{payment_method.id}]" %>
<%= label_tag "name_on_card_#{payment_method.id}", Spree.t(:name_on_card) %><span class="required">
<%= text_field_tag "#{param_prefix}[name]", "#{#order.billing_firstname} #{#order.billing_lastname}", { id: "name_on_card_#{payment_method.id}", class:'form-control'} %>
<% options_hash = Rails.env.production? ? {:autocomplete => 'off'} : {} %>
<%= text_field_tag "#{param_prefix}[number]", '', options_hash.merge(:id => 'card_number', :class => 'required cardNumber form-control', :size => 19, :maxlength => 19, :autocomplete => "off") %>
<%= text_field_tag "#{param_prefix}[expiry]", '', :id => 'card_expiry', :class => "required cardExpiry form-control", :placeholder => "MM / YY" %>
<%= text_field_tag "#{param_prefix}[verification_value]", '', options_hash.merge(:id => 'card_code', :class => 'required cardCode form-control', :size => 5) %>
<% if #order.bill_address %>
<%= fields_for "#{param_prefix}[address_attributes]", #order.bill_address do |f| %>
<%= render :partial => 'spree/address/form_hidden', :locals => { :form => f } %>
<% end %>
<% end %>
<%= hidden_field_tag "#{param_prefix}[cc_type]", '', :id => "cc_type", :class => 'ccType' %>
UPDATE Submitting the form without remote: true fixes the problem (BUT I NEED IT!)
Add onsubmit="return false();" on your form it will make sure that form is not submited through default form submission. It will be submitted through ajax as you are adding remote: true
Related
I'm new in rails and I want retrieve a selected value to a field in order to adapt an other field in the same form.
<% if #issue.safe_attribute? 'tracker_id' %>
<p><%= f.select :tracker_id, #issue.project.trackers.collect {|t| [t.name, t.id]}, {:required => true},
:onchange => "updateIssueFrom('#{escape_javascript project_issue_form_path(#project, :id => #issue, :format => 'js')}')" %></p>
<% end %>
<% if #issue.safe_attribute? 'parent_issue_id' %>
<p id="parent_issue"><%= f.text_field :parent_issue_id, :size => 10, :required => #issue.required_parent_issue_id?(*the tracker_id selected*) %></p>
<% end %>
How to handle the value the tracker_id selected ?
I have been trying to pass what round the users intends to go to by which button they click through a hidden field that passes in the round number.
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '1', :class =>'round1' %>
<%= f.submit 'Picks', :class => 'round1' %>
<%= f.hidden_field :round, :value => '2', :class =>'round2' %>
<%= f.submit 'Picks', :class => 'round2' %>
<% end %>
With this code I constantly get 2 passed through as the round in my pick_page. It is obviously skipping over the first hidden field. How can I get it so 'f.submit' submits the round number that is associated with its class.
We just had the same problem! We fixed it creating 2 diferent forms right next to each other.
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '1', :class =>'round1' %>
<%= f.submit 'Picks', :class => 'round1' %>
<%= f.hidden_field :round, :value => '2', :class =>'round2' %>
<%= f.submit 'Picks', :class => 'round2' %>
<% end %>
Change it to
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '1', :class =>'round1' %>
<%= f.submit 'Picks', :class => 'round1' %>
<% end %>
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '2', :class =>'round2' %>
<%= f.submit 'Picks', :class => 'round2' %>
<% end %>
You can just remove the hidden fields and add a name and value attributes on your submit button.
I am trying to implement Ajax search. When the user enters the query into the text field, the data should display below it and if there are not results, a new entry should be created in the database.
I have a button next to the text field. When the user presses the button, the data in the text field should be posted to the create method which creates a new record in the database. How can I do that?
Now i am using this:
<%= form_tag song_requests_path, :method => 'get', :id => "requests_search", :class => 'well form-horizontal' do %>
<p>
<h2> I Love To Sing ...</h2>
<%= text_field_tag :search1, params[:search1], :class => 'text_field span4',:placeholder=>"Song Title", :maxlength => "250" %>
<%= text_field_tag :search2, params[:search2], :class => 'text_field span3', :placeholder=>"Album", :maxlength => "250" %>
</p>
<% end %>
<%=form_for( :song_request, :remote => true) do |f| %>
<center>
<%= f.hidden_field :title, :value => "#{params[:search1]}" %>
<%= f.hidden_field :album, :value => "#{params[:search2]}" %>
<%= f.submit " Request ", :class => 'btn btn-primary'%>
</center>
<%end%>
Functionally its working perfect, but i can't place the request button besides the text field...Thank u....
I have a web form to which I want to take a parameter from the URL and add it to the web form as a hidden field on the form.
<%= form_tag(:action => 'create') do |f| %>
<%= text_field_tag :email,"Your email address...", :class => "text", :id => "email", :name => 'email',
:onFocus => "change(this,'#222222'); this.value=''; this.onfocus=null;", :size => "26" %>
<%= hidden_field_tag :ref_code, :id => 'ref_code', :name => 'ref_code', :value => params[:ref_code] %>
<%= submit_tag "Enter To Win", :class => "button-positive submit" %>
<% end %>
If I just do a:
<%= params[:ref_code] %>
I get the value I want which is a five character alphanumeric, however when I use it in the form, I get the full hash description:
{:id=>"ref_code", :name=>"ref_code", :value=>["k53e5", "home", "index2"]}
Why? I tried .values, .to_s, and other ways of getting by key and I always get the full hash instead of just the value. What am I doing wrong? Thanks.
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/hidden_field_tag
hidden_field_tag(name, value = nil, options = {})
<%= hidden_field_tag :ref_code, params[:ref_code], { :id => 'ref_code', :name => 'ref_code' } %>
'shared/subscription' %>
To call this partial view:
<% form_for(:subscription, :url => city_subscriptions_path(#city)) do |form| %>
<%= form.hidden_field :city_id, :value => #city.id %>
<%= form.text_field :email, :size => 30 %>
<%= form.submit "Email Me" %>
<% end %>
Since I am using this partial view on different places, how do I alter the caller so it will pass a hash for the form_for helper? So it would be like this when the helper is called:
<% form_for(:subscription, :url => city_subscriptions_path(#city), :html => {:id => 'main_input' }) do |form| %>
<%= form.hidden_field :city_id, :value => #city.id %>
<%= form.text_field :email, :size => 30 %>
<%= form.submit "Email Me" %>
<% end %>
<%= render :partial => "shared/subscription", :locals => {:foo => "bar", :foofoo => ["bar", "bar"]}
In your partial view, use them:
<%= foo #this outputs "bar" %>
<%= foofoo.to_s %>