Is it possible to have deeply nested html_options with a link_to tag.
I want a link with bootstrap tooltip on hover.
In HAML, I can write:
%a{data: {bs: {toggle: "tooltip", placement: "top"}}, title: "baz", href: "bar"} foo
Which will output
<a data-bs-placement="top" data-bs-toggle="tooltip" title="baz" href="bar">foo</a>
But I'd prefer to use the rails helpers if possible, so:
= link_to "foo", "bar", {data: {bs: {toggle: "tooltip", placement: "top"}}, title: "baz"}
which generates
<a data-bs="{"toggle":"tooltip","placement":"top"}" title="baz" href="bar">foo</a>
So toggle and placement aren't being separated into their own tag option.
Looking through Rails on GitHub, I can see the relevant part which generates the tag options
link_to calls content_tag, content_tag calls content_tag_string, content_tag_string calls tag_options which is what generates the tag options from the hash that I provide.
tag_options will iterate through the hash and check if the value is another hash or a string, if its a string, it places it in the tag options, if it is a hash, it will iterate through that hash to generate more tag options. However it doesn't check any deeper than 1 hash.
So now I come to my question, without modifying rails, is there any way I can use the rails link_to helper while having a hash within a hash, or will I have to do something like this?
= link_to "foo", "bar", {data: {"bs-toggle": "tooltip", "bs-placement": "top"}}, title: "baz"}
Which does work, but I'd prefer to use symbols if I can for cleanliness.
Related
Given a hash of properties for a text_field to be made in HAML:
entry = {class: "form-control", disabled: true, id: :foobar}
How can I do this:
%dd!= f.send(:text_field, entry[:id], class: entry[:class], disabled: entry[:disabled])
But flexible? (So don't need placeholders, just a hash). Have looked at simple form docs and action view form helper docs and found input_html, but that's not working in this manner.
The f.send is obligatory, as :text_field can be anything. Would prefer not to use eval
text_field takes:
text_field(attribute_name, input_html_options)
Does
f.text_field(entry[:id], entry)
not work?
How, in rails, do you supply a dynamic hash to the source data attribute that x-editable accepts?
<a href="#" data-xeditable="true" data-type="select" data-pk="<%= g.id %>" data-model="graded_item" data-name="teacher_grade_id" data-url="<%= graded_item_url(g) %>" data-source="<%= g.gradescale.grade_scale_items.map{ |x| "{x.id, x.grade}" } %>" data-title="Edit Grade">
<%= g.teacher_grade.try(:grade) %> - <%= g.teacher_grade.try(:name) %>
</a>
The docs want a hash like { value: id, text: name } but I can't seem to get a hash that doesn't result in a red inactive select menu (x-editable doesn't like the source).
Anyone have an example of a dynamic data-source for x-editable in rails?
Per docs
If array - it should be in format: [{value: 1, text: "text1"}, {value: 2, text: "text2"}, ...]
For compatibility, object format is also supported: {"1": "text1", "2": "text2" ...} but it does not guarantee elements order.
update
This created an instance variable that is an array of hashes in the exact format the docs says is needed for an array data-source. But, this also results in an error empty select in the editable popup.
#grade_scale_items = []
#course.gradescale.grade_scale_items.each do |x|
#grade_scale_items << {value: x.id, text: "#{x.grade} - #{x.name}"}
end
Pretty simple fix...after digging a bit. The docs aren't real helpful with this, and the rails examples only uses text fields (no select menus). Convert the array of hashes to json...like so.
#grade_scale_items = []
#course.gradescale.grade_scale_items.each do |x|
#grade_scale_items << {value: x.id, text: "#{x.grade} - #{x.name}"}
end
view: data-source="<%= #grade_scale_items.to_json %>"
This puts it in a format that x-editable is happy with as a data-source
I'm just trying to create a simple select menu that takes you to a specific URL. So far I have something like this:
# haml
= form_tag new_something_path, method: :get do
= select_tag :type, options_for_select(my_array)
= submit_tag 'New Something'
However, when I submit the form I get the UTF8 parameter as well as a "commit" parameter with the text of the button.
How can I remove the UTF8 and commit parameters?
Removing the commit param is relatively simple, you need to specify that the input does not have a name:
submit_tag 'New Something', name: nil
Regarding the UTF-8 param...it serves an important purpose. Once you understand the purpose of the Rails UTF-8 param, and for some reason you still need to remove it, the solution is easier than you think...just don't use the form_tag helper:
# haml
%form{action: new_something_path, method: 'get'}
= select_tag :type, options_for_select(my_array)
= submit_tag 'New Something', name: nil
You can get rid of the utf8 param by adding the enforce_utf8: false option of form_tag (and also form_form) like the following:
= form_tag new_something_path, method: :get, enforce_utf8: false do
(thanks to #Dmitry for pointing that out)
But please make sure you don't need it: What is the _snowman param in Ruby on Rails 3 forms for? (I'm not sure if it is actually relevant for GET forms.)
The additional parameter is generated by the submit button can be removed by setting the name: false option on your submit_tag (Also works for submit in case of form_for).
= submit_tag 'New Something', name: nil
For example I have this code:
<%= link_to "Start", start_path(:id=>1,:box=>1)%>
the id and the box are parameters right?
and for example, it generated this url: http://localhost:3000/start?id=1&box=1
How can I add a string to it, to make look like this:
http://localhost:3000/start?id=1&box=1#box_1
Use the :anchor key:
<%= link_to "Start", start_path(:id => 1, :box => 1, :anchor => 'box_1')
And to answer your first question, yes, id and box are parameters. They are passed in to the request as part of the params hash.
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.