I am using Haml in a Ruby on Rails project. I know you use the = sign to execute Ruby, but so far what I have seen is that the Ruby code has to be the last part of a line.
I am trying to add a class to a th element dynamically. (In case it's relevant: each td also contains more Ruby.)
I have the following code:
%th= link_to 'Name', res_path
I want to add a class to th, and the name of this class is in an instance variable called class_name. I tried this:
%th.=#class_name
But it doesn't work.
How should one include Ruby code twice on the same line using Haml?
The html_options solution offered by Alok will add the class to the 'a' tag. I would do this over two lines instead of one:
%th{ :class => #class_name }
= link_to 'Name', res_path
As #DavB pointed out, only static text can be used with the ./# notation. Otherwise, you can either pass your options to your helper method (if it accepts them), or, more universally, use a hash attribute as in %th{:class => #name}. It will result in <th class="namevalue">.
You could use the html_options of the link_to tag and then add the class there. I think thats the standard way of doing this.
Related
I was given this ruby code to overview. I am still new to ruby on rails. I come from a java background.
in User.rb:
def last_name=name
require 'debugger'; debugger
self[:last_name] = name
end
And told me that this is a setter method. They told me that this get executed in the "form" in this line:
<%= f.label :last_name%>
<%= f.text_field :last_name %>
Ok. Could somebody clarify how this ridiculous syntax can be valid?
1) An instance of the class "User" is never initialized. How is the method even called?
2) Where does the variable "name" comes from? what is the value of it? (the variable name is called nowhere else) And what does this syntax stand for? "def last_name=name" ?? Pass to the method a variable that has not been initialized? It is a short-cut for another syntax just to save typing 2 more symbols?
3) How can this method be called, in the form? I dont see a "User.last_name("David") or anything similar.
Could somebody clarify those piece of code please?
And please dont post links to tutorial or anything else. Just clarify this piece of code
The code you described:
<%= f.label :last_name%>
<%= f.text_field :last_name %>
..is used by the default rails template engine. It is view code.
1) A User instance is likely initialized and populated when the form is submitted.
The form action corresponds to an appropriate controller action, which likely accepts :last_name as a parameter. When you submit the form, the controller action probably instantiates the User instance. Without more code, however, I can't be 100% certain this is the case with your application.
2) The variable name comes from the argument accepted by the last_name method.
Perhaps, to help you understand the method, let's rewrite it:
def last_name= (name)
require 'debugger'; debugger
self[:last_name] = name
end
Either last_name=('John Doe') or last_name = 'John Doe' will execute this method.
3) I think my previous descriptions should help you make sense of this..
MVC. In the action new a new instance of User is created and assigned to #user, which is what will be used for the form.
name comes from the method declaration def last_name=name
The form helpers does last_name=name when it assigns a value to that variable and uses the setter to do that. When you edit the object it will use the getter to display its value in the text field.
You can understand better point #2 with this syntax:
def last_name=( name )
end
The = is part of the function's name.
C equivalent would be
void last_name_equals( char *name ) {}
The rest is a Rails tutorial's job.
I have
= image_tag "chart.jpg"
I am new to HAML so how do I add a class to this?
Assuming this is rails (Haml doesn't have its own image_tag helper, but Rails does), then the second argument is an options hash where you can specify the class:
=image_tag "chart.jpg", :class => "my_class"
Stumbled across this answer when I was looking for something else and thought I'd throw in a little update to use the more modern formatting :)
=image_tag 'chart.jpg', class: 'my_class'
I use rails 3. Is there any easy way to tell I18n to respect 'html safness' of string used in interpolation and make all translated string html safe by default? So if I have this en.yml:
en:
user_with_name: 'User with name <em>%{name}</em>'
and I use t('user_with_name', :name => #user.name), I get users name html escaped, but <em> and </em> is left as is?
http://guides.rubyonrails.org/i18n.html#using-safe-html-translations
The official Rails guide says you can use the interpolated variables without concern, since they are html escaped automatically, unless you specifically declare them to be String.html_safe.
From the guide:
Interpolation escapes as needed though. For example, given:
en:
welcome_html: "<b>Welcome %{username}!</b>"
you can safely pass the username as set by the user:
<%# This is safe, it is going to be escaped if needed. %>
<%= t('welcome_html', username: #current_user.username %>
Safe strings on the other hand are interpolated verbatim.
Change the name from user_with_name to user_with_name_html, then rails will know you have included html in the text.
Old question, but if someone wants to achieve this, here's the monkey patch I came up with :
module ActionView
module Helpers
module TranslationHelper
private
def html_safe_translation_key?(key)
true
end
end
end
end
Put this in an initializers and that's it!
Works with Rails 3.2.6.
Only marks the text in localization files as safe, not the interpolation parameters.
In Ruby on Rails 3 How would I create a view that decides by a parameter what link in view links to?
For example to a page in my view I pass a type parameter which displays all projects in my data base and depending on the type links to either the new show or edit action.
I am interested in only passing on the path of the link.
I would like to write something like:
<% link_to(enter_here_path) do %>
<div class="blah"><%=#project%></div>
<%end%>
You could use a conditional which returns the proper location or even creates the link, probably best wrapped in a helper method.
Something like that:
def your_link_method(type="delete")
case type
when "delete"
link_to …
when "foobar"
link_to …
else
link_to …
end
end
end
As a sidenote: This kind of construct smells IMO and I'd probably rethink my design first, before I implement a solution like this. Even if you can probably find a simpler and more elegant way to write it.
How can I disable automatic conversion of HTML tags in Rails3? I have output in some controller view. For example I have method which outputs simple HTML link set..
[:en, :de].map{ |locale| link_to locate.to_s.upcase , { :locale => locate } ...
In view I'm calling my method <%= my_method %>
As a result I get this:
| <a href="/login?class=language_selected&locale=en">EN</a>
How can I disable it?
I haven't worked with Rails3 so no guarantees. but it looks like this has to do with the fact that your method returns a list.
Rails will usually format internal data structures for output by escaping the special characters and displaying the html escaped interpretation of your data.
Try tacking a .join onto the end of your map call to return a string
[:en, :de].map{ |locale|
link_to locate.to_s.upcase , { :locale => locate }
...
}.join("<br/>")
Also rwilliams aka r-dub's suggestion to use raw will probably be necessary addition to this code. raw on a list however may give you an undesirable result probably because of an internal to_string call. Which is an implicit join(""). So add the raw to the method call in addition to returning a string.
<%= raw my_method %>
If you're sure your methods output is safe then you can use the raw method.
<%= raw my_method %>