I've built a lookup table in my Rails application.
It is a table that stores lookup text values for drop-down pickers or possibly check boxes. I want the lookup table to have a view associated so the values can be easily edited. I also may want to share lookup values among multiple models for a single field in the future.
So far I've managed to get it to work for the selection of values, but then displaying the text value again on a show or index view has been problematic.
This is how I built the lookup table
rails g scaffold Lookup field_name lookup_text table_name note
In the edit.html.erb where there is a lookup on a field, I've got code like this, which works and allows me to pick from a list.
<div class="field">
<%= f.label :status %><br />
<%= f.collection_select :status, Lookup.find(:all,:conditions => ["table_name = 'course' and field_name = 'status'"]), :id, :lookup_text, include_blank: true,:prompt => "Status" %>
</div>
That all works fine. When I try to display it back I cannot find the correct syntax. The best I have found is this: (in the controller)
@status = Lookup.where(:id => @course.status).pluck(:lookup_text)
(in the view)
<p>
<b>Status:</b>
<%= @status %>
</p>
I think I am getting the entire object. It displays like this:
Status: ["Active"]
My questions are: (1) How do I display the value only? (2) Is this the best approach?
I've had a look at these and other SO questions, but none are really what I am looking for:
Rails Polymorphic with Lookup Table
Implementing a lookup table in Rails
EDIT OK this works, but it doesn't look like it is the correct solution. Has anyone got a better way of doing this?
@status = Lookup.where(:id => @course.status).pluck(:lookup_text)[0]