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' %>
Related
I want to post some data as an html attribute. Suppose there is a rails variable
#fields = [{:name => "id", :type => :integer}]
I want to post in the page for use with jquery's .data(). So in the .html.erb I have
<%= form_tag( {:action => 'whatever'}, {... , :"data-fields" => h(#fields.to_json)}) %>
But while rendering, the quotations in string [{"name":"id","type":"integer"}] mess up the other attributes because Rails form_tag uses double quotes to enclose the whole json string in double quotes. How do I post json with strings as an attribute from Rails?
Have you tried the following?
<%= form_tag { :action => "whatever" }, { :data => { :fields => h(#fields.to_json) } } %>
Have you tried escape_javascript ? Although it has known downsides, it is not exactly for escaping JSON, and I am not sure escaped quotes will work in HTML attributes.
There is an episode on railscasts addressing your situation using a serializer. The point is that the serializer replaces double quotes with $quot; The serialuzer also gives you the ability to select which attributes to serialize and include associations.
After some trial and error this is what works.
Server side:
<% #fields = [{:name => "id", :type => :integer}] %>
<%= form_tag( '/posts/new', {id:'example',:data => { :fields=>#fields}}) %>
Generated HTML:
<form accept-charset="UTF-8" action="/posts/new" data-fields="[{"name":"id","type":"integer"}]" id="example" method="post">
Javascript wiht JQuery's data method
fields = $('#example').attr('data-fields')
I'm using rails 2.3.8 and jquery-rails 2.2.1
Here is how I side-stepped the problem:
In the form tag, I did :"data-fields" => #fields.to_json.gsub('"',"'").
This produces HTML of this sort:
"data-fields"="[{'name':'id','type':'integer'}]"
And then, in the JS, I retrieve it like this:
$.parseJSON($('form').data('fields').replace(/'/g,'"'))
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" })
I know that link_to uses get action by default and also you can change the method to post by passing :method => :post to link_to function, but it does not seem to work. Here is the syntax that I am using:
= link_to "Export" export_path(:data_array => d_array), :method => 'post'
But this does not seem to work. The array is being passed by as a query parameter which I can see in the URL box and it bombs my application since it blows the string length limit in the url string.
Try using form instead:
= form_tag export_path do
- d_array.each do |val|
= hidden_field_tag 'data_array[]', val
= submit_tag 'Export'
Notice that in a controller params[:data_array] will be an array of strings.
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
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.