Вопрос

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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top