views:

402

answers:

2

In "create method" i am using parameter name i.e :name => params[:name]

I have used truncate in html by adding :maxlength and :size to restrict length of name

but truncating in html can be skipped easily, so i want to know the code for "truncating the parameter :name => params[:name] in controller"

please suggest some code

A: 

You can do:

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

15 being how many characters you want to limit until (so this would be 16 in total).

Example:

>> 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"
Garrett
+4  A: 

I'd recommend doing this in your activerecord validations

That would keep your code cleaner, you can easily provide feedback to your users and it's a single line of code that then handles this anywhere it comes up in your application, something like:

validates_length_of :name, :maximum => 15
Mike Buckbee