Passing local variable to a partial in rails 2.x - ruby-on-rails

I have the following method in my controller:
def update_fees_collection_dates_voucher
#batch = Batch.find(params[:batch_id])
#dates = #batch.fee_collection_dates
render :update do |page|
page.replace_html "fees_collection_dates", :partial =>"fees_collection_dates_voucher"
end
end
the method calls the following partial in _fees_collection_dates_voucher.html.erb file:
<%= select :fees_submission, :dates_id, #dates.map { |e| [e.full_name, e.id]},{:prompt => "#{t('select_fee_collection_date')}"},{:onChange => "#{remote_function( :url => {:action => "load_fees_voucher"},:with => "'date='+value+'&batch_id='+#batch.id") }"} %>
The partial makes a remote call to the load_fees_voucher method when a selection list value is selected or changed. However, I'm unable to pass the information in the #batch instance variable (from my original method) to the remote method via the partial. If I change the #batch.id to a hard-coded value in the last line (under :with) the code runs fine but not in the current format. Is there a different procedure to access instance variables in partials ? Thanks!

You can use the same instance variable in the partials.
Try this,
<%= select :fees_submission, :dates_id, #dates.map { |e| [e.full_name, e.id]},{:prompt => "#{t('select_fee_collection_date')}"},{:onChange => "#{remote_function( :url => {:action => "load_fees_voucher"},:with => "'date='+value+'&batch_id='+#{#batch.id}") }"} %>

Related

Output content from render_to_string using global variable

