Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
931 views
in Technique[技术] by (71.8m points)

ruby on rails - Using multiple input fields for one attribute

I'm new to rails, so sorry if this one is too easy.

I have a datetime attribute in my model and I try to put the values in it with 3 form elements. The First one is for the date and is a input-form (I use bootstrap-datepicker-rails and I'd like to stick with it). In the second one I'd like to have a select-box for the hours and the third one is for the minutes.

So I saw I could use DateHelpers datetime_select but then I can't use bootstrap-datepicker anymore. So what's the proper way to to fill up a attribute (datetime) by using more then one form input element. I saw that there's something like assign_multiparameter_attributes but the doc doesn't quite help :-(

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To me, this seems like a problem for the controller to solve. The model is still valid and would work fine if it were receiving its attributes in the "Rails" way. My solution involves the controller modifying the params hash so that the model isn't even aware of the changes in the view. This depends on behavior in ActiveRecord that could be considered a private API, so…fair warning :-)

Say you have this model:

# app/models/example.rb
class Example < ActiveRecord::Base
  attr_accessible :started_at    # this is a DateTime
end

Adjust your view to use a normal text_field_tag for the date portion (which will be enhanced by bootstrap-datepicker) and Rails's time_select for the time portion:

# app/views/examples/_form.html.erb
<%= form_for(@example) do |f| %>
  <p>
    <%= f.label :started_at %>
    <%= text_field_tag :started_at, @example.started_at.try(:to_date) %>
    <%= f.time_select :started_at, :ignore_date => true %>
  </p>

  <p><%= f.submit %></p>
<% end %>

Notice that we're using try since this form will be rendered for new and persisted records. This avoids problems when the started_at attribute is nil.

Then create a before_filter in the controller to modify the params hash before it gets sent to the model:

class ExamplesController < ApplicationController
  before_filter :fix_params, :only => [:create, :update]

  # ...

private

  def fix_params
    date = Date.parse(params.delete(:started_at))

    params[:example].merge!({
      'started_at(1i)' => date.year.to_s,
      'started_at(2i)' => date.month.to_s,
      'started_at(3i)' => date.day.to_s
    })
  end
end

Essentially, we're parsing params[:started_at] as a date and assigning it to the correct keys of params[:example]. Then when the parameters are eventually passed to the model, ActiveRecord will assign the model's started_at attribute correctly.

You'll want to do some error checking and validation, but this should work for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...