All threads / eval issue in Rails view

Ask A Question

Notifications

You’re not receiving notifications from this thread.

eval issue in Rails view

Mark Nelson asked in Rails

Any idea why the following code :

<% model_set = instance_variable_set("@#{table_name}", eval("#{model_name}.all")) %>

produces this runtime error:

(eval):1: syntax error, unexpected '.'

The code correctly sets the instance variable @countries to the output from executing Country.all in the console or when using byebug (model_name is "Country" and table_name is "countries").

Should this be in a helper anyway?

Thanks in advance for assistance.

This was due to erroneous check in a containing loop which caused a blank model name to be used in the eval. The code is fine.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 33,399+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.