我的SMS付款系统效果很好。现在我面临问题。
当用户点击新广告时:
def new
@advertisement = Advertisement.new
retries = 0
loop do
@advertisement.identifier = ('0'..'9').to_a.shuffle.first(5).join
retries += 1
break if retries == 5 || Advertisement.find_by(identifier: @advertisement.identifier)
end
@int = @advertisement.identifier.to_i
@compare_identifier = ((@int + 99)*7)+2
respond_with(@advertisement)
end
这样就生成了identifier和compare_identifier。
在用户创建新广告之前,他需要通过短信付款。
SMS看起来像这样:ABC 12345-ABC是服务标识符,12345是广告标识符。
收款将在SMS#RECEIVE控制器中处理,该控制器首先将识别广告,然后通过与Advertisement#new中相同的功能生成compare_identifier:
@int = @advertisement.identifier.to_i
@compare_identifier = ((@int + 99)*7)+2
然后,将此compare_identifier发送回发送SMS的用户。然后,他输入此代码,如果匹配,则可以创建该广告。
在广告#_form中:
<%= form_for @advertisement,:html => {:multipart => true, :class => "form-horizontal advertisement" } do |f| %>
.....
<%= f.hidden_field :smsidentifier, :value => @compare_identifier%>
<%= f.text_field :smsidentifier_confirmation %>
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
advertisements_path, :class => 'btn btn-default' %>
<% end %>
Advertisement.rb
validates :smsidentifier, confirmation: true
validates :smsidentifier_confirmation, presence: true
然后我得到了这个错误:
Smsidentifier confirmation translation missing: lv.activerecord.errors.models.advertisement.attributes.smsidentifier_confirmation.blank
我相信是因为那个hidden_field吗?
将该字段隐藏起来非常重要,这样就不会出现安全漏洞。
当我尝试创建广告时,参数散列:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZIevYutJzBvZoAxseuaAvIqhqKWfL2Qr4PBxsO4zvJs=", "advertisement"=>{"user_id"=>"24", "name"=>"liu", "country_id"=>"1", "region_id"=>"4", "age"=>"41", "height"=>"152", "phone_number"=>"2222222", "weight"=>"58", "email"=>"operins@gmail.com", "description"=>"4141", "provaider"=>"ipo", "your_ip"=>"i;", "terms_of_service"=>"1", "smsidentifier"=>"588807", "smsidentifier_confirmation"=>"588807"}, "hour_ids"=>["1"], "service_ids"=>["2", "7"], "images"=>[#<ActionDispatch::Http::UploadedFile:0x00000005e8d078 @tempfile=#<Tempfile:/tmp/RackMultipart20141217-13535-1glcbic>, @original_filename="sludinajums_ruby1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"images[]\"; filename=\"sludinajums_ruby1.jpg\"\r\nContent-Type: image/jpeg\r\n">], "commit"=>"Create Advertisement", "locale"=>"lv"}
还有其他方法吗?还是我的方法不好?
最佳答案
您是否在“强参数”中将该字段列入了白名单?
https://github.com/rails/strong_parameters
如果不是,它将在控制器中无效。