Maximum length on a textarea in Ruby on Rails - ruby-on-rails

I tried applying the :maxlenght => 40 on a textarea on my form.
But it didn't work out.
Can we have a length limit on a textarea?
The code for text area is
<%= f.text_area :data,
:rows => 2,
:cols => 60 ,
:maxlength => 140,
:autocomplete => :off,
:class => "textareabytes" %>

Just like Rahul said, there's no maxlength attribute for textarea in HTML. Only text input's have that.
The thing you need to remember, is that RoR's text_area function (and all of RoR's HTML-generator functions) accept any argument you'll give them. If they don't recognized the parameter, then the'll just convert it to HTML.
<%=f.text_area :data, :hellothere => "hello to you too"%>
Will output this HTML:
<textarea name="data" hellothere="hello to you too"></textarea>
I know it's hard to remember, but Ruby on Rails isn't magic, it just does a lot of things for you. The trick is to know how it does them, so you can understand why they work, and how to fix them when they don't!

Could it be due to a typo?
":maxlenght => 40 " in your post is misspelt.
EDIT:
I didn't read your post carefully. I think there is no maxlength attribute for textarea in HTML. You will have to handle it in JavaScript. There is more information in "MaxLength on a Textarea".

Not strictly what you're after of course, but, you can always put a:
validates_length_of :data, max: 40
on your model. Won't stop the textarea size of course :)

You can use the maxlength attribute. It is new for the tag in HTML5. It should work nowadays.

Related

displaying formatted numbers in a rails simple_form view with angular js

I have a rails form created using the simple_form and haml with Angular JS code sprinkled in it. Angular is doing its job and computing the values fine. However, I have a display problem: I cannot display the totals being computed by angular and display it as a formatted value in a form control.
Here is the code snippet of the haml view:
= f.input :total_amount, :label => 'Invoice Total', :input_html => { :readonly => true, "ng-model" => "grand_total" }
So the form_control 'total_amount' is bound to the "grand_total" attribute on $scope for Angular.
Everything is working fine computationally. However, the total_amount input field is being displayed 'unformatted', e.g. 1655.3456. I would like to display it as currency, e.g., $1,655.00.
I do not know how to do it. I have tried to use the suggested approach of using number_to_currency rails view helper as shown below:
= f.input :total_amount, :label => 'Invoice Total', :input_html => { :readonly => true, "ng-model" => "grand_total", :value => number_to_currency(f.object.total_amount) }
But since the total_amount field is blank to begin with when I initialize the Rails view, it has no effect. Also, as the users change the nested form fields, the total amount is being updated dynamically by Angular. The number updates fine, but formatting is an issue.
Thanks in advance for any help.
You can use currency filter from angular https://docs.angularjs.org/api/ng/filter/currency
Because you are not using this field as input, try wrapping display value in something like span, then you can add filter like this
<span>{{total_amount | currency}}</span>
You can read about other options from the link
Or you can use directive from this answer https://stackoverflow.com/a/27051526/1805815

How to properly use number_to_phone or similar way to style a phone number input

I always like to mention that I'm fairly new to rails and building my first app. So I apologize in advance if the following question is too stupid.
I have a form with a :office_number that I would like to format. I'm running rails 4 with bootstrap 3. Here's my current code:
<%= f.input number_to_phone(:office_number, :groupings => [2, 4, 4], :delimiter => "-"), label: "Office #", :input_html => { class: "form-control" }, :label_html => { :class => 'form-label' } %>
I get no erros, but when I type the number, it doesn't get styled as it was supposed to. I want to achieve a number like this 48-1234-5679. Better yet, if is not asking too much, I want the number to show exactly like this (48) 1234-5678. Any help on how can I achieve that?
Check the docs. Maybe you need the area_code option.
GL & HF.

HTML_SAFE allow everything except <span style="font-size: TAG

How do I disallow font size change in html_safe rails 3
I have here a truncated description of an article, and I want to disallow big font sizes in display mode when the user inputs a big font size using tinymce editor
= truncate(event.description.html_safe, :length => 110, :omission => "...")
How can i do that?
You will want to use the sanitize helper before marking it as html_safe. Unfortunately for you, in this case, the blacklist functionality has been removed, so you will need to list literally all of the attributes you do want, in addition to the defaults. It may be easier to use a regex to remove the attribute in question.
Also, for what it's worth, raw(event.description) does the same as event.description.html_safe, but will not blow up on a nil value (not sure what your validation rules are), so it is generally preferred.
Edit:
Sanitize example usage (from http://apidock.com/rails/v3.2.8/ActionView/Helpers/SanitizeHelper/sanitize ):
truncate( raw( sanitize(event.description, :tags => %w(table tr td), :attributes => %w(id class style) ) ), :length => 110, :omission => "...")
Note: Truncating HTML like that can lead to some weird and hard-to-track-down errors, by creating invalid HTML because of cut-off end tags.

Rails best_in_place gem input has nasty extra space

I'm using Rails 3.2.3, Twitter Bootstrap, and the best_in_place gem to handle in-place editing.
The problem is that whenever I click to edit input, a bunch of extra space is placed to the right of the input.
Before:
After:
Has anyone else had this problem? What should I do?
I've tried explicitly setting the padding and margin to 0 and the width to a set pixel width - less than the td width.
Thanks.
Twitter bootstrap gives you wide range of input sizing classes. like 'input-small', 'input-large' etc.
try adding those classes for 'inner_class' attribute and it should apply on the input field.
<%= best_in_place #category, :name, {:inner_class => "input-small", :type => :input} %>
hope this helps :)
I recently ran into this issue myself. I passed the html attribute size using the :html_attrs option available in best_in_place, as follows:
<%= best_in_place #payment, :currency, :html_attrs => { size: 3 } %>
A size of 3 means an input box for 3 characters. This substantially reduced the input box expansion in my example.
I doubt that this is actually adding data to your database with extra spaces.
This is just a CSS issue, where your input field is designed to take up a certain percentage of the containing element or something like that. Hard to tell without seeing the exact CSS behind the input field.
I used cols and rows, as an alternative to size:
<%= best_in_place #payment, :currency, :html_attrs => { :cols => '45', :rows => '10' } %>

Simple Form - Translating options of a input field

I'm trying to use the i18n feature of simple form, which works great in most cases.
My only problem is, that in one case I want to use numbers as option values, so I can not simply create a symbol like in the other cases. Right now, I'm using this solution:
f.input :adm, :as => :select, :collection => [[:adm11 ,"11"],
[:adm00, "00"], [:adm06, "06"], [:adm99, "99"]]
Can I somehow make simple_form look up adm11 and so on in the usual way, so I can keep a sensible structure in my translation file?
I know I could do it with standard ruby i18n, but I'm looking for a better way.
f.input :adm,
:collection => [[:adm11 ,"11"], [:adm00, "00"], [:adm06, "06"],
[:adm99, "99"]],
:label_method => lambda { |el| t "define.i18n.keys.here.#{el.first}" }
I think you can't do it because of this line in SimpleForm:
collection_translated = translate_collection if collection_classes == [Symbol]
So it means that SimpleForm translates options if it's array of symbols. See discussion here https://github.com/plataformatec/simple_form/pull/302

Resources