tinymce in ajax call - how to get contents - ruby-on-rails

this is what I have in my view:
<% form_remote_tag :url => { :controller => 'comments', :action => "create", :post_id => "#{#post.id}"}, :html => {:id => 'comment_form' },
:before => "tinyMCE.triggerSave(true,true);" do %>
<%= label_tag 'Comment' %><br/>
<%= text_area_tag :comment_body, nil,:rows => 10, :cols => 100 %><br/>
<p style="margin-top: 10px;">
<%= submit_tag 'Add',:id => 'btnCommentSave' %>
</p>
<% end %>
Tinymce editor is displayed properly.
In my controller:
how to get the contents of the text area?
I am expecting the contents in params[:comment_body] and I am not seeing it?
I tried doing this also,
$('#btnCommentSave').click( function(){
tinyMCE.triggerSave(true,true);
$('#comment_form').submit();
});
What am I missing?
Thanks

try this and see if it makes a difference
tinyMCE.get(id_of_the_text_area).save()

Related

Rails remote form submitting duplicate form data

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

Rails: Getting a Value from a Select Tag

<%= form_tag 'select_domain', :url => administer_admin_domain_path(:id), :method => :get do %>
<%= select_tag "id", options_from_collection_for_select(#domains, :id, :caption), :onchange => "this.form.submit();" %>
<% end %>
I want the option selected's id to be the :id inside my form's url when it submits, is this possible?
How about:
<%= form_tag 'select_domain', :url => administer_admin_domains_path, :method => :get do %>
<%= select_tag "id", options_from_collection_for_select(#domains, :id, :caption), :onchange=>"this.form.action=this.form.action + '/' + this.value; this.form.submit(); %>
<% end %>
I didn't test the code. Just giving u some idea which might help. : )

Why it won't detect that the request is come from edit page?

At the edit page, if I leave captcha empty, and press "save button", it takes me to #new page and says 'Wrong Captcha!'.
In fact, it should take me to edit page again.
Why my controller won't detect that the request was come from edit page?
comunity_topics_controller.rb
before_filter :simple_captcha_check, :only => [:update, :create]
def simple_captcha_check
if !simple_captcha_valid?
flash[:error] = 'Wrong Captcha!'
if request.put? # We came from an edit request
#community_topic = CommunityTopic.find(params[:id])
#community_topic.attributes = params[:community_topic]
render :action => :edit
elsif request.post? # We came from a new request
#community_topic = CommunityTopic.new params[:community_topic]
render :action => :new
end
end
end
_form.html.erb
<%= form_for :community_topic, url: community_topic_index_url, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :title, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :title, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :body, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :body, :class => 'text_area' %>
</div>
</div>
<div class="control-group">
<div class="controls">
<%= show_simple_captcha(:label => "human authentication") %>
</div>
</div>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
community_topic_index_path, :class => 'btn' %>
</div>
<% end %>
UPDATE:
routes.rb
resources :communities, :path => "shop", do
resources :community_topics, :path => "topic", :as => :'topic'
end
New Update:
rake routes | grep community_topic
community_topic_index GET /shop/:community_id/topic(.:format) community_topics#index
POST /shop/:community_id/topic(.:format) community_topics#create
new_community_topic GET /shop/:community_id/topic/new(.:format) community_topics#new
edit_community_topic GET /shop/:community_id/topic/:id/edit(.:format) community_topics#edit
community_topic GET /shop/:community_id/topic/:id(.:format) community_topics#show
PUT /shop/:community_id/topic/:id(.:format) community_topics#update
DELETE /shop/:community_id/topic/:id(.:format) community_topics#destroy
Because the request method is post and not put. To use a put request you should write:
<%= form_for #community_topic, url: community_topics_url,
#MKK <%= form_for :community_topic, url: community_topic_url(#community_topic), :html => { :class => 'form-horizontal' } do |f| %> but check it within your rake routes

Updating paperclip avatar in multipart simple_form

I would like to create an edit page for the below form. The problem is that when the user browses to the edit page the brand_name and name are pre-filled, but the image upload field shows 'no file chosen' even when an avatar exists for the 'style'. Please let me know if there is some way to remedy this. Thanks!
My Edit Form:
<%= simple_form_for #style, :html => { :class => 'form-horizontal' }, :remote => true do |m| %>
<%= m.input :brand_name, :label => 'Brand', :placeholder => 'Brand' %>
<%= m.input :name, :label => 'Style', :placeholder => 'Style' %>
<%= m.input :avatar, :label => "Image" %>
<div class="form-actions" style = "background:none">
<%= m.submit nil, :class => 'btn btn-primary' %>
<%= link_to 'Cancel', styles_path, :class => 'btn' %>
</div>
<% end %>
Just implemented this yesterday. Make a custom input in /app/inputs
class AvatarInput < SimpleForm::Inputs::FileInput
def input
out = '' # the output string we're going to build
# check if there's an uploaded file (eg: edit mode or form not saved)
if object.send("#{attribute_name}?")
# append preview image to output
# <%= image_tag #user.avatar.url(:thumb), :class => 'thumbnail', id: 'avatar' %>
out << template.image_tag(object.send(attribute_name).url(:thumb), :class => 'thumbnail', id: 'avatar')
end
# append file input. it will work accordingly with your simple_form wrappers
(out << #builder.file_field(attribute_name, input_html_options)).html_safe
end
end
Then you can do
<%= f.input :avatar, :as => :avatar %>
This is all I needed for this to work (in haml):
=simple_form_for #user, :html => {:multipart => true } do |f|
=f.file_field :image
The code for new/edit views from paperclip's github page looks like this:
<%= form_for :user, #user, :url => user_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<% end %>
So maybe you should try m.file_field and include :html => { :multipart => true } as well? Though I personally prefer Attachment-Fu.

How to add selection box dynamically in rails 3.1.0?

Here is a partial form _standards.html.erb we want to add to the view dynamically:
<p><%= f.association :standards, :collection => Standard.active_std.all(:order => 'name'), :label_method => :name, :value_method => :id :include_blank => true %></p>
Here is the view form itself _form_new.html.erb which calls _standards.html.erb:
<%= simple_form_for #rfq do |f| %>
<div id="std">
<%= render :partial => 'standards/standards', :collection => #rfq.standards, :locals => { :f => f } %>
</div>
<%= link_to_function("Add std", nil) do |page| %>
page.insert_html :bottom, 'std', :partial => 'standards/standards', :object => #rfq.standards.build
<% end %>
<%= f.button :submit, 'Save' %>
<% end %>
This solution did not work as link_to_function was not reacting to click by loading the _standards.html.erb. This solution seems out of date and does not work in rails 3.1.0. I am wondering if there is other solution to add dynamic content to the view page in rails 3.1.0. If you do, please don't hesitate to post. Thanks.
I know this answer is coming months after you need it but hopefully this helps someone else who stumbles across this question. Here is how you can dynamically add a selection box unobtrusively for your example.
_form_new.html.erb
<%= simple_form_for #rfq do |f| %>
<a id="add-selection-box" href="#">Add Selection Box</a>
<div id="std"></div>
<script type="text/javascript">
$(document).ready(function(){
$("#add-selection-box").click(function() {
$("#std").append("<%= escape_javascript(render(:partial => 'standards/standards', :collection => #rfq.standards, :locals => { :f => f })) %>");
});
});
</script>
<% end %>
Here is how you can do it using link_to_function
_form_new.html.erb
<%= simple_form_for #rfq do |f| %>
<%= add_selection_box "Add Selection Box", #rfq.standards, f %>
<div id="std"></div>
<% end %>
helpers/my_form_helper.rb
module MyFormHelper
def add_selection_box(name, collection, form)
page = %{
partial = "#{escape_javascript(render(:partial => "standards/standards", :collection => collection, :locals => { :f => form }))}";
$("#std").append(partial);
}
link_to_function name, page
end
end

Resources