I'm currently working on a rails app in which I have a form which is submitted with jquery sexy post and gets back a js response, the code below was working fine but after I added the placeholder attribute for some reason it got broken.
= form_for [#room, #room.messages.new], :html => { :multipart => true } do |f|
= f.text_area :text, :size => "60x1", :class => "field", :placeholder => "say something"
= f.submit "Send"
= f.file_field :image, :class => "img_field"
= link_to "Add Image", "#", :id => "add_imgLink"
The form is being submitted and we get the answer back from the server, though, the js response is not being executed and the form is not being refreshed, any idea what of what could be possible going wrong ?
Thanks!
--
UPDATE:
Just find out that if I replace :placeholder by :value, it does works, but if I don't include :value it doesn't.
UPDATE2:
I found the problem and it wasn't related with jquery, rails or js, one of my coworkers modified sexypost and introduced a bug. Problem fixed.
I'd guess the space in the string. Try url encoding it.
Related
I'm having some trouble rendering validation messages using "remote => true"
I have a partially rendered form for saving an address
<%= render :partial => "registrations/address", :locals => {:address => #user.customer_addresses.build, :is_new => true, :checked => false} %>
In this form, the user clicks this button to create a new address
<%= link_to "Save Address", "javascript://", :remote => true, :data => "new", "data-target" => create_user_address_url, "data-method" => "Post" %>
I have the 'create_address' function set up in the controller (which returns the validation errors), as well as a create_address.js.erb file. However, it doesnt seem like the browser is executing anything in the js file.
It would be great to see the code of js.erb file
But I assume you have escape javascript issue.
Just use j in your render partial method. E.g.
<%= j render('partial_name') %>
You might have forgot to reload the partial which show the error message. After using remote => true you need to reload the partial so that the changes will reflect on that partial.
I have a page That starts with a form. Inside that form, I render another page called information. Inside this render I have another render for a modal. This modal is another form. So at this point i have one nested form. This works great in all browsers except IE9. I think what IE9 is trying to do is it sees when the second form is ending, and it also ends the first form, so everything that is after the nested form is screwed up.
Has anyone else ran into this problem? and how do you fix it?
Parent File (form):
= simple_form_for #form do |f|
#the_form
= render 'information', :f => f
.buttons
%input{:name => "submit", :type => "submit", :value => "SUBMIT"}
%input{:name => "cancel", :type => "submit", :value => "Cancel"}
Render information file:
#information
%fieldset
%legend
Form Title
= f.input :form_id, :url => form_name_path, :label => 'Field Name'
= render 'modal'
(the rest of the code here breaks)...
Render modal file:
.modal.hide.fade
.modalBox
%h3
New Form Name
%a{href: "#", class: "x", title: "Close" : 'data-dismiss' => "modal"}
.diagRepeater
.modal-body
= simple_form_for Form.new, :url => {:controller => :form, :action => :modal_create} do |o|
=o.input :name, :label => 'Name', :required => true
=o.input :form_id, :as => :hidden
It is in this last file that I see the problem. If I comment out the simple_form_for and on, it will work great. If I leave it, it will break the rest of the form.
HTML not support nested form.
You can have several forms in a page but they should not be nested.
As you say: this work great in all browser for me it is miracle because 'You would even have problems making it work in different versions of the same browser' so avoid using that.
Webkit explain why HTML not supporting nested form
bool HTMLParser::formCreateErrorCheck(Token* t, RefPtr<Node>& result)
{
// Only create a new form if we're not already inside one.
// This is consistent with other browsers' behavior.
if (!m_currentFormElement) {
m_currentFormElement = new HTMLFormElement(formTag, m_document);
result = m_currentFormElement;
pCloserCreateErrorCheck(t, result);
}
return false;
}
I am using Rails 4 and Ruby 2 with Carrier Wave along with jQuery Mobile 1.3, and a user has a profile which has a logo. I have tested the backend Carrier Wave stuff in the console and it works.
The problem is in the form helper, it doesn't matter what I do, but a file_field will display and let me choose an image, but it does not come through in the params. If I change it to a field that does not exist eg. =f.file_field :field_not_permitted_in_strong_params it does not fall over and a file picker is visible, if I change that to =f.text_field :field_not_permitted_in_strong_params, only then does rails kick in and complain about the field not existing.
So my problem is basically, I can upload a file client side, but it does not get sent through in the form data, or appear in the params hash, and all my other fields work correctly.
Here is a snippet of the form:
= form_for(#business_profile, :html => {:multipart => true}, :url => business_profile_path, :validate => true) do |f|
=image_tag(#business_profile.logo.url, class: 'business-logo')
%div{:data => {:role => 'fieldcontain'}}
=f.file_field :logo
- # Basic Information
%div{:data => {:role => 'collapsible', :collapsed => 'false'}}
%h3
Basic Information
%div{:data => {:role => 'fieldcontain'}}
= f.label :name, 'Business name:'
= f.text_field :name
%div{:data => {:role => 'fieldcontain'}}
= f.label :address, 'Address:'
= f.text_area :address, class: 'address'
OK after spending hours on the matter, the problem is that jQuery Mobile submits forms with Ajax by default, and files cannnot be submitted with Ajax without using plugins etc.
So the solution is to disable the Ajax like this:
= form_for(#business_profile, :url => business_profile_path, :validate => true, :html => { :'data-ajax' => false }) do |f|
I'm trying to setup a formtastic form with multiple submit actions, following along with Railscast #38. What's the equivalent of this in Formtastic?
<%= submit_tag 'Create' %>
<%= submit_tag 'Preview', :name => 'preview_button' %>
This post gave me hope, but it looks like commit_button was deprecated in 2.1.0, and I can't seem to figure out the new syntax.
Here's my code for what it's worth. I'd like each submit button to go to the same controller, where I will handle them differently:
# Use prepaid credits to checkout
<%= f.action :submit, :as => :button, :label => "Order Critique (1 credit will be spent)", :button_html => { :class => "btn btn-primary", :disable_with => 'Processing...' } %>
# Use credit card to checkout
<%= f.action :submit, :as => :button, :label => "Order Critique ($10)", :button_html => { :class => "btn btn-primary", :disable_with => 'Processing...' } %>
TL;DR: If you use javascript to submit a form, it won't carry over the submit button's name in the commit params.
My problem ended up being the code used in Railscasts Episode #288. This CoffeeScript function gets fired when you submit the form, after the Stripe token checks out:
handleStripeResponse: (status, response) ->
if status == 200
$("#stripe_card_token").val(response.id)
$("#my_form_id")[0].submit()
else
# other stuff
Since javascript is doing the form submission with $("#my_form_id")[0].submit(), the name parameter won't be carried over in the commit params.
My solution was to add a "clicked" attribute to the button that gets clicked...
$('form_input[type=submit]').click ->
$('input[type=submit]', $(this).parents('form')).removeAttr('clicked')
$(this).attr('clicked', 'true')
..and then grab the id attribute of the clicked button populate a hidden field with it:
submitter = $("input[type=submit].clicked=true").attr("id")
I don't particularly like this solution. It feels like my js knows too much, and I don't like depending on js for this sort of thing. Any criticism or better solutions are certainly welcome :)
I have this form and tried many aproches to no avail to repopulate form fields for a rails form_for after submit. Whats the correct way to do it? Maybe just overlook in the docs but no good reference on how to to this :(
The only non working solution is to use the params tag and set the collections default value to that, but if form not submitted this fails too. params[:age_from]
= form_for :people, :url => request.fullpath, :method => :get, :html => { :class => 'form-search' } do |f|
#container_search
.clear
= select_tag(:age_from, options_for_select(18..60)
= select_tag(:age_to, options_for_select(18..60)
= select_tag(:gender, options_for_select(gender)
.clear
.center
= f.submit "Search ยป", :class => "span-4"
It looks like you are missing closing ) on your select_tag lines, you can add selected value by adding another param to your options_for_select:
select_tag(:age_from, options_for_select(18..60, params[:age_from]))