I am inside a Controller where I have a method that saves information inside global variables like the following
#var = "test"
Now when I want to render the view file I am using the following code
MyController.new.render_to_string(:template => 'folder/file.erb', :layout => false)
The issue I'm having is that inside the file.erb it cant access the variable #var for some reason. The code goes as follows:
<% #var.each do |v| %>
<% end if !#var.nil? %>
Which is returning null as it's content. How can I fix this?
I think you should pass it as a local:
MyController.new.render_to_string(:template => 'folder/file.erb', :layout => false, :locals => { :var => #var )
Then it will be accessible as a local variable var (without #).
If you want to do some Rails magic, you should point an action where your #var is defined:
MyController.new.render_to_string(:template => 'folder/file.erb', :layout => false, :action => 'your_action_name')
so Rails will be able to reuse your variable in the view

PayPal integration in Ruby on Rails

I want to add PayPal payment system to my RoR app. For this I did install paypal-sdk-rest gem.
I have a model Feed and inside of index.html.rb, where the route is:
get '/:locale/feed', to: 'feed#index', as: 'feed'
I want to paste the next code:
<%= link_to 'checkout', feed.paypal_url(products_url) %>
And inside of the model feed.rb:
def paypal_url(return_url)
values = {
:business => 'my_paypal_mail#gmail.com',
:cmd => '_cart',
:upload => 1,
:return => return_url,
}
values.merge!({
"amount_1" => unit_price,
"item_name_1" => name,
"item_number_1" => id,
"quantity_1" => '1'
})
"https://www.sandbox.paypal.com/cgi-bin/webscr?" + values.to_query
end
But it prints me, that:
undefined local variable or method `feed' for #<#<Class:0x007f5644b79520>:0x007f5644b89858>
on
<%= link_to 'checkout', feed.paypal_url(feeds_url) %>
What is the problem and how can I fix this error?
UPDATE
I just want to paste button just for payment, to my website with the amount 1$. How can I make it?
Where is feed object defined? If in controller then it should be instance variable like #feed, To access variables defined in controller from views we have to defined them as instance variables like #feed.

Is it possible to pass parameters when using 'def_erb_method'?

My current code is this.
<%= render 'codes/code', {:code => code, :icon_photo => code.community.community_icon} %>
I tried to use 'def_erb_method', so I changed the code to this
<%#= raw render_code(code), {:code => code, :icon_photo => code.community.community_icon} %>
codes_helper.rb
module CodesHelper
extend ERB::DefMethod
def_erb_method('render_code(code)', "#{Rails.root}/app/views/codes/_code.html.erb")
end
However, I got this error back:(
undefined local variable or method `icon_photo'
I cannot pass the parameters when using 'def_erb_method'?

What is the Rails convention for sending form data to a URL dependent on form value?

I would like a form submitted at the url
/index/fruit
to submit the form data to
/index/:identifier
where :identifier is determined by a value of the form
What is the rails convention in this instance?
Is there a way to achieve this without a controller level redirect or javascript-updating the submit URL?
routes.rb
match 'smasher(/:action(/:id))', :controller => "customcontroller", :as => :smasher, :defaults => { :action => :index, :id => :fruit }
index.html.erb
<%= semantic_form_for :d, :url => smasher_path, :html => { :method => :get } do |f| %>
... form data ...
<%= f.input :identifier, :as => :hidden %>
<% end %>
My current implementation is similar to this answer
There's isn't really a "convention" for this, but rather one of those things where there's more than one way to do it.
One way that you could do it is still send the form to one and only one action within the controller, but then delegate in the controller which action to go to, like this:
def smasher
if params[:identifier] == 'this'
smash_this!
else
smash_that!
end
end
def smash_this!
# code goes here
end
def smash_that!
# code goes here
end
Heres the javascript version (though technically its all on an erb html template), if you're feeling up to it.
<%= f.input :identifier, :as => :hidden, :onchange => "$(this).setAction()" %>
<script>
// While you can this script block here within your erb template
// but best practice says you should have it included somehow within `<head></head>`
$(function() {
//create a method on the Jquery Object to adjust the action of the form
$.fn.setAction = function() {
var form = $(this).parents('form').first();
var action = form.attr('action')
form.attr('action', action.substr( 0, action.lastIndexOf('/')+1 ) + $(this).val());
}
});
</script>
Heres the pure javascript version:
$(function() {
//create a method on the Jquery Object to adjust the action of the form
$.fn.setAction = function() {
var form = $(this).parents('form').first();
var action = form.attr('action')
form.attr('action', action.substr( 0, action.lastIndexOf('/')+1 ) + $(this).val());
}
//we gotta bind the onchange here
$('input[name="identifier"]').change($.fn.setAction);
});

How to “dynamically add options” to 'form_for'?

I am using Ruby on Rails 3.2.2. In order to implement a "dynamic generated" AJAX style file upload form I would like to "dynamically add options" to the FormHelper#form_for statement if some conditions are meet. That is, at this time I am using code as-like the following (note that I am using the merge method in order to add options to the form_for method):
<%
if #article.is_true? && (#article.is_black? || && #article.is_new?)
form_options = {:multipart => true, :target => "from_target_name"}
else
form_options = {}
end
%>
<%= form_for(#article, :remote => true, :html => {:id => "form_css_id"}.merge(form_options)) do |form| %>
...
<% end %>
However, I think that the above code is too much hijacked.
Is there a better way to accomplish what I am making? For example, can I access from view templates some (unknown to me) instance variable named as-like #form and "work" on that so to change related options as well as I would like? Or, should I state a helper method somewhere? How do you advice to proceed?
BTW: Since the upload process is handled by using a HTML iframe, I am using the remotipart gem in order to implement the AJAX style file upload form - I don't know if this information could help someone...
This looks like a good candidate for a helper method. In your view:
<%= form_for(#article, :remote => true, :html => article_form_options(#article, :id => "form_css_id")) do |form| %>
...
<% end %>
In app/helpers/articles_helper.rb
module ArticlesHelper
def article_form_options(article, defaults = {})
extras = if article.is_true? && (article.is_black? || article.is_new?)
{ :multipart => true, :target => 'form_target_name' }
else
{}
end
defaults.merge(extras)
end
end
Helpers are a good place to keep logic that's too complex for a view but still related to the view.

Resources