0

Is there any way to pass a variable through to an instance in a show partial? Basically I'm creating a service that follows CRUD and want to be able to iterate an instanced variable. I can create it by passing a partial like so:

In the parent view:

<%= form_for(@report) do |f| %>
     <%= render :partial => "reports/forms/partial_form", :locals => {:g => f, :var => "01"} %>
<% end %>

In the partial view:

<label><%= "Enter Data for Variable #{lane_number}" %></label>
<%= g.text_field :"Variable_#{var}" %>

This is useful the variables in the partial have a similar-enough naming convention to use the same var to iterate through several items. This works just fine.

I would like to do something similar when viewing the input data, but I can't figure out how to pass that var to the instance variable.

In the parent view:

<%= render :partial => "reports/shows/partial_show", :locals => {:var => "01"} %>

In the partial view:

<%= @report.Variable_#{var} %>

I know that #{var} is just used for string interpolation (which is why it works in the original create), but is there a way for me to do this by passing the variable to the partial?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
P. King
  • 3
  • 3
  • 1
    Welcome to Stack Overflow. We don't care if you're new or lack expertise in a language. We care a lot whether you've done your research and asked a detailed and concise question. Read "[ask]" and the linked pages, along with "[How To Ask Questions The Smart Way](http://catb.org/esr/faqs/smart-questions.html)", especially http://catb.org/esr/faqs/smart-questions.html#idm46227255999488 – the Tin Man Apr 14 '17 at 18:17

1 Answers1

1

You can use metaprogramming to get variable value because you just call a method:

<%= @report.send("variable_#{var}") %>
idej
  • 5,034
  • 1
  • 11
  • 14