Haml Hyperlink inside a label tag - ruby-on-rails

Inside my .haml file, I have
= employee.label :tos_confirmation, 'I agree to the Terms of Service.', :class => 'wide'
How do i make Terms of Service a hyperlink?

= link_to employee.label(:tos_confirmation,
'I agree to the Terms of Service.',
:class => 'wide'),
'http://example.com/tos'

The answer I was looking for is like this:
%label.wide{:for => "tos_confirmation"}
I agree to the
%a{:href => "/tos"} Terms of Service
and
= succeed "." do
%a{:href => "/privacy_policy"} Privacy Policy
The only part I'm missing is the "employee" part, which is not too important.

Related

Rails: Setting class and data-tag of an HTML attribute with a single rails method

I'm currently working on a tour interface that guides new users around my site. I have a Tour model that has many TourStops, each of which contains information about a section of the site.
Basically, I'd like to write a function for the Tour model that -- when passed the number of a TourStop -- generates the correct class and data attribute for the HTML element it's attatched to. For example, I'd like
<%= link_to image_tag("new_button.png", tour.stop_data(1), :title => 'Add new asset'), new_asset_path %>
to call a function and return something like
def stop_data(order)
" :class => '#{tour_stops.find_by_order(order).name}',
:data => '{:order => order}'"
end
creating a link_to tag like:
<%= link_to image_tag("new_button.png", :class => 'tour_stop_1',
:data => {:order => 1}, :title => 'Add new asset'), new_asset_path %>
The above code doesn't work. Is something like this even possible? If not, what's a better approach I might take?
The image_tag accepts two parameters. A source, and a options Hash.
What you are trying to do is squeezing your return value from stop_data into this options Hash.
In order to get this to work, you first, need to return a Hash from stop_data, and second, make sure you pass only two arguments to image_tag - the source, and the options.
First:
def stop_data(order)
{
:class => tour_stops.find_by_order(order).name,
:data => { :order => order } # you may need order.to_json
}
end
Second:
link_to image_tag("new_button.png", tour.stop_data(1), :title => "Add new asset"), new_asset_path
This looks like it will work, but it won't, since your'e passing three parameters to image_tag.
When you do the following:
image_tag("new_button.png", :class => "tour_stop_1", :data => { :order => 1 }, :title => "Add new asset")
It looks like you're passing even 4 parameters to image_tag, but in fact they are only two. In Ruby, when the last parameter of a method is a Hash, you don't need to wrap the Hash key/value pairs in curly braces ({}), so the example above is essentially the same as
image_tag("new_button.png", { :class => "tour_stop_1", :data => { :order => 1 }, :title => "Add new asset" })
Now, to get your helper to work with image_tag, you need to merge the options, so they become only one Hash.
link_to image_tag("new_button.png", tour.stop_data(1).merge(:title => "Add new asset")), new_asset_path
Again, we're omitting the curly braces when calling merge, because it's only (and therefore last) parameter is a Hash. The outcome is the same as:
tour.stop_data(1).merge({ :title => "Add new asset" })

Rails - Render partial inside Twitter Bootstrap popover

I want to use a Twitter Bootstrap popover to display a user avatar and email address, with the possibility of adding in more details at a later date.
I am having an issue getting the partial to render inside the content field of the link. The view code is below (in HAML).
%span.comment-username
=link_to comment.user_name, "#", "title" => comment.user_name,
"data-content" => "=render 'users/name_popover'",
class: "comment-user-name"
Currently, this will only produce the content code as a string.
Is there a way to do this so the partial will be inserted instead of the code as a string?
You want rails to actually evaluate the content of the string and not just show the string. The easiest way to do that would be:
%span.comment-username
=link_to comment.user_name, "#", "title" => comment.user_name,
"data-content" => "#{render 'users/name_popover'}",
class: "comment-user-name"
That should pass the render statement to rails and render your partial as expected.
Thanks for the answer jrc - it helped me find a solution.
My solution might be useful for other non HAML people like me:
`<%= link_to('Service History' , '#', :class => "popover-history", :rel => "popover", :"data-placement" => "bottom", :title => "Service History", :"data-content" => "#{render 'services/service_history'}") %>`
with
`$(function () {
$('.popover-history').popover({ html : true });
});`

How do I specify the format for url_for in a nested route?

The following link_to statement:
<%= link_to image_tag("icons/document_24.png"),
[program_code.program, program_code],
:class => :no_hover,
:alt => "Print Tracking Code",
:title => "Print Tracking Code",
:target => :new
%>
will generate a url like /programs/1/program_codes/1
If I want the url to be /programs/1/program_codes/1.svg, how do I specify the format in the array that is being passed to url_for? I've searched the Rails API documentation and looked at several examples but have been unable to find anything like this.
I think your looking for the :format option. It will append the file extension to the link e.g. '.svg'
Make sure you put the :format option in the path building hash of the link_to method.
<%= link_to 'somewhere', {somewhere_to_path(#var), :format => "svg"},:title => "Print Tracking Code", :target => "_blank" %>
Hope this helps.
If you are dealing with a specific class and can use a named route, that is the most efficient option. But if you're dealing with nested resources and the parent resource isn't fixed (for instance, in a polymorphic association), AND you want to specify a format, url_for doesn't meet your needs.
Fortunately you can use polymorphic_url.
For instance, if server could be an instance of ManagedServer or UnmanagedServer, and both can have alerts, you can do this:
polymorphic_url([server, :alerts], :format => :xml)
and that will give you
/managed_server/123/alerts.xml
/unmanaged_server/123/alerts.xml

Using hyphen in link_to property?

In my Rails app, I need to set a value for a custom data-* attribute on an anchor tag. However, hashes can't have hyphens if I'm not mistaken. I essentially want to do this:
<%= link_to 'Example', example_path, :class => 'something', :data-id => '15' %>
:data-id is not valid, however. What can I do to work around this?
IIRC, for such purposes hashes and strings are equivalent, so you can use "data-id" instead of :data-id. Never checked for this particular method, though, so no guarantees.
I think in Rails 3 you can do
link_to "Click Here", root_path, :data => { :id => #model.id }
for all data attributes.

Ruby on Rails / Yellow Maps For Ruby Plugin woes

Okay I've read through the plugin comments and the docs as well and I have yet to come up with an answer as to how to do this. Here's my problem I want to use the :info_window_tabs and the :icon option, but I don't know what format to pass my information in. According to the documentation the following code should be correct. Here's my code:
#mapper.overlay_init(GMarker.new([map.lat, map.lng],
:title => map.name,
:info_window_tabs => [
{:tab => "HTML", :content => #marker_html},
{:tab => "Attachments", :content => "stuff"}],
:icon => {
:image => "../images/icon.png"
}))
The readme and documentation can be viewed here.
And the relevant ruby file that I am trying to interact with, including the author's comments, can be viewed here.
I have tried the #rubyonrails channel in IRC as well as emailing the author directly and reporting an issue at GitHub. It really is just a question of syntax.
Thanks!
Okay, so I finally got this figured out. Here's how you do it; :icon accepts a GIcon variable and :info_window_tabs accepts an array of GInfoWindowTabs. Here is how you would declare each with the plugin.
Declare GIcon
#mapper.icon_global_init(GIcon.new(:image => "../images/civil.png",
:icon_anchor => GPoint.new(0,0),
:shadow => "../images/shadow.png",
:shadow_size => GSize.new(37,32),
:info_window_anchor => GPoint.new(9,2)), "civil_icon")
#civil_icon = Variable.new("civil_icon")
Declare GInfoWindowTab
#tab1 = GInfoWindowTab.new('Tab 1 Label', 'HTML for inside of tab1')
#tab2 = GInfoWindowTab.new('Tab 2 Label', 'HTML for inside of tab2')
#window_tabs = [#tab1, #tab2]
Then in your GMarker declaration just do the following:
#mapper.overlay_init(GMarker.new([map.lat, map.lng],
:title => map.name,
:icon => #civil_icon,
:max_width => 300,
:info_window_tabs => #window_tabs))
And you're done.

Resources