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'?
Related
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.
I'm trying to create a simple two-page web-app that allows users to search for words, then display their definitions on a new page.
The problem is that when searching for a word, I'm not getting any response back from my model file.
I think the problem is trying to convert the JSON request to a hash, then accessing/displaying the hash in the view (not sure how to go about this).
app/views/index.html.erb
<%= form_tag("/search", method: "get") do %>
<%= text_field_tag :search, params[:search], :class => "search-form" %>
<%= submit_tag nil, :class => "search-glass", :src => "search-icon.png", :type => "image" %>
<% end %>
app/controllers/words_controller.rb
def search
#word = Word.search(params[:search])
render layout: "application", template: "words/index"
end
app/models/word.rb
class Word
require 'rubygems'
require 'json'
attr_accessor :search
def self.search(search)
# Get back JSON response
response = Wordnik.word.get_definitions(search)
# Parse JSON to an array of hashes?
hash_result = JSON.parse(response)
end
end
When searching for a single word, I'm getting the following error (screenshot):
Any help is greatly appreciated.
When you call Wordnik.word.get_definitions(search), an Array of Hashes is returned, not a JSON string. Remove the line:
hash_result = JSON.parse(response)
Also having your API key publicly available on Github is probably not a good idea.
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}") }"} %>
How can I add hash map element with a key that contains "-"?
Like this:
<%= button_to_function 'Cancel','cancelRemove("cancelEmail")', :data-dismiss=>'modal', :class=>'btn' %>
I get an error:
undefined local variable or method 'dismiss' for #<ActionView::Base:0x3482fed>
While :'data-dismiss' works, with data attributes you can also do
:data => { :dismiss => 'modal' }
Additional data-prefixed html attributes can be included in the same hash. So for example on another link you might do:
:data => { :remote => true, :method => 'delete' }
which would add to the link the html attributes data-remote="true" data-method="delete".
While the hash syntax is less compact for a single attribute, it's nice when you've got more than one html5 data attribute. And it's arguably a bit more rails-ish.
Just rename it to:
<%= button_to_function 'Cancel','cancelRemove("cancelEmail")', :'data-dismiss'=>'modal', :class=>'btn' %>
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.