Custom helper - dependencies link_to remote - ruby-on-rails-3

I'm currently writing a custom form helper based on bootstrap and I have problem with dependencies (I think):
class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::OutputSafetyHelper
alias :super_collection_select :collection_select
def collection_select_append(method, collection, value_method, text_method, options = {}, html_options = {})
('<div class="control-group' + has_error(object.errors[method]) + '">' +
label(method, :class => "control-label") +
'<div class="controls">' +
'<div class="input-append">' +
super_collection_select(method, collection, value_method, text_method, objectify_options(options), #default_options.merge(html_options)) +
'<span class="add-on">' +
link_to(raw("<i class\"icon-white icon-ok\"></i>"), {:controller => "myController", :action => "myAction"}, :method => "post", :remote => true, :html => { :val1 => "val1", :val2 => "val2" } ) +
'</span>' +
'</div>' +
get_error_message(object.errors[method]) +
'</div>' +
'</div>').html_safe
end
end
This is returning an error:
undefined local variable or method `controller' for #<..>

I think that you missed the :url option for controller in this:
link_to(raw("<i class\"icon-white icon-ok\"></i>"), {:controller => "myController", :action => "myAction"}, :method => "post", :remote => true, :html => { :val1 => "val1", :val2 => "val2" } )
TRY:
class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::OutputSafetyHelper
alias :super_collection_select :collection_select
def collection_select_append(method, collection, value_method, text_method, options = {}, html_options = {})
('<div class="control-group' + has_error(object.errors[method]) + '">' +
label(method, :class => "control-label") +
'<div class="controls">' +
'<div class="input-append">' +
super_collection_select(method, collection, value_method, text_method, objectify_options(options), #default_options.merge(html_options)) +
'<span class="add-on">' +
link_to(raw("<i class\"icon-white icon-ok\"></i>"), :url => {:controller => "myController", :action => "myAction"}, :method => "post", :remote => true, :html => { :val1 => "val1", :val2 => "val2" } ) +
'</span>' +
'</div>' +
get_error_message(object.errors[method]) +
'</div>' +
'</div>').html_safe
end
end

Related

Netzke column locking

I saw a feature in Ext JS document - http://dev.sencha.com/deploy/ext-4.0.1/examples/grid/locking-grid.html. We can lock a column by set the option "locked" according the document, but when I did this in Netzke Grid, the grid disappears. Is there any specific method to achieve this. Below the sample code is given,
def configure(c)
super
c.model = "Product"
c.title = "Product List"
c.columns = [
{
:name => :name,
:text => "Name",
:read_only => true,
:locked => true
},
{
:name => :price,
:text => "Price"
},
{
:name => :date,
:text => "Date"
}
]
end

Rails 3 : In submit button :disable_with is not working

In this following code :disable_with is not working in rails 3. But it is perfectly working in rails 2.2
What is wrong in this code?
<%= submit_tag "Save", :class => "buttons",
:disable_with => "Processing",
:id => 'save_btn' %>
Since version 3.2.5 you should not use :disable_with.
<%= submit_tag "Save", 'data-disable-with' => "Processing", :class => "buttons", :id => 'save_btn' %>
or
<%= submit_tag "Save", data: { disable_with: "Processing" }, :class => "buttons", :id => 'save_btn' %>
Check the following in your raw html:
the submit button has data-disable-with attribute
your page includes jQuery js. For me I have:
https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
you page includes jquery_ujs js. For me I have:
/javascripts/jquery_ujs.js
You can read more on https://github.com/rails/jquery-ujs. Different installations for different rails versions.
I had a similar issue on Safari 8.0.6 and latest rails-ujs. Here's my approach of solving this issue:
%button{ class: 'btn btn-action js-submit', data: { method: :put, 'href' => '...', disable_with: "<i class='icon icon-spinner icon-spin'></i>".html_safe }} Submit
$('.js-select-plan').click(function(event) {
event.preventDefault();
var $target = $(this),
href = $target.data('href'),
method = $target.data('method'),
formTarget = $target.attr('target'),
csrfToken = $('meta[name=csrf-token]').attr('content'),
csrfParam = $('meta[name=csrf-param]').attr('content'),
form = $('<form method="post" action="' + href + '"></form>'),
metaData = '<input name="_method" value="' + method + '" type="hidden" />';
if (csrfParam !== undefined && csrfToken !== undefined) {
metaData += '<input name="' + csrfParam + '" value="' + csrfToken + '" type="hidden" />';
}
if (formTarget) {
form.attr('target', formTarget);
}
// disable button/link element
var contentMethod = $target.is('button') ? 'html' : 'val';
var replacement = $target.data('disable-with');
$target.data('ujs:enable-with', $target[contentMethod]());
if (replacement !== undefined) {
$target[contentMethod](replacement);
}
$target.prop('disabled', true);
form.hide().append(metaData).appendTo('body');
setTimeout(function(){
form.submit();
}, 50);
});
This works like a charm in all browsers. For better flexibility you might want to wrap this in a helper.

Netzke basepack. Need advice with multi uploading fields

Is there any easy way to include the multiupload feature to NetzkeFormView or GridView(AddInForm)?
My current image uloading field with carrierwave is:
{:name => :image_link, :xtype => :displayfield, :display_only => true, :getter => lambda { |r| %Q(<a href='#{r.image.url}'>Download</a>) if r.image.url }},
{:name => :image, :field_label => "Upload image", :xtype => :fileuploadfield, :getter => lambda { |r| "" }, :display_only => true}

rails3.1 and formtastic 2.0.0.rc2 - undefined method `inputs'

I am using rails 3.1.0.rc3 with formtastic 2.0.0.rc2 and I am getting this error -
undefined method `inputs' for #<ActionView::Helpers::FormBuilder:0x000001059c2fb0>
Here is the block of code
= form_tag '#', :class => 'formtastic' do
= fields_for CustomFields::Field.new, :builder => Formtastic::Helpers::FormHelper.builder do |g|
= g.inputs :name => :attributes do
= g.input :_alias
= g.input :hint
= g.input :text_formatting, :as => 'select', :collection => options_for_text_formatting, :include_blank => false, :wrapper_html => { :style => 'display: none' }
= g.input :target, :as => 'select', :collection => options_for_association_target, :include_blank => false, :wrapper_html => { :style => 'display: none' }
Is this a bug ?
Thanks, Alex
You are trying to use a formtastic method here. When you are actually in a block for Rails's form builder.
You need semantic_form_for and formtastic in your Gemfile to use f.inputs for example..

Submit PayPal data encrypted from the code

I'm working with Ruby On Rails 3, and I would like to do the following, but from the code behind:
<% form_tag "https://www.sandbox.paypal.com/cgi-bin/webscr" do %>
<%= hidden_field_tag :cmd, "_s-xclick" %>
<%= hidden_field_tag :encrypted, #cart.paypal_encrypted(products_url, payment_notifications_url) %>
<p><%= submit_tag "Checkout" %></p>
<% end %>
I've tried this in my Cart model, but it's not redirecting anywhere, and I don't know what to do:
PAYPAL_CERT_PEM = File.read("#{Rails.root}/certs/paypal_cert.pem")
APP_CERT_PEM = File.read("#{Rails.root}/certs/app_cert.pem")
APP_KEY_PEM = File.read("#{Rails.root}/certs/app_key.pem")
PANEL = 'sandbox.paypal.com'
PATH = '/cgi-bin/webscr'
USERAGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1'
def paypal_url(order_id, return_url, notify_url)
http = Net::HTTP.new(PANEL, 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
# GET request -> so the host can set cookies
resp, data = http.get2(PATH, {'User-Agent' => USERAGENT})
cookie = resp.response['set-cookie'].split('; ')[0]
values = {
:cmd => '_s-xclick',
:encrypted => paypal_encrypted(order_id, return_url, notify_url)
}
#headers = {
'Cookie' => cookie,
'Referer' => 'https://'+PANEL+PATH,
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => USERAGENT
}
resp, data = http.post2(PATH, values.to_query, #headers)
end
def paypal_encrypted(order_id, return_url, notify_url)
values = {
:business => 'seller_1234111143_biz#asciicasts.com',
:cmd => '_cart',
:upload => 1,
:return => return_url,
:invoice => order_id.to_s,
:notify_url => notify_url,
:currency_code => "USD"
}
items.each_with_index do |item, index|
values.merge!({
"amount_#{index + 1}" => item.unit_price,
"item_name_#{index + 1}" => item.product.title,
"item_number_#{index + 1}" => item.product.id + Time.now.to_i,
"quantity_#{index + 1}" => item.quantity.to_i
})
end
encrypt_for_paypal(values)
end
def encrypt_for_paypal(values)
signed = OpenSSL::PKCS7::sign(OpenSSL::X509::Certificate.new(APP_CERT_PEM), OpenSSL::PKey::RSA.new(APP_KEY_PEM, ''), values.map { |k, v| "#{k}=#{v}" }.join("\n"), [], OpenSSL::PKCS7::BINARY)
OpenSSL::PKCS7::encrypt([OpenSSL::X509::Certificate.new(PAYPAL_CERT_PEM)], signed.to_der, OpenSSL::Cipher::Cipher::new("DES3"), OpenSSL::PKCS7::BINARY).to_s.gsub("\n", "")
end
If you're wondering why I can't just use the html form, that's because I let users choose between more than one payment option, using radio fields, and once they have selected one, they will click on the "Submit Order" button, generating the respective movements in my database, before redirecting to the payment method.