user938363
user938363

Reputation: 10350

Why dynamic view erb file in rails from hash not working?

All, to compose dynamically a search.html.erb view, we are storing the details of each field that needs to be displayed as a hash or a hash represented as a string in a table. E.g. to display a status field, we are storing the related status layout as follow:

{:status => {:label => 'Status', :collection => return_misc_definitions('project_status') } }

Then my search.html.erb has the below pseudo code

<%= simple_form_for @model,  :method => :put, :url => @search_results_url  do |f| %>
      <%= @fields_details.each do |field, layouts| %>
          <%= f.input field, eval(layouts) %>
      <% end %>
<% end %> 

The goal is to end up with something like

<%= f.input :status, :label => 'Status', :collection => return_misc_definitions('project_status') %>

We are using the eval function of the "layout", but this is not working as the eval tries to evaluation the function return_misc_definitions. How can I escape this evaluation? We are using ruby 1.9.3, rails 3.2.12 and simple_form. Thanks for any help

Upvotes: 0

Views: 237

Answers (1)

Josh
Josh

Reputation: 5721

This worked for me:

<% @fields_details = {:status => {:label => 'Status', :collection => [1,2,3,4]} } %>

<%= simple_form_for :form,  :method => :put, :url => @search_results_url  do |f| %>
  <% @fields_details.each do |field, layouts| %>
    <%= f.input field, layouts %>
  <% end %>
<% end %>

I removed the = in <%= @fields_details.each do |field, layouts| %> to prevent @field_details from rendering on the screen then just passed the field and layouts into Simple Form directly, no eval necessary.

Upvotes: 1

Related Questions