1901

How to limit character/word count on database string value in Sinatra/Active Record?

Question:

I have a column in my ActiveRecord database that I want to have a certain word limit.

Essentially, I've created a form that allows users to enter text(string). I want to limit the amount of characters that are allowed in that string.

@allposts = Post.limit(20)

This is what I have so far in the get method for the /current page that posts all of content. 20 = number of posts shown.

I also have a /new page where users will post new content.

Answer1:

You can limit the number of characters in a few different ways:

1.Defining the limit of the HTML field you create:

<input class="ip-input" id="ip" maxlength="15" name="ip" size="20" type="text" value="0.0.0.0" />

by changing the maxlength attribute. Example taken from <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_field_tag" rel="nofollow">here</a>.

2.Using the validates option in the user model:

validates :attribute_you_want_to_limit, length: { maximum: 50 }

You can find more about this option <a href="http://guides.rubyonrails.org/active_record_validations.html#length" rel="nofollow">here</a>.

3.Putting a limit in the schema:

t.string :your_attribute, :limit => 20

The first option won't allow the user to input any more in the field, the second won't allow saving the object and the third option won't let the attribute get saved to the database.

I recommend the second option.

You can also use Javascript in a few different ways, <a href="https://stackoverflow.com/questions/9841363/how-to-restrict-number-of-characters-that-can-be-entered-in-html5-number-input-f" rel="nofollow">here's</a> a good explanation on how to.

Recommend

  • SQL Server stored procedure runs fast in SSMS and slow in application [duplicate]
  • How to pre-filter showCommands in VSCode's quickOpen panel
  • Inno Setup Calling DLL with string as parameter
  • Angular4 - Nested route with params
  • Speed up Date#parse & Date#strptime in Ruby, more elegant way or best practice?
  • Max execution time error
  • E: Malformed entry 1 in list file /etc/apt/sources.list.d/sbt.list (Suite)
  • Tensorflow crash using tf.train.Saver() with GPU
  • Load php page results into div with ajax with link?
  • Signed Java web start application with Glassfish 4.1 and Java7
  • GTK3 + OpenGL for windows: multiple GtkGLArea (ie. OpenGL contexts)
  • Julia 1.0 UndefVarError - Scope of Variable
  • Autocomplete source from project settings
  • python selenium scraping tbody
  • How to redirect into different page by user type in php and mysql
  • how to specify different css for ie
  • How do I use libcurl to printf a remote FTP directory listing?
  • Annotate objects in a queryset with next and previous object ids
  • How to make 100% div height between header and footer?
  • Content-Type alternative in MQTT
  • JQuery: Infinite input select
  • Error handeling in antlr 3.0
  • how do i compare two rows and store the similarities of the two rows in another column
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • How to turn off notice reporting in xampp?
  • how to run ejabberd with Erlang on Heroku?
  • Terminal run dalvikvm with am.jar
  • How to specify generic type when the type is only known at runtime?
  • Change cell value based on cell color in google spreadsheet
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • Did not understand process of initialize in swift programming
  • How to encrypt Connectionstring written in web.config from codebehind?
  • Call Microservice from another Microservice within Docker
  • Sign a Pdf using custom digital signature in Java
  • Android Library Projects on Windows and Mac
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • WPF custom control and direct content support
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}
  • Time Complexity of Fibonacci Algorithm [duplicate]