저는 레일리 모델에 유효성 검사를 추가하고 있으며 유효성 검사기 중 하나에서 약간의 이상한 부분이 생겼습니다. 와, 내가 기본보기를레일즈 validates_length_of가 잘못되었습니다.
create_table "clients", :force => true do |t|
t.string "name"
t.string "last_contact"
t.integer "contacting_agent"
t.date "last_payment_date"
t.float "last_payment_amt"
t.datetime "created_at"
t.datetime "updated_at"
t.string "office"
t.integer "client_id"
end
: 그래서 여기
은 내가 (schema.rb에서) 함께 일하고 테이블
<%= error_messages_for 'client' %>
<h1>New Client</h1>
<% form_for @client do |new| %>
<table id='newform'>
<tr>
<th>Field</th>
<th>Value</th>
</tr>
<tr>
<td>
ID
</td>
<td>
<%= new.text_field :client_id %>
</td>
</tr>
<tr>
<td>
Name
</td>
<td>
<%= new.text_field :name %>
</td>
</tr>
<tr>
<td>
Office
</td>
<td>
<%= new.select :office, $offices %>
</td>
</tr>
<tfoot>
<tr>
<td>
<%= image_submit_tag "/images/icons/save_32.png" %>
<a href="/clients/new" title="Clear"><%= image_tag "/images/icons/close_32.png" %></a>
</td>
<td>
</td>
</tr>
</tfoot>
</table>
<% end %>
내 겸손 모델
class Client < ActiveRecord::Base
validates_length_of :client_id, :in => 5..7
validates_uniqueness_of :client_id
validates_presence_of :name, :client_id
end
그래서 내 엉덩이를 걷어차는 부분이 모델의 첫 번째 유효성 검사입니다. 내가 브라우저에 떨어져 머리와 뷰를로드 (/ 고객/신규), 나는 CLIENT_ID와 이름을 입력하면
validates_length_of :client_id, :in => 5..7
후 제출 공격, 사무실을 선택합니다. 유효성 검사기는 :client_id
을 올바르게 선택하지 않습니다. "너무 짧음"또는 "너무 긴"오류 메시지가 나타나기 때문에 항상 실패합니다.
키커는 약 11자를 입력하고 12자를 입력 할 때까지 "너무 짧음"오류가 발생한다는 것을 알고 있습니다. 따라서 11은 "너무 짧음"의 임계 값이며 범위가 "5.7"이라고 가정합니다. 그러나 "너무 긴"메시지 대신 실제로 유효성을 검사하여 레코드를 삽입하지만 기록합니다 inserts는 "client_id"와 완전히 다른 번호를 가지고 있으며, validates_uniqueness_of
에도 불구하고 항상 동일합니다.
내가 생각하는 것은 :client_id
이라는 실제 필드 client_id의 유효성을 검사하는 대신 객체 id를 선택하여 유효성을 검사하는 것입니다. 적어도 이것은 내가 생각할 수있는 유일한 것입니다.
Parameters: {"x"=>"13", "y"=>"14", "authenticity_token"=>"removed", "client"=>{"name"=>"test345", "client_id"=>"12345678", "office"=>"US10"}}
위는 서버 로그에서, 그래서 :client_id
,이 불확실성을 보정 할 수있는 방법이 제발에 대한 "너무 짧다"로 유효성을 검사? (참고 : validates_length_of "client_id", :in => 5..7
을 시도했지만 검증이 전혀 없음)
감사 나에게 그 지적에 대해. 오늘 하루는 분명해 보인다. 'validates_format_of : client_id, : with =>/\ A \ d {5,7} \ z /'로 전환하고 regex를 사용합니다. –
데이터베이스의 정수 열인 경우 양식의 값이 문자열이기 때문에'validates_inclusion_of'가 작동하지 않습니다. 당신은': greater_than_or_equal_to'와': less_than_or_equal_to' 옵션으로'validates_numericality_of'를 원합니다. –
실제로 필드가 할당 될 때 value는 integer로 변환되기 때문에됩니다. '>> c = Client.new (: client_id => "22222")'; '>> c.client_id'; '=> 22222' – Voyta