I have a variabla var. If I try to output its value in HAML like =val then I just get the string value of the object which looks like this: #<ShortenedUrl:0x118c50fa.
But how do I get the value that is in there?
Using Haml
%h2
#{#project.name}
or
%h2
#{org.id}
I think you may want the .inspect method.
= val.inspect
That will show you something like:
#<ShortenedURL #url="the url", #count=0, #etc="etc">
Of course, if you want to dive in to specifics (for example, you only want to show someone the url attribute (or whatever attribute you may have), then use that method:
= val.url
Which will show:
the url
Related
Is it possible to pass pre formatted HTML to the haml file. For example I pass a variable such as:
my_text = "<b>this is bold</b>"
Then in my haml file:
%p
=#my_text
I was hoping it would display This is bold
But it just returns the original string and ignores the tags surrounding "this is bold"
The goal is to highlight certain key words("one" and "two" in this example), here's a better example:
#my_text = "This <b>one</b> plus <b>one</b> is a total of <b>two</b>"
Not sure what you want to achieve, but i'd recommend that you keep your markup in the haml and plug in your copy in the instance variable like this.
MyTextController.rb
#my_text = this is bold
my_text.html.haml
%b
= #my_text
Edit after further clarification.
You can use sanitize helper for this.
my_text.html.haml
%p
= sanitize(#my_text, tags: %w(b))
Ahh I figured it out, looks like you can do:
%p
= raw #my_text
I am trying to interpolate a ruby variable in a HAML%img src tag, with the ruby interpolation operator #{}. In the following way:
-#locations.each do |location|
%li
%img(src: "#{location.thumbnail_url}")
However, I get the following error:
Invalid attribute list: "(src: \"\#{location.thumbnail_url}\")".
Is there a valid way to do this? I am sure it has been done before but can't see any literature/any other posts about it.
It looks like you’re mixing the two attribute styles, the normal style and the HTML style.
You want to either replace () with {} and use the normal style:
%img{src: "#{location.thumbnail_url}"}
or use the HTML style with = instead of :, like this:
%img(src = "#{location.thumbnail_url}")
I am trying to create a link using HAML which looks like the this
=link_to("Last updated on<%=#last_data.date_from.month %>",'/member/abc/def?month={Time.now.month}&range=xyz&year={Time.now.year}')
It is not taking the Ruby code and it is displaying that as a string
Last updated on<%=#last_data.date_from.month %>
and in the URL as well it is not taking the function Time.now.month or Time.now.year .
How do I pass Ruby code in URL and in the string ?
You should probably use something like this:
= link_to("Last updated on #{#last_data.date_from.month}", "/member/abc/def?month=#{Time.now.month}&range=xyz&year=#{Time.now.year}")
Note that in the second string, it's necessary to change the ' to ". Also if the link text is getting long, you can use something like this:
= link_to("/member/abc/def?month=#{Time.now.month}&range=xyz&year=#{Time.now.year}") do
Last updated on #{#last_data.date_from.month}
Everything after the = in HAML is a Ruby expression. Ruby doesn't interpolate strings the way HAML does, it has own way of such interpolation.
In Ruby, when you want to have string value of some variable inside another string, you could do.
"Some string #{Time.now}"
So, it should be:
= link_to "Last updated on #{#last_data.date_from.month}", "/member/abc/def?month=#{Time.now.month}&range=xyz&year=#{Time.now.year}"
A simple example with easy syntax:
link_to "Profile #{rubycode}", "profile_path(#profile)/#{ruby_code}", class: "active"
guys!
I`d like to make 'clever pagination'. For example, if i already have params.category=Auto - i want it to be added to my params.
Of course, i can do smth like this :
<g:paginate total="${total}" max="10" maxsteps="5" params="[category: params.category,subcategory: params.subcategory]"/>
But if current params.subcategory is null - it will also be added to url ( ?subcategory=&category=Auto) . I don`t want to have 'subcategory=' in my params in such a case!
Also I can do it with string concatenations to create new url - but maybe grails have some cheats/mechanism to create new url without string concatenations?
Cheers, Dmitry.
There aren't any great solutions I can think of. You could try:
params="${[category: params.category,subcategory: params.subcategory].findAll {it.value} }"
Alternatively, provide your own custom tag that strips out null param values before delegating to the paginate tag.
I have this line in a template:
jander
So as you can guess when I click that link, it executes an action. Inside that action I have this:
var_dump($request->getParameter('aux'));var_dump($request->getParameter('sf_subject'));die("fasf");
It's printing 4 and NULL. Why NULL? I expected the $category object was showed..
You can't get the sf_subject parameter that way. You can only use the sf_subject with the sfDoctrineRoute or sfPropelRoute. In your code, you can then get the selected object with $this->getRoute()->getObject().
What is the html representation of your first line?
Seems like in your template $category is not defined, so the < a > should looks like
foo.php?aux=4&sf_subject=null or
foo.php?aux=4
Define the category in your template :-)