Hi i converting rails views from erb to Haml .The issue i faced is when use the form_for the haml throws the UNEXPECTED $end error. I am sure i did the space indentation well with in form_for .......Even if i use "each do" loop is says the same error. if i revert the haml to erb it works fine.
Rails version i used : 2.3.2 & i installed haml gem 2.2.19 as well as haml plugin also.
my controller code :
def new
#user = User.new
end
My View code :
.contentContainer.signup
- form_for(#user) do |f|
Make sure your haml indentation is perfect.
.contentContainer.signup
- form_for(#user) do |f|
= f.text_field :name
Are you including - end in your templates? Haml takes care of ends for you, so if you add your own, it won't work.
Can you paste your entire template (in a code block, so it's formatted properly)?
There's also a good command-line tool to make transition easier: html2haml. It doesn't always produce the prettiest haml, but it certainly works.
Hey, there's even a web-based form for this: http://html2haml.heroku.com/
Generally, be sure your indentation is perfect. haml is very particular about indentation. If you use a decent editor (such as textmate or vim) this is an easy task.
If the last line in file is indented there has to be an addidtional, empty line.
Related
I'm trying to convert my Spree store to Haml, and I'm facing some trouble with some parts of the backend.
For instance, I take the following Haml code:
- #orders.each do |order|
%tr{:class => "state-#{order.state.downcase} #{cycle('odd', 'even')}"}
%tr{:class => "state-#{order.state.downcase}"}
And here's the output HTML (simplified to a single iteration):
<tr class="state-#order.state.downcase"></tr>
<tr></tr>
Can someone help me understand why the string interpolation is wrong in both cases? I have been looking at this for hours...
Your HAML syntax looks fine, but it's possible cycle is causing trouble and failing silently. Cycle is a RAILs method within ActionView::Helpers::TextHelper, so perhaps your code no longer is including that library now that you've dropped Spree.
If this is the source of the problem, you could either include the ActionView Helpers or rewrite your code without cycle with something like the following:
%table
%tbody
- #orders.each_with_index do |order,index|
%tr{:class => "state-#{order.state.downcase} #{%w(even odd)[index%2]}"}
%td{:class => "state-#{order.state.downcase}"}
#{order.state}
PS - I'm assuming the second tr in your sample code is supposed to be a td and that you're just providing a fragment of the table.
I have just revisited this and came across the fact that Deface (used by Spree) is not compatible with Haml.
So either one uses ERB and lives with it, or completely disables Deface and rewrite all 'defaced' views.
P.S.: In order to disable Deface, I added config.deface.enabled = false to development.rb, production.rb and test.rb inside config/environments/.
I'm trying to figure out how you can herald a Ruby block in a <%= ... %> emitter.
No problem with the '<% form_tag do %>' part, but as I dig into Rails internals
and see how it uses erb to process templates, the generated Ruby code is invalid,
due to that hanging 'do'. Is there a post-processor hiding in Rails somewhere that
straightens out the code before running it? If yes, where is it? If no, how does
Rails pull HTML and Ruby code out of this form?
Rails added a hack which uses a regular expression to figure out if what is passed to erb is a block expression and then handle it differently.
For a more detailed explanation: http://timelessrepo.com/block-helpers-in-rails3
I'm working on upgrading an old Rails app (1.1.6) to Rails 3. Obviously, a lot has changed. One thing appears to be that Rails automatically escapes content dropped into the view. However, I have a situation where I have a helper generating IMG tags for me, and Rails is automatically escaping the resulting content.
<%= random_image('public/images/headers') %>
This results in escaped content, much like one would expect had I done this (in 1.1.6)
<%= h random_image('public/images/headers') %>
Is there a way to tell it to not escape?
<%= raw random_image('public/images/headers') %>
.html_safe
It may need to be inside the helper
There are there ways in which this can be achieved in rails 3 application
html_safe
raw
h
raw and h can only be used in controller and views these methods are defined in helpers.
html_safe can be used anywhere in a rails application i.e., can be used in models, helpers, controller etc.
For more information please read http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/
When designing for layout and styling with CSS, it might be useful to add a line in the view or partial:
<%= who_am_i %>
or if using haml:
= who_am_i
so that it will print on the webpage
this is view/products/_footer.html.haml
How can this helper be written? (or is there one already in Rails?)
(my first try was to use __FILE__ and do some manipulation with string, and it works well if everything is done inside the view or partial file, but when it moves to helper, then __FILE__ becomes helpers/application_helper.rb so it won't work. But I'd like to find out possibly better ways to do it)
Your helper method should be:
def who_am_i
#template.template
end
Rails 3
def who_am_i
#_virtual_path
end
Note:
I tested the solution in Rails 3.0.5 and it works. I am not certain that it will work in all scenarios. Use with caution.
If you use haml as rails view template, you can write portion of your page using markdown by using the ":markdown" filter.
Is is possible to do the same using erb?
It's pretty easy to write a method that does it, assuming you're using something like Rails which has #capture, #concat, and #markdown helpers. Here's an example, using Maruku:
def markdown_filter(&block)
concat(markdown(capture(&block)))
end
Then you can use this as so:
<% markdown_filter do %>
# Title
This is a *paragraph*.
This is **another paragraph**.
<% end %>
There are a few things to note here. First, it's important that all the text in the block have no indentation; you could get around this by figuring out the common indentation of the lines and removing it, but I didn't do that in the example helper above. Second, it uses Rails' #markdown helper, which could easily be implemented in other frameworks as so (replacing Maruku with your Markdown processor of choice):
def markdown(text)
Maruku.new(text).to_html
end
Rails 3 has removed the #markdown helper, so just add the above code in the appropriate helper, substituting the Markdown processor of your choice.
ERB does not have filtering like this built-in. You'll need to directly use a gem that handles it, like RDiscount or the venerable BlueCloth.