1

I have a large "data" active record object whose info I filter based on what the user clicks on a page, then I display that filtered data to the user. I was wondering where the best place to put the data-filtering logic is. Right now I do the filtering in the rails controller action, so that in the view I only have to say something like

<script>
var filtered_data = <%= raw @filtered_data %>
alert('first piece of data is: '+filtered_data[0])
var filtered_names = <%= raw @filtered_names %>
</script>

and other such vars.

or, I was considering just passing the whole @data like:

<script>
var data = <%= data.to_json.html_safe %>
var filtered_data = **some js logic to filter data var**
var filtered_names = **more js logic to filter for names, maybe using the jquery .map function, etc **
</script>

which way is better for performance? I would think client-side JS is better, but it's probably not client side anymore since it's a .js.erb? Or would having more logic in the js.erb still help a bit?

Thanks for any insight!

1 Answer 1

1

you should put as few logic as possible into your view... if you filter the data via js, that data is beeing sent to the client (producing traffic).. so it is probably better to do the filtering on the server side.

always try just to display things in your view - this makes your code much more maintainable.

Sign up to request clarification or add additional context in comments.

1 Comment

the thing is, the default is to not filter the data at all, so I would still be sending all the data to be displayed in the view anyway

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.