Reputation: 2019
I am having a drop down in my rails application as below.
= form_tag({:controller=>"r4c", :action=>"result"}, method: :get) do
= label_tag(:q, "Trip Type: ")
= select_tag(:q, options_for_select([["Single load completed trip", "r4c_001"]]), class:"select")
= submit_tag("Get Test Details")
As we can see, i am passing the value [["Single....]] value directly into the options_for_select. I am trying to fetch this value from another class say a model and i have created a model class.
require 'active_record'
class R4cOptionsModel < ActiveRecord::Base
def country_options
return [["Single load completed trip", "r4c_001"]]
end
end
and the view form to
= select_tag(:q, options_for_select(R4cOptionsModel.country_options), class:"select")
but i am getting a error message as
undefined method `country_options' for #
What is the correct approach to do this. Thanks for your help.
Upvotes: 0
Views: 1170
Reputation: 4920
Your method country_options
is defined as instance method in class R4cOptionsModel
. So, either call it on an object of this class in the view:
= select_tag(:q, options_for_select(@r4c_option_model.country_options), class:"select")
Or, better if your options are more static, define the method as a class method using self
:
class R4cOptionsModel < ActiveRecord::Base
def self.country_options
[["Single load completed trip", "r4c_001"]]
end
end
... and keep view code as is.
Use this approach if you need these option values only in views. Define it in ApplicationHelper
or any other helper module.
module ApplicationHelper
def country_options
[["Single load completed trip", "r4c_001"]]
end
end
and in views:
= select_tag(:q, options_for_select(country_options), class:"select")
Upvotes: 1