Giving 'TemplateError' can't convert String into Integer - ruby-on-rails-3

I recently transfered my app from Rails2 to Rails3.
The code in 'app/views/distribution/index.html.erb' is like :-
<div style="padding-bottom:10px; padding-left:0px;float:left;display:<%= (!session[:album][#artist.id.to_s].empty? && !session[:album][#artist.id.to_s].nil?)?'block' : 'none' %>" id = "make_payment_enabled">
<%= link_to 'Make Payments',{:action => 'pay', :album=>#album.id}, :class => "button" %>
</div>
It's giving me TemplateError on line :-
<div style="padding-bottom:10px; padding-left:0px;float:left;display:<%= (!session[:album][#artist.id.to_s].empty? && !session[:album][#artist.id.to_s].nil?)?'block' : 'none' %>" id = "make_payment_enabled">
How to resolve the problem ?

Solution 1: In the ERB tag, try putting spaces around the 'or' question mark, i.e. ....nil?) ? 'block....
Solution Better: Do step one, then put that code in a helper. Will really help to clean up your views.
UPDATE:
A few other tips: you will want to switch the order of the conditions, because you will want to see if the value is nil before checking if it's an empty string.
Calling obj.blank? is the equivalent of calling obj.nil? && obj.empty?, so that could make the code a bit shorter. Even better, obj.present? is the same as !obj.blank?.
Therefore, that line could be simplified to:
session[:album][#artist.id.to_s].present? ? 'block' : 'none'
Happy Rails-ing!

Related

ejs, how to add dynamic attributes of html tag?

I use express.js + ejs, I have two cases:
1.
prev
But it give me an error: Could not find matching close tag for "<%="./nundefined/nError: Could not find matching close tag for "<%=".
I want to get
prevDisabledClass ? <a href=''>prev</a> : <a href='?page=<%=+page - 1%>'>prev</a>
2.
like above, but dynamic add href attribute to html tag <a>
I want to get this:
prevDisabledClass ? <a>prev</a> : <a href='?page=<%=+page - 1%>'>prev</a>
How can I solve these two problem?
For the first one you currently have this:
prev
You can't nest <%=, try this instead:
prev
For the second one it'd be almost exactly the same but you'd move the condition around more of the output:
<a<%- prevDisabledClass ? '' : (' href="?page=' + (page - 1) + '"') %>>prev</a>
Here I've used <%- instead of <%= to ensure the " doesn't get HTML encoded.
It might be clearer to ditch the ?: altogether:
<% if (prevDisabledClass) { %>
<a>prev</a>
<% } else { %>
prev
<% } %>
There's some duplication but it's much easier to read.

Why are ampersands escaped when generating url with link_to?

Here is my simple rails 3 code :
<%= link_to "link", gateway_index_url(developer:#item.developer.api_key, tracker:"email", url:#product.url) %>
And the result is :
<a href="/gateway?developer=abcde&tracker=email&url=http%3A%2F%2Fwww.bla.fr%2FproductA" >link</a>
The problem is that & are rewritten in &. I can't figure how to prevent escaping, as :escape => false doesn't exist in Rails 3
Update: So here's the source
def link_to(*args, &block)
if block_given?
options = args.first || {}
html_options = args.second
link_to(capture(&block), options, html_options)
else
name = args[0]
options = args[1] || {}
html_options = args[2]
html_options = convert_options_to_data_attributes(options, html_options)
url = url_for(options)
href = html_options['href']
tag_options = tag_options(html_options)
href_attr = "href=\"#{ERB::Util.html_escape(url)}\"" unless href
"<a #{href_attr}#{tag_options}>#{ERB::Util.html_escape(name || url)}</a>".html_safe
end
end
As we can see, from the source, this behavior is by design.
You can try one of two solutions, I haven't tried them but they should work
1.) Try placing the call to gateway inside of a call to #raw:
<%= link_to "link", raw(gateway_index_url(developer: #item.developer.api_key, tracker:"email", url:#product.url)) %>
That may solve your specific problem, an the second approach, while a bit more brute-force should also work...
2.) If you want to convert it (the whole href) back you can... use CGI::unescape_html:
<%= CGI::unescape_html(link_to "link", gateway_index_url(developer: #item.developer.api_key, tracker:"email", url:#product.url)) %>
Good luck, hopefully this helps.
Update 2: Fixed call to cgi unescape, was using "." when it should be "::" and formatting fix. Forgot to indent example for #1
Rory O'Kane is spot on. The answer to "Why are ampersands escaped when generating url with link_to?" is that is the correct way to separate params in a url.
Is there a problem with the url the way it is?
If so, could you elaborate on the problem?
You may be able to prevent escaping the url by using raw on the entire url like so:
<%= link_to "link", raw(gateway_index_url(developer:#item.developer.api_key, tracker:"email", url:#product.url)) %>

Ternary operator not working in Haml

This question has been asked but the answers have not worked. The problem I am having is this hamlc code:
.UI_feed_item.deletable.clearfix{ :class => #feed.fav_post ? 'favorited' : '', feed_id: "#{#feed.id}", id: "feed_item_#{#feed.id}" }
*a lot more haml that doesn't have to do with this question*
the indentation is correct - it shows up weird on here
I want an extra class added to say favorited if feed.fav_post is true. for some reason it added a class 'true' or 'false' instead. I have also tried this:
.UI_feed_item.deletable.clearfix{ :class => (#feed.fav_post ? 'favorited' : ''), feed_id: "#{#feed.id}", id: "feed_item_#{#feed.id}" }
same result
I cannot do an if/else thing because there is no end in haml and I would have to rewrite a hundred lines of indented code. please help! none of the other solutions on the web have worked
Your second shot should work fine. The first variant returns true/false because the hash rocket wins over the ternary operator in terms of precedence - but shouldn't it break on syntax error after that?
You can do if-else in haml.
- if true
some stuff here
- else
some other stuff here
Indentation is used instead of end.
HAMLC doesn't support the ?/: ternary operators, but you can still achieve what you want using inline if/then/else. Try this:
.UI_feed_item.deletable.clearfix{ :class => "#{ if #feed.fav_post then 'favorited' else '' }", feed_id: "#{#feed.id}", id: "feed_item_#{#feed.id}" }

Pass variables into rails partial

So basically in my partial I have the following line of code
...
<%= " active" if current_user."#{prop_row}" == "repeat-x" %>
...
So I tried to pass in the following variables "prop_id", "prop_row" using:
<%= render :partial => "users/image_props/repeat", :prop_id => "mbr", :prop_row => "main_background_repeat" %>
I get the error
/Users/codyjames408/rails/moz/app/views/users/image_props/_repeat.html.erb:4: syntax error, unexpected tSTRING_BEG
...= ( " active" if current_user."#{prop_row}" == "repeat-x" );...
...
^
I think the errors because its appending a string instead of the row method. But I am pulling my hair trying to figure how to work around this.
I would love to turn this into a big helper method or something! I just don't know how...
If prop_row is a string, containing the name of an attribute, you ucan do this:
<%= " active" if current_user.attributes[prop_row] == "repeat-x" %>
Or use this:
<%= " active" if current_user.send(prop_row.to_sym) == "repeat-x" %>

Rails 3 custom validation: Highlighting offending fields

I'm writing my first custom rails validation, and would like to tag the offending class with an html "error" class if they return false - I can't quite figure out how to do it. Relevant validation code below - any help appreciated.
(If it makes a difference, I'm using jQuery)
validates_each :shop do |record, attr, value|
shopvar = record.shops.map{ |s| s.email.downcase.strip }
if shopvar.count != shopvar.uniq.count
record.errors.add(attr, 'has the same email address entered more than once')
#record.errors[attr] << "You have entered this shop in the form twice"
end
end
So in your form you'd have something like this for an input field
<%= form.text_field :title %>
Since errors is a hash you could use the "include?" method like so...
errors.include?(:title)
This tells you that there's something wrong with this field. Now all you need to do is style it.
Whack on a ternary operator asi...
<% css_class = errors.include?(:title) ? "highlight_error_class" : "no_problem_class" %>
<%= form.text_field :title, :class => css_class %>
Done.