3

Is there a way of attaching data to elements in Rails like jQuery. For instance if you have

var element = $('element');
element.data('i', 'Hello world');

in jquery, you can retrieve that data later:

console.log(element.data('i));

will result in

Hello world

this way it stores the information in an obscure place resulting in a variable that isn't accessible from outside of jQuery directly.

In contrary, right now I'm only aware of appending HTML data attributes using Rails:

<%= content_tag(:div, "Something", data: { i: 'Hello world' }) %>

Which is equivalent of setting a .attr() in jQuery.

Setting HTML data attributes results in:

  • visibility on the elements upon inspection
  • cannot be objects

So essentially, what I'm trying to ask here is, can I append jQuery data set with Rails so it will be:

  • accessible only from .data()
  • not accessible from .attr() or anywhere else
  • not publicly visible on the element upon inspection
3
  • What's wrong with data attributes? Commented Feb 25, 2017 at 21:57
  • @muistooshort They can be viewed in the source code. Commented Feb 25, 2017 at 22:02
  • 1
    Anything that is stored client-side is easily visible. Anything that is accessible at all in the client is easily visible. Commented Feb 25, 2017 at 22:07

3 Answers 3

1

Is there a way of attaching data to elements in Rails like jQuery

Example:

# in your view *.html.erb
<%= content_tag(:div, "Content", data: { whatever: 42, whenever: "5pm" }) %>

Results in:

<div data-whatever="42" data-whenever="5pm">Content</div>
Sign up to request clarification or add additional context in comments.

Comments

1
+50

Option 1

Have a look at gon, which makes it trivially simple to access your Rails variables in JavaScript. It doesn't attach them as data attributes, but at the same time you are sending the data to the client all in one go, so it will be available in the page source.

(This Railscast episode also walks you through the setup, and how it works)

Option 2

If you really need the data to be hidden/obfuscated (I assume this is from scrapers, so that's what I'll focus on here), and you don't want to do this client side, then you'll need to set up a new route in your Rails application, and do AJAX calls to that endpoint to return the data. It's not foolproof, but there's options here to make things a bit more difficult for scrapers.

Comments

0

Your best best is to use a Javascript class and store them on the object as "variables" or method calls. There is a good example of this in the book Javascript: The Good Parts, in the section regarding classes.

Although the variables would still be accessible if someone really wants to get at them but much more obfuscated. Another approach is to leverage the local session via Javascript, again still retrievable by the client but somewhat obfuscated. Basically, if you do it on the client side, security is a tough nut to crack.

Also, FWIW your question has nothing to do with Rails, this is all client side Javascript, although it might be in a web app that is Rails based it's still Javascript.

One option you could do is AJAX calls to the Rails backend to retrieve the data via specific Rails routes, i.e. an API, but I'm not sure that gains you what you're looking to acheive.

1 Comment

Yes, I figured my question doesn't make sense after reading it myself again. Good points.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.