Why is the word "notifications" causing i18n to break in rails? - ruby-on-rails-3

I'm having a really weird issue with i18n on a current rails project. I get translation missing messages whenever I attempt to use the key notifications. For example:
#en-US.yml
en-US:
notifications:
index:
title: 'notification page'
#/notifications/index.html.erb
<%= content_for :title, t('.title') -%>
This will fail and tell me the translation is missing
#en-US.yml
en-US:
notifications:
index:
title: 'notification page'
#/notifications/index.html.erb
<%= content_for :title, t('notifications.index.title') -%>
This will also fail.
#en-US.yml
en-US:
notification:
index:
title: 'notification page'
#/notifications/index.html.erb
<%= content_for :title, t('notification.index.title') -%>
This oddly, will work, removing the s will allow it to find the translation.
#en-US.yml
en-US:
notifications1:
index:
title: 'notification page'
#/notifications/index.html.erb
<%= content_for :title, t('notifications1.index.title') -%>
This also will work, adding a 1 to the end of notifications works.
It appears that rails does not like the word notifications. This is a problem because I don't want to have to rename the entire model for this, and also I want to use the i18n.t view shortcuts for consistency. Is notifications a reserved word? Is there any reason why it is failing to find it?

I'm not sure why it's failing in your case but I just had a go myself and it's not a reserved word, my locale file:
en:
notifications:
foo: bar
The result:
1.8.7 :001 > I18n.t('notifications.foo')
=> "bar"
This test was done with Rails 3.1.5 and Ruby 1.8.7.
In your case you got the following?
1.8.7 :001 > I18n.t('notifications')
=> "translation missing: en.notifications"

Although not an exact answer, I have found that there are a few words in I18n that cannot be used as keys. I am assuming the I18n code uses them elsewhere.
Others include "no", "yes", "on", etc.
Just using a synonym or do something like "notifications_" to make it different.

Related

Couldn't find Project with id=1 [WHERE "projects"."deleted_at" IS NULL]

