简体   繁体   English

Rails合并表单字段

[英]Rails Merge Form Fields

I have a Rails 3.2.18 app in which I'm doing a simple create/update action on a model. 我有一个Rails 3.2.18应用程序,其中正在对模型执行简单的创建/更新操作。

There's a field called sms which I used ActionMailer to send a message to a person's phone in the form of 2812222222@vtext.com. 有一个名为sms的字段,我使用ActionMailer将该消息以2812222222@vtext.com的形式发送到某人的电话。 Currently I manually type this in which is fine, but I want other's to be able to enter a phone number, select a carrier and have these two items merge into the proper format in the sms field again being 2812222222@vtext.com. 目前,我可以手动键入此命令,但可以,但是我希望其他人能够输入电话号码,选择运营商,然后在sms字段中将这两项合并为正确的格式,再次为2812222222@vtext.com。 Makes it a lot more user-friendly than having to know the full sms-to-email address by heart. 使其比必须完全知道完整的短信至电子邮件地址更加友好。

I assume I can do most of this via JS/JQuery, but I'm not sure on how to get started. 我想我可以通过JS / JQuery来完成大部分工作,但是我不确定如何开始。

I'd like them to be able to enter the phone number in a 281-555-4444 format and have the carrier select as a drop-down of Tmobile, Verizon, etc and substitute @tmomail.net, @vtext.com, etc, strip (regex) the phone number and merge it with the carrier (@vtext.com) into the sms field. 我希望他们能够以281-555-4444格式输入电话号码,并选择运营商作为Tmobile,Verizon等的下拉菜单,并替换为@ tmomail.net,@ vtext.com等,剥离(正则表达式)电话号码,然后将其与运营商(@ vtext.com)合并到sms字段中。

If anyone can point me in the right direct, it would be appreciated. 如果有人能指出我正确的方向,将不胜感激。

Update: 更新:

I figured I could use a helper method to define the carriers: 我想可以使用辅助方法来定义载体:

def phone_carriers
{
  "All Tell" => "@message.alltel.com",
  "AT&T" => "@txt.att.net",
  "Boost" => "@myboostmobile.com",
  "Cellular South" => "@csouth1.com",
  "Centennial Wireless" => "@cwemail.com",
  "Cincinnati Bell" => "@gocbw.com",
  "Cricket Wireless" => "@sms.mycricket.com",
  "Metro PCS" => "@mymetropcs.com",
  "Powertel" => "@ptel.net",
  "Qwest" => "@qwestmp.com",
  "Rogers" => "@pcs.rogers.com",
  "Sprint" => "@messaging.sprintpcs.com",
  "Suncom" => "@tms.suncom.com",
  "T-Mobile" => "@tmomail.net",
  "Telus" => "@msg.telus.com",
  "U.S. Cellular" => "@email.uscc.net",
  "Verizon" => "@vtext.com",
  "Virgin Mobile USA" => "@vmobl.com"
}

end 结束

Add attr_accessor :carrier to the Person model 将attr_accessor:carrier添加到Person模型

Then in the form do something like this: 然后在表单中执行以下操作:

<%= f.input :phone, placeholder: "555-555-5555", required: true, input_html: {required: true} %>

<%= f.select :carrier, phone_carriers.map{ |k, v| [k, v] }, {include_blank: true}, {placeholder: "Select your phone carrier", class: "select2"} %>

But I'm still having problems figuring out how to strip the phone number to 5555555555 and merging the :phone field with the :carrier object into the :sms field automatically (preferably client side) 但是我仍然无法解决如何将电话号码剥离到5555555555以及将:phone字段与:carrier对象自动合并到:sms字段中的问题(最好是客户端)

After doing a lot of research and breaking things, I've come up with the following to make this work. 经过大量研究和突破后,我提出了以下解决方案。 The model and form only show what changes I've made, not the full form or model. 模型和表单仅显示我所做的更改,而不显示完整的表单或模型。

Form 形成

