Store several text_field strings to array Rails 3 -
i'm trying store strings couple of text_fields have array, , save db. have column named "opening_hours" wich i've tried separate 2 different attributes using virtual attributes this:
model
class venue < activerecord::base attr_accessible :opening_hours, :start_time, :end_time attr_writer :start_time, :end_time def start_time @start_time.nil? ? @start_time : opening_hours.to_s.split("-").first end def end_time @end_time.nil? ? @end_time : opening_hours.to_s.split("-").last end end
the idea type in start_time , end_time this:
view
<%= form_for @venue |v| %> <p><%= v.label "monday" %><%= v.text_field :start_time %>-<%= v.text_field :end_time %><p/> <p><%= v.label "tuesday" %><%= v.text_field :start_time %>-<%= v.text_field :end_time %><p/> <p><%= v.label "wednesday" %><%= v.text_field :start_time %>-<%= v.text_field :end_time %><p/> <% end %>
the array should in db:
{08-12|09-14|07-13}
with "|" separating different days of week.
i've tried couple of things in controller like:
controller
class venuescontroller < applicationcontroller def new @venue = venue.new end def create @venue = venue.new(params[:venue]) @total_time = params[:venue][:start_time]+"-"+params[:venue][:end_time] @venue.opening_hours = @total_time.map {|t| [t.start_time, t.end_time]} if @venue.save redirect_to venue_path(@venue), :notice => "success!" else render 'new' end end
but nothing seems work... either saves start_time , end_time last day, or nothing gets saved @ all.
i perhaps may have misunderstood, but...
in create action, there no @venue.save - why not seeing saved.
class venuescontroller < applicationcontroller
def new @venue = venue.new end def create @venue = venue.new(params[:venue]) @total_time = params[:venue][:start_time]+"-"+params[:venue][:end_time] @venue.opening_hours = @total_time.map {|t| [t.start_time, t.end_time]} @venue.save end
end
Comments
Post a Comment