I am using the paranoia gem.
I have a bunch of projects in my database but for somereason they are behaving as if they were deleted. When I navigate to my projects/1 route I get the error:
Couldn't find Project with id=1 [WHERE "projects"."deleted_at" IS NULL]
When I type this in my console:
Project.find(1).deleted_at
I get
nil
What is happening here?
This is my controller show action:
def show
#project = Project.find(params[:id])
#comments = Comment.all.where(:project_id => #project.id)
#updates = ProjectUpdate.all.where(:project_id => #project.id)
end
Error happens on
#project = Project.find(params[:id])
Here are some model Project scopes:
scope :by_category, lambda {|category| {:conditions => {:category_id => category.id}}}
scope :by_npo, lambda {|npo| {:conditions => {:npo_id => npo.id}}}
With Project.find(1) I get:
=> #<Project id: 1, name: "project 1", npo_id: 1, description: "project1 description", location_id:
4, singular_unit: "1", past_tense_action: "past tense action", conversion: #<BigDecimal:7547d78,'0.1
5E1',18(45)>, created_at: "2014-05-13 00:12:33", updated_at: "2014-05-22 01:20:51", future_tense_act
ion: "future tense action", plural_unit: "2", amount1: #<BigDecimal:75475b0,'0.1E2',9(36)>, amount2:
#<BigDecimal:7547520,'0.2E2',9(36)>, amount3: #<BigDecimal:7547490,'0.3E2',9(36)>, min_amount: nil,
other_amount: true, short_description: "project1 short description", featured_image_id: 3, deleted_
at: nil>
From my index page I link to it 2ice(this is the relevant code:
<div class="pj">
<h5><%= link_to project.name, project, :class => "button-link" %> </h5>
<hr />
<div class="index_featured_image">
<%= link_to image_tag(project.get_featured_image, :alt => "Project Title", class: "featured_image"), project %>
</div>
<div class="proj-content">
<p><strong><%= link_to project.npo.name, npo_path(project.npo) %></strong></br>
<%= project.short_description %></p>
</div>
<div>
<p class="project-loc"><i class="footicons fi-marker large"></i> <%= project.location.city_state %></p>
</div>
<div class="text-center">
<%= button_tag :type => "button", :class => "radius" do %>
<%= link_to "View More", project, :class => "button-link" %>
<% end %>
</div>
</div>
Couple guesses and some advice.
First the guesses. If Project.find(params[:id]) calls WHERE "projects"."deleted_at" IS NULL condition on the table, there must be a default_scope call on the Project. Though it's still weird why id prevents the find method from finding the correct project, if it's really not deleted. Let me see that default_scope (or better just post the whole model in a gist) and I might tell a bit more.
Now some advice. It's not really related to the current issue, but I believe, it will make your Rails life a lot easier these days.
Try not to use deprecated syntax, get on with the new one.
E.g. those scope calls should look like this
scope :by_category, ->(category){where(category_id: category.id)}
Don't use .all with .where. You just don't need it. The .where call will return an ActiveRecord::Relation object you are looking for.
Try to reduce the number of instance variables in controllers. For example you can omit the use of the #comments and #updates variables in your show action, provided that you have set the needed associations on the Project model.
has_many :comments
has_many :updates, class_name: ProjectUpdate
Then in the view you can refer to them as #project.comments and #project.updates without the need to store them separately. It will clean up the controller a bit. Also, if you want to ensure that the comments and updates are not lazy-loaded, you can load them together with the project by calling #project = Project.includes(:comments, :updates).find(params[:id]).
Hope that would be helpful. I'll update the answer would I have something more concrete on the problem.

simple_form error messages do not go away

I'm using simple_form with twitter bootstrap on Rails.
Everything works great, except when showing live validations in a form-inline class. My code for the form is:
<%= simple_form_for #message,
url: mailing_list_path,
html: { class: "form-inline" },
method: :post,
validate: true do |f| %>
<%= f.input_field :email_address, label: false %>
<%= f.submit "Submit" %>
<% end %>
This shows the error message properly (e.g. "is invalid"), but if I click off the input and then back on again, it adds another message (e.g. it would say "is invalid is invalid"). For example, two sequential invalid entries and then a blank entry would give:
Is there any way to have simple_form remove the existing error message before adding a new one?
EDIT:
I solved this using some ghetto js, but would still like to know if the functionality I mentioned above is built in. The divs are still there, they're just hidden instead of all showing together. Would be great to have them actually removed by the form validation...
$('input.email-address-input').on 'keyup', () ->
$(this).parent('form').siblings('.help-inline').hide()

Rails Flash was working, now it's not

Suddenly flash and params aren't working for me. Even my normal flash error/success messages don't work.
This is just one sample - I have the following in a view:
<%= link_to 'New Comment', new_comment_path, :class => 'btn btn-primary', :onclick => flash[:worequest_id] = #worequest.id %>
And in the new comment form:
<% if flash[:worequest_id] != nil %>
<%= f.hidden_field :worequest_id, :value => flash[:worequest_id] %>
I have been using flash to pass data between a view and a form in quite a few places in my Rails app. Now, none of them are working!!
Could I have set some configuration that would turn off flash and params?
I appreciate your help!!
UPDATE
I read on another post, where someone was having "session problems" with FLASH. Where would I look for session problems?
UPDATE2
I added the following to the view that receives the data from the flash:
<% if flash[:worequest_id].blank? %>
<h3>flash blank</h3>
<% end %>
And "flash blank" showed up.
Really - This has been in many places of my app for months. And now it's not working!!!!!!!
UPDATE3
I also tried this:
<% flash.keep[:worequest_id] = #worequest.id %>
Is there a rails config file that deals with session? Maybe I messed up a parameter or something.
UDPATE4
This is the only line in my initializers/session_store.rb file:
Ndeavor::Application.config.session_store :cookie_store, key: '_ndeavor_session'
UPDATE5
Params don't work either !!!!!
params[:worequest_id]
UPDATE6
I just created a new app from scratch using Rails 3.2.11 and flash and params are working fine. So, it's not the version of Rails.
Im a bit confused about what/how you're trying to achieve.
Have you looked at the output produced by this?
<%= link_to 'New Comment', new_comment_path, :class => 'btn btn-primary', :onclick => flash[:worequest_id] = #worequest.id %>
I removed the jbuilder gem and now flash and params are working. It has to be something I did, because I just tried jbuilder in another app, and it didn't break flash. I'm going to try and figure out why my use of the gem caused the problem.

Rails localization syntax

I've installed Twitter Bootstrap into my project and found unknown syntax for me:
<%=t '.title', :default => model_class.model_name.human.pluralize %>
<%= link_to t('.new', :default => t("helpers.links.new")),
new_article_path,
:class => 'btn btn-primary' %>
I can't understand the meaning of '.title', '.new' and 'helpers.links.new'. How do these constructions interact with the locale dictionary?
Also I've never met the construction :default => in t method, where I can read about it?
t is a helper method supplied by I18n internationalization mechanism of rails, and is a shortcut for I18n.translate method.
The locale file which I18n reads from is set by default to Rails.root/config/locales/en.yml assuming en is your default locale.
The first argument is the key which I18n will look for in your locale file.
The statement t('.new', :default => t("helpers.links.new")) means that I18n will look for the construct
en:
new: "new string"
in your locale file.
:default is the string which will be returned in case the first key was not found.
:default => t("helpers.links.new") just means that I18n will look for the following construct in en.yml:
en:
helpers:
links:
new: "new string"
and return it in case the first one was absent.
You can find here the full documentation of I18n translate method.

Routing error on Rails 3

I'm working on a profile page, so the config is like this:
routes.rb
=> match "user/:login(*path)" => 'users#profile', :as => :profile
rake routes
=> profile /user/:login(*path)(.:format) {:action=>"profile", :controller=>"users"}
on console
> Rails.application.routes.recognize_path("/user/example/whatever")
=> {:action=>"profile", :login=>"example", :controller=>"users", :path=>"/whatever"}
And I have a profile action in UsersControllers.
But when I use
&lt%= link_to user.name, profile_path(user.login) %>
in a view I get the error
No route matches {:login=>"example", :controller=>"users", :action=>"profile"}
What am I missing?
Thanks
Update:
Thanks for the answer and attention, Steve!
After a lot of time trying, a coworker find what I was missing: the problem was only with some logins that are emails too, with "#", ".", etc. The solution was adding to_url at params[:login] in link_to:
&lt%= link_to 'name', profile_path(params[:login].to_url) %>
Again, thanks for the attention!
Using your configuration, the route and helper seem to work fine in a Rails 3.0.5 sample app.
I verified the route helper profile_path in the Rails console:
>> app.profile_path('example')
=> "/user/example"
and checked that it worked in the view as well:
<%= link_to 'name', profile_path(params[:login]) %>
No route errors in either place. And putting <%= debug(params) %> in the view shows the path '/user/example/whatever' is being parsed correctly:
--- !map:ActiveSupport::HashWithIndifferentAccess
controller: users
action: profile
login: example
path: /whatever