对一个属性使用多个输入字段 [英] Using multiple input fields for one attribute

查看:143
本文介绍了对一个属性使用多个输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的模型中有一个datetime属性,我尝试把值放在它与3个表单元素。
第一个是日期,是一个输入表单(我使用bootstrap-datepicker-rails,我想坚持下去)。在第二个我想要一个选择框的小时,第三个是分钟。



所以我看到我可以使用DateHelpers datetime_select,但是那么我不能再使用bootstrap-datepicker了。
那么通过使用多个表单输入元素来填充属性(datetime)的正确方法是什么。我看到有一些像assign_multiparameter_attributes这样的东西,但是这个文件并没有太大帮助: - (



谢谢

对我来说,这似乎是控制器解决的一个问题,该模型仍然有效,如果以Rails方式接收到属性,我的解决方案涉及到控制器修改 params 哈希,以便模型甚至不知道视图中的更改,这取决于行为可能被视为私有API,所以...公平警告: - )



说你有这个模型:

 #app / models / example.rb 
class示例< ActiveRecord :: Base
attr_accessible:started_at#这是一个DateTime
end

调整您的视图以使用正常的 text_field_tag 作为日期部分(将通过引导日期标记增强)和Rails的 time_select 对于时间部分:

 #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 =>真实%>
< / p>

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

请注意,我们使用 try since此表单将被呈现给新的持久记录。当 started_at 属性为 nil 时,可以避免出现问题。



然后在控制器中创建一个 before_filter ,以便在发送到模型之前修改 params hash:

  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

本质上,我们正在解析 params [:started_at] as一个日期并将其分配给 params [:example] 的正确键。然后当参数最终传递给模型时,ActiveRecord将正确地分配模型的 started_at 属性。



我想做一些错误检查和验证,但这应该适用于您。


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

解决方案

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.

这篇关于对一个属性使用多个输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