2009-10-02 5 views
1

내가 사용하고 매개 변수 이름, 즉 : 이름 => PARAMS [: 이름]루비 - 잘라 내기 매개 변수 "방법을 생성"에서

나는 를 추가하여 HTML에서 잘라 내기를 사용했다 : 최대 길이를 실행 한 다음, 크기를 PL

이름

의 길이를 제한 할 수 있지만, HTML의 절단을 용이하게 생략 될 수 있고, 그래서 "컨트롤러 파라미터 절단 : 이름 => PARAMS [이름]" 위한 코드를 알고 싶어 몇 가지 코드를

답변

1

을 제안 완화 당신은 할 수 있습니다 :

:name => params[:name][0..15] 

(15)는 (그래서이 총 16 것이다)까지 제한 할 수있는 문자 인.

예 :

>> lipsum = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." 
=> "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." 
>> lipsum[0..10] 
=> "Lorem Ipsum" 
4

내가 당신의 액티브에이 일을 권하고 싶습니다 validations, 당신은 쉽게 사용자에게 피드백을 제공 할 수 있으며 단 한 줄의 코드의 코드 청소기를 유지하는 것

그 다음과 같이 애플리케이션에서 발생하는 모든 위치를 처리합니다.

validates_length_of :name, :maximum => 15 
관련 문제