<%= f.label 'Cell_Phone'%>
<%= f.text_field :phone, placeholder: "xxx-xxx-xxxx", required: true %>
<%= f.label :carrier, "SMS Notifications", class: "control-label" %>
<%= f.select :carrier, phone_carriers.map{ |k, v| [k, v] }, {include_blank: true}, {placeholder: "Select your phone carrier", class: "select2"} %>
<%= f.hidden_field :sms %>

Model 模型

attr_accessible :carrier
attr_accessor :carrier

CoffeeScript CoffeeScript的

$ ->
  $("#person_carrier").on "change", update_sms_address
  $("#person_phone").on "keyup", update_sms_address
  update_sms_address

update_sms_address = ->
    digits  = $("#person_phone").val().match(/\d+/g)
    phone   = digits.join("") if digits
    carrier = $("#person_carrier").val()

    if phone? && phone.length == 10 && carrier
      sms = phone + carrier
      $("#person_sms").val(sms)
      $("#person-sms-number").html(sms)
    else if !carrier
      $("#person_sms").val("")
      $("#person-sms-number").html("Choose a carrier to receive SMS notifications")
    else
      $("#person_sms").val("")
      $("#person-sms-number").html("Invalid phone number")

This seems to work when entering a phone number in the format of 281-444-4444 as it strips the number down and joins it with the value from the carrier array and sets the hidden :sms field appropriately. 输入格式为281-444-4444的电话号码时,此方法似乎有效,因为它会精简该编号并将其与运营商数组中的值连接在一起,并适当设置隐藏的:sms字段。

The only problem I have now is since I'm using a virtual attribute for the carrier, if I go to edit the person again it doesn't retain the carrier select and leaves it blank. 我现在遇到的唯一问题是,因为我为载体使用了虚拟属性,如果我再次编辑该人,它不会保留载体选择并将其留空。 If I submit the form without changing any info it will retain the :sms field, but it could be confusing since the :carrier is not an actual field that is remembered by the database but instead a virtual attribute. 如果我提交表单时不更改任何信息,它将保留:sms字段,但是可能会造成混淆,因为:carrier不是数据库会记住的实际字段,而是虚拟属性。

Is there a better way to go about doing this? 有没有更好的方法可以做到这一点?

If you used the standard javascript string library, wouldn't something like 如果您使用标准的javascript字符串库,则不会像

function prepare(str){
  str2 = str.replace("(","").replace(")","").replace("-","")
  carrier = getCarrier //assuming this implemented by you somewhere 
  return (st2.concat(carrier))
}

I would start by creating a carriers table: 我将从创建一个载体表开始:

rails generate model Carrier name:string email_domain:string

And referencing it in the Person model by adding the following to your migration: 并通过在迁移中添加以下内容来在Person模型中引用它:

change_table :people do |t|
  t.references :carrier
end
add_index :people, :carrier_id

And the following to your People model: 以下是您的“人物”模型:

belongs_to :carrier
validates_presence_of :carrier

def sms
  carrier ? "#{phone}@#{carrier.email_domain}"
end
def phone_with_dashes
  "#{phone[0..2]}-#{phone[3..5]}-#{phone[6..9]}"
end
def phone_with_dashes=(_phone_with_dashes)
  self.phone = _phone_with_dashes.gsub(/-/ ,'')
end

Now, your view (I'll leave the formatting to you): 现在,您的视图(我将格式留给您):

Phone: <%= f.phone_field :phone_with_dashes %>
Carrier: <%= f.collection_select :carrier_id, Carrier.all, :id, :name %>
<div id='person-sms-number'>
  <%= f.object.sms %>
</div>

Lastly, the Coffeescript: The only thing that has to change here is you no longer need the '#person_sms' input since you are no longer persisting the value directly--you are generating it dynamically from other fields. 最后,Coffeescript:唯一需要更改的地方是您不再需要'#person_sms'输入,因为您不再直接保留该值-您是从其他字段动态生成它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM