Ruby on Rails / HAML image_path not working in else condition? - ruby-on-rails

I'm currently working on a project on RoR with haml, and i have an issue i have never saw before.
I have an "user profile" with an optional image. I want to show the image if it exists, if not, i show a placeholder (stored in assets => "admin/avatar.png". So, here is my sample code below:
-unless #user.image.nil?
=image_tag #user.image.url,class:"img-responsive img-circle",style:"width:150px;height:150px;margin:auto;"
-else
%img.img-responsive.img-circle{:src => image_path("admin/avatar.png"),style:"width:150px;height:150px;margin:auto;"}
%img.img-responsive.img-circle{:src => image_path("admin/avatar.png"),style:"width:150px;height:150px;margin:auto;"}
The first
%img.img-responsive.img-circle{:src => image_path("admin/avatar.png"),style:"width:150px;height:150px;margin:auto;"}
is not working (and shows "missing") and the seconde is actually working. I'm a bit lost with this.
Anyone ?
thanks.

unless else is a kind of code smell
A solution I'd propose:
- if #user.image.present?
= image_tag(#user.image.url, class:"img-responsive img-circle",style:"width:150px;height:150px;margin:auto;")
- else
= image_tag('admin/avatar', class:"img-responsive img-circle", style:"width:150px;height:150px;margin:auto;")

Related

Inline Attachment image is not displaying in Rails 4

I've been trying to show inline attachments in my mail (logo),
I've seen many answers on the community, nothing seems to be working for me,
Mail is going fine,I can see the text but unable to see image
this is my mailer:
path variable contains path to logo
attachments.inline['logo.png'] = { content: File.read(path),mime_type: "image/png" }
this is my view:
Welcome to my site
<%= image_tag attachments['logo.png'].url%>
Please Help,I've been pulling my hair for hours now on this.
In your mailer try:
attachments.inline["myimage.png"] = File.read(File.join(Rails.root, "app", "assets", "images", "myimage.png"))
your view code is correct.
I've been using this code successfully with one image, though there may be a bug in rails. Check: https://github.com/rails/rails/issues/2686

sfPHPCaptchaPlugin : The following code is invalid

I meet a very strange problem,I used sfPHPCaptchaPlugin to make captha to my form in sf1.4 project I worked on,all configuration and installation of plugin is correct but the captcha verification won't let me save data of form in database and tell me that the text is wrong but I'm sure I entered the same text showing in image of captcha and here is the line who get me the error :
'captcha' => new sfValidatorPHPCaptcha(array(), array('invalid' => 'The following code is invalid.')),
Someone had the same errore before please?
sfPHPCaptchaPlugin is not stable for symfony 1.4 as it said in the plugin information Here.
You can use sfCaptchaGDPlugin its a very good one, folow the intalation guide in README, and use this widget:
$this->widgetSchema['captcha'] = new sfWidgetCaptchaGD();
$this->validatorSchema['captcha'] = new sfCaptchaGDValidator(array('length' => 4));

Aptana Studio 3 Snippet Around Selection

So I have recently switched from Dreamweaver to Aptana Studio 3 and I have been playing around with the whole custom snippet feature. For the life of me though I cannot figure out how to take a selection/highlighted text and wrap it with my own custom code and/or text. I have looked around the internet for three days now and cannot find anything regarding snippets. I have found some things using commands and key combinations, but I am wanting to create and use a snippet and trying to modify what I have found is not producing good fruit.
I have been able to create my own category and some basic snippets that insert straight text, but nothing that uses a selection.
I have absolutely NO experience with Ruby so forgive me if what follows is completely atrocious. I have more experience with PHP, HTML, Javascript, Java, etc. Here is what I have so far.
snippet "Selection Test" do |snip|
snip.trigger = "my_code"
snip.input = :selection
selection = ENV['TM_SELECTED_TEXT'] || ''
snip.expansion = "<test>$selection</test>\n"
snip.category = "My Snippets"
end
I haven't done much with custom Snippets, but if it helps, there is an example in the HTML bundle of a snippet that surrounds the selected text with <p></p> tags when you do Ctrl + Shift + W. You can see the code for it in snippets.rb in the HTML bundle:
with_defaults :scope => 'text.html - source', :input => :none, :output => :insert_as_snippet do |bundle|
command t(:wrap_selection_in_tag_pair) do |cmd|
cmd.key_binding = "CONTROL+SHIFT+W"
cmd.input = :selection
cmd.invoke do |context|
selection = ENV['TM_SELECTED_TEXT'] || ''
if selection.length > 0
"<${1:p}>${2:#{selection.gsub('/', '\/')}}</${1:p}>"
else
"<${1:p}>$0</${1:p}>"
end
end
end
end
I fiddled around with putting it into the PHP bundle for a few minutes under CTRL + Shift + P and got it working in HTML files, which was not my goal... but was progress. I may play around with it some more later, but in the meantime, maybe you know enough after all of your research to get something put together. I would be interested to see your results if you get this figured out.

(Rails) Assert_Select's Annoying Warnings

Does anyone know how to make assert_select not output all those nasty html warnings during a rake test? You know, like this stuff:
.ignoring attempt to close body with div
opened at byte 1036, line 5
closed at byte 5342, line 42
attributes at open: {"class"=>"inner02"}
text around open: "</script>\r\t</head>\r\t<body class=\"inner02"
text around close: "\t</div>\r\t\t\t</div>\r\t\t</div>\r\t</body>\r</ht"
Thanks
It's rather that your code is generating invalid HTML. I suggest running it through a validator and fixing all the validation errors.
You can find out which test ran into the problem by using the TESTOPTS v flag:
(bundle exec) rake test TESTOPTS="-v"
This will give:
test: Request the homepage should have a node list. (PresentControllerTest): .
test: Request the homepage should have the correct title. (PresentControllerTest): ignoring attempt to close div with body
opened at byte 4378, line 89
closed at byte 17745, line 393
attributes at open: {"class"=>"colleft"}
text around open: "class=\"colmid\"> \n\t\t\t<div class=\"colleft\""
text around close: "x2.js\" ></script>\n </body>\n</html>\n\n"
ignoring attempt to close div with html
opened at byte 4378, line 89
closed at byte 17753, line 394
attributes at open: {"class"=>"colleft"}
text around open: "class=\"colmid\"> \n\t\t\t<div class=\"colleft\""
text around close: "</script>\n </body>\n</html>\n\n"
.
test: Request the homepage should not set the flash. (PresentControllerTest): .
test: Request the homepage should respond with 200. (PresentControllerTest): .
Rails's HTML scanner expects XHTML, if you're using HTML4 where tags don't have explicit closing tags, you may get this warning... doesn't look like solved issue
http://dev.rubyonrails.org/ticket/1937
http://gilesbowkett.blogspot.com/2009/10/ignoring-attempt-to-close-foo-with-bar.html
What I'd want is to know where the warning is coming from. The fact it doesn't specify the test or the controller/action which generates the invalid HTML is the big problem for me.
I had some problems after an update to rails 3.0.9 and HAML 3.1.2
What I did was silence those ugly outputs with the following code in *test_helper.rb*
# Wrap up the method assert_select because after updating to Rails 3.0.9 and HAML 3.1.2,
# I don't know why but it was raising warnings like this:
# ignoring attempt to close section with body
# opened at byte 6157, line 128
# closed at byte 16614, line 391
# attributes at open: {"class"=>"left-column"}
# text around open: "->\n\n\n</span>\n</div>\n<section class='left"
# text around close: "'1'>\n</noscript>\n</body>\n</html>\n"
# But the HTML seems to be valid (in this aspects) using a HTML validator.
ActionDispatch::Assertions::SelectorAssertions.class_eval do
alias_method :assert_select_original, :assert_select
def assert_select(*args, &block)
original_verbosity = $-v # store original output value
$-v = nil # set to nil
assert_select_original(*args, &block)
$-v = original_verbosity # and restore after execute assert_select
end
end
Anyway, I don't recommend using a solution like this. Use only if you are in hurry, and give it a good comment to explain other developers why is that estrange piece of code there.
For me, the cause was valid but poorly formed HAML.
This is what I had:
%ul
%li
= link_to "Help", help_url
- if current_user
%li
= link_to "Users", users_url
= link_to "Logout", logout_url
- else
= link_to "Login", login_url
This is what is correct for what I'd wanted to do:
%ul
%li
= link_to "Help", help_url
%li
- if current_user
= link_to "Users", users_url
= link_to "Logout", logout_url
- else
= link_to "Login", login_url
The best way to track this down is to look very carefully at the "text around open" and "text around close" and try to track down where in your template the open occurs.
Even if you do rake test TESTOPTS="-v" and the error appears to be coming from your view templates, DON'T forget to check the application layout html. This happened to me and it took a few minutes longer than I'd like to admit going back and forth between a couple index.html.erb files before I finally figured it out. Same goes for any rendered partials.

FileTest.exists? issue with ruby on rails

I am trying to check if a file exists in my rails application.
I am running ruby 1.8.6 and rails 2.1.2 with windows XP.
So, the problem is that the FileTest.exists? method doesn't seem to be working. I have simplified the code to this point :
if FileTest.exists?("/images/header.jpg")
render :text => "yes"
else
render :text => "no <img src='/images/header.jpg' />"
end
If I do that the system displays "no" and then includes the image that displays correctly because /images/header.jpg exists.
I tried FileTest.exists?, FileTest.exist?, File.exists?, File.exist? and nothing seems to be working.
What am I doing wrong ?
Thanks
I'm guessing it's because you're asking whether a file "header.jpg" exists in a directory "images" off of the root directory for your system (which on Windows I'd assume is "c:\"). Try putting the full path (from the filesystem root) to the "/images" directory rather than the URL path.
In particular, as pointed out by #Brian, you should use:
FileTest.exists?(RAILS_ROOT + "/images/header.jpg") # < rails 3.0
FileTest.exists?(Rails.root + "/images/header.jpg") # >= rails 3.0
Add RAILS_ROOT to the filename that you're checking before calling exists?

Resources