How do I specify the format for url_for in a nested route? - ruby-on-rails

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

Related

Ruby hash map with key that contains '-'

How can I add hash map element with a key that contains "-"?
Like this:
<%= button_to_function 'Cancel','cancelRemove("cancelEmail")', :data-dismiss=>'modal', :class=>'btn' %>
I get an error:
undefined local variable or method 'dismiss' for #<ActionView::Base:0x3482fed>
While :'data-dismiss' works, with data attributes you can also do
:data => { :dismiss => 'modal' }
Additional data-prefixed html attributes can be included in the same hash. So for example on another link you might do:
:data => { :remote => true, :method => 'delete' }
which would add to the link the html attributes data-remote="true" data-method="delete".
While the hash syntax is less compact for a single attribute, it's nice when you've got more than one html5 data attribute. And it's arguably a bit more rails-ish.
Just rename it to:
<%= button_to_function 'Cancel','cancelRemove("cancelEmail")', :'data-dismiss'=>'modal', :class=>'btn' %>

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" })

Haml Hyperlink inside a label tag

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.

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.

Auto_complete_for question

I've recently installed this plugin, and I meant to create a Tag field with it, like StackOverFlow does.
When I put the following syntax on my AnnouncementsController(I want to tag announcements) it works great:
auto_complete_for :announcement, :title
protect_from_forgery :only => [:create, :delete, :update]
Also, I had to add the routes syntax as well to make it work:
map.resources :announcements, :collection => {:auto_complete_for_announcement_title => :get }
Now, when I try to accomplish the same with the tags, at the time I create a new announcement, I simply replace the word "announcement" for "tag" and "title" for "name", and it won't work. Tag makes reference for my Tags table at the database.
The error says the following:
<h1> ActiveRecord::RecordNotFound
in AnnouncementsController#show </h1>
<pre>Couldn't find Announcement with ID=auto_complete_for_tag_name</pre>
Can anybody tell me what I'm doing wrong?
Thanks,
Brian
In your view you probably want to change:
<%= text_field_with_auto_complete :announcement, :title %>
to:
<%= text_field_with_auto_complete :tag, :name %>
to make it work, take another look at the error it's giving, it's still calling announcement.
--- edit:
from autocomplete source:
def text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {})
Well, I finally got the answer to my problem.
I was missing the following at routes.rb:
map.auto_complete '/:controller/:action',
:requirements => { :action => /auto_complete_for_\S+/ },
:conditions => { :method => :get }
My new question now it works is the following:
What if I wanted to multi-tag an announcements, for example: "Ruby, C#". Should I change the plugin's logic or is there a functionality to make this work? Cause right now, it will check for the text_field text, not discriminating a new word after a comma or any kind of separator.
Thanks,
Brian

Resources