Rails dealing with single and double quotes - ruby-on-rails

I have a problem in dealing with single quoted characters in en.yml, in my erb file I have a line of code,
<input type="text" class="something" id="myUsername" placeholder='<%= I18n.t("entries.email") %>' value="<%= user.email%>">, and corresponding yml is,
entries:
email: "Entrer le nom d'utilisateur"
The problem here is that the string after apostrophe (d') gets truncated, I could resolve it by using double quoted string for placeholder, that way it worked, but is that a good solution, can someone suggest me a better way to deal with this.
Note: Tried escaping single quote but didn't work for some reason.

Write it using text_field_tag helper :
<%=
text_field_tag "user[email]", user.email, id: "myUsername",
placeholder: I18n.t("entries.email"), class: "something"
%>
It is easy to use.

Related

How to change the message when a form input doesn't match the pattern parameter?

A pattern argument can be provided to a form field as described here
Example (regex from here)
<%= f.text_field :username, pattern: "([A-Za-z0-9\-\_]+)" %>
When the regex is not adhered to, a message is displayed. E.g.:
How can that message be customised?
The pattern attribute is actually a HTML spec, not a Rails thing. You can use the title attribute to give users a hint on to what format is expected.
<%= f.text_field :username, pattern: "([A-Za-z0-9\-\_]+)",
title: "A username can only contain letters, numbers, hyphens and underscores" %>
Find more information here: https://html.spec.whatwg.org/multipage/input.html#the-pattern-attribute

How to write correctly <%= t('.title_#{Time.now.wday}') %>?

Usually to add a word (for my multilanguage website) I use this system: <%= t('.title') %>
Now in config/locales/en.yml, I added something like this:
en:
home:
title: Title
title_0: Finally Sunday! Watch this title
title_1: Hey! It's monday! This is the Title
And I want to use Time.now.wday and to create something like <%= t('.title_#{Time.now.wday}') %>
But it doesn't work. How to write it correctly?
That's because you need double quotes for string interpolation in Ruby. Just replace
<%= t('.title_#{Time.now.wday}') %>
for
<%= t(".title_#{Time.now.wday}") %>
although it maybe should be
<%= t("home.title_#{Time.now.wday}") %>
because title, title_0 and title_1 are nested under home
Interpolation does not work with single quotes
That means that:
puts "Works in double quoted strings: #{1 + 2}."
puts 'Not not work in single quoted strings: #{1 + 2}.'
will print out:
"Works in double quoted strings: 3."
"Does not work in single quoted strings: #{1 + 2}."
source: http://ruby-for-beginners.rubymonstas.org/bonus/string_interpolation.html

I want to use labels for my Rails 4.0 application such that they appear on the form as well as in activerecord error messages

I have checked several posts here and none of them seemed to work for me. I don't know what I am missing.
The ruby documentation here http://apidock.com/rails/ActionView/Helpers/FormHelper/label, seems to have the answer, but it is not clear how to interpret some text. For eg.,
helpers:
label:
post:
body: "Write your entire text here"
Is this the text one should put in the en.yml file as-is (replacing the post with my own model name and body with my own field name etc?) I tried that and it didn't work. Then I tried to put this text in the app/helpers/"modelname"_helper.rb file. Didn't work there either.
helpers:
label:
representative:
fname: "First Name"
After these edits, in my view I have the code as follows:
<%= f.label :fieldname %>
In my case:
<%= f.label :fname %>
At this point when I run my app, I am expecting my custom label that to be shown on the form. It doesn't. It just shows Fname
When I see similar posts on stackoverflow even there I see the same convention being used. Looks like I have two issues going on. 1) Understanding this convention and how to interpret it and 2) The solution to my actual issue itself.
I know I can use <%= f.label :fname, 'First Name' %>, but because I am validating for fname in the model for presence, the error message says "Fname is required". I would like it to say "First Name" is required.
How else can I do this?
Please help.
If you want to translate the names of your model attributes, put something like the following in config/i18n/en.yml:
activerecord:
attribute:
representative:
fname: "First name"
body: "Write something here"
Error messages for specific attributes and specific errors can be overriden too:
activerecord:
errors:
models:
representative:
attributes:
body:
blank: "Body cannot be blank"
But I think your best option is to create a new translatable for the custom body label. In your form:
= f.label :body, t(:write_text)
In your en.yml:
write_text: "Write something here"

When using translate rails 4.1 sets data value incorrectly

I'm saving some translation data in the data attribute of a DOM element.
<input type='button' id=admin-button data-add= <%=t :Add_Category%> data-save= <%=t :Save%> value= <%=t :Add_Category%>
unfortunately when the translation file value has a space such as Add_Category:"Add Category"
I get
data-add="Add" category=""
instead of
data-add="add category"
it works if i do it without the translation in between.
Put double quote the around the erb tags? or better use the rails helpers, maybe button_tag
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-button_tag
<%= button_tag t(:add_category),
id: "admin-button",
data: {
add: t(:add_category),
save: t(:save)
}
%>
this renders a <button> if you must have input type="button", maybe this would work
<%= content_tag :input,
t(:add_category),
type: "button",
id: "admin-button",
data: {
add: t(:add_category),
save: t(:save)
}
%>
http://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag
Also I recommend using all lower case with underscores - i.e. :add_category vs :Add_Category - it is more in the ruby style

image_tag syntax with or without "" - with method - ruby

To "" or not to "" - that is the question.
<%= image_tag(#profile.image, :size => "80x80", :alt => "Picture") %>
or
<%= image_tag("#profile.image", :size => "80x80", :alt => "Picture") %>
Ask yourself this questions: Do you ever quote variable's in any programming language? Probably not. Some languages handle variables inside of double quotes so that you can write a string like this: "My name is <variable>". The other way to do it is "My name is "+<variable>
So basically no you do not want to quote "#profile.image".
Using "#profile.image" will put the src attribute equal to #profile.image, NOT the value of #profile.image.
Don't use the quotes, unless your image is called "#profile.image".
If the image's filename is #profile.image, then use quotes. If #profile.image contains the name of the image file, don't.

Resources