Question

I'm trying to create a select list using the form_for helper:

<%= bootstrap_form_for(@lesson) do |f| %>
   <%= f.hidden_field('course_id', :value => @course.id) %>
   <%= f.select(:start_time, days_of_week_options_for_select) %>
   <%= f.text_field('starting_on', :value => Date.today.to_s(:short)) %>
   <%= f.text_field('ending_on', :value => "5 June") %>
<% end %>

(I'm using the bootstrap_form gem to make things pretty but that doesn't effect the issue, as subbing form_for for bootstrap_form_for doesn't change anything.)

It seems that the symbol :start_time above has to be a property of Lesson but I'd rather it not be as the field is only used during the creation of a new Lesson, but is not part of the Lesson model.

Specifically, this form serves to create multiple lessons and the actual date property on Lesson will be calculated depending on the value of the select dropdown.

Am I missing something in the bigger picture? Or do I need to learn some new syntax?

Was it helpful?

Solution

The easiest way here is to use attr_accessor on your model. This will not persist to DB / storage but creates a temporary attribute you can use to store the info.

attr_accessor :start_time

OTHER TIPS

The easiest way is to just avoid using form object helpers:

<%= form_for(@lesson) do |f| %> <%= f.hidden_field('course_id', :value => @course.id) %> <%= select_tag(:start_time, days_of_week_options_for_select) %> <%= ... %> <% end %>

After you get the form to compile, you can look at the output of your ids and input names and name the select tag the same way, then it will work just like your other form elements.

See this for documentation: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-select_tag

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top