Rails 4 as IFrame Facebook application - ruby-on-rails

I have problem with my fb application. I use this tutorial: http://3adly.blogspot.com/2012/12/ruby-on-rails-facebook-application.html I have app hosated on Heroku and box with authorization request doesn't work if i use app as iframe (https://apps.facebook.com/my-app). In chromium console i see error:
Refused to display 'https://www.facebook.com/dialog/oauth?client_id=123...%2Fmy-app.herokuapp.com%2F%2F&scope=read_stream' in a frame because it set 'X-Frame-Options' to 'DENY'.
But I have correct x-auth value im my config/application.rb:
module TestApp
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.action_dispatch.default_headers = {
'X-Frame-Options' => 'ALLOWALL'
}
end
end
When I use my app standalone (https://my-app.heroku.com) I see box with "permissions request", when I click OK my app works in IFrame and standalone. MY app works correctly, because I can see "Hi Grzegorz", in my view I have Hi <%= #user_profile["first_name"] %>!.
This is weird for me, why I don't see box with permissions request in IFrame?

I once had a similar problem, and it was the case that the redirect URL in the application configuration was different from the one used in the autorization URL. Also check whether http/https usage is consitent.
Furthermore, check whether the authentication link has the attrubute target="_top", see here: http://www.techguywebsolutions.com/facebook-app-getloginurl-issue.html

Related

Rails I18n default_locale and fallbacks by user preference

Rails internationalization has a nice fallback feature where empty linguistic versions don't show empty content, but a default locale's content via application configuration in application.rb
config.i18n.default_locale = :en
config.i18n.fallbacks = true
By inference, fallbacks may be set to true, but if the application knows not what locale to fallback upon, then there is breakage.
For multi-user application with fallbacks, what approach must be taken to have the fallback local not be the config.i18n.default_locale but a user defined one ?
You can overwrite the fallback configuration from the application.rb by setting the I18n.fallbacks on the app level. For example:
#application_controller
before_action :set_locale
def set_locale
......
I18n.fallbacks = current_user.fallback_locales
end
That being said, on the user account page might be a field/list of fallback languages that user can choose, so every time I18n goes to fallback it will display it in the preselected language.
Hope this helps.
Cheers

Setting Rails time zone in an initializer does not work?

Since Rails 5 the application.rb tells me to place my app's configuration in initializers. In order to follow this convention, I wanted to set my timezone in an initializer.
config/initializers/time_zone.rb
Rails.application.config.time_zone = "Paris"
Rails seems to ignore this setting and keeps its default time zone. Using the same code inside of my application.rb works. Why is the initializer being ignored?
Per Rails5 guide it should be configured in config/application.rb file.
#application.rb
class Application < Rails::Application
config.time_zone = 'Paris'
end

How to define application configurations in Rails 5?

I've normally put settings like the below in config/application.rb
config.generators.stylesheets = false
config.time_zone = 'Berlin'
But in Rails 5 the message below is found in config/application.rb
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
What does this mean? Am I supposed to add an initializer-file for every config setting? And in that case, what should such a file contain?
You should still be able to put the configuration in your config/application.rb, however the message is informing you that your environment specific configurations take precedence over those specified there, so if you have another configuration overriding any of such values in your config/environments those in the environment specific would be used.
If you're using the initializers approach, in your config/initializers/stylesheet_generator.rb, you'd have:
Rails.application.config.generators.stylesheets = false
and in your config/initializers/time_zone.rb, you'd have:
Rails.application.config.time_zone = 'Berlin'

Automatically reload rails yml files in config/locales

In rails, the yml files in config/locales allow you to give locale-specific text and formatting directives. For example, you can specify date formatting like this:
# config/locales/en.yml
date:
formats:
month: "%B, %Y"
Then in your views you can use the helper, like this:
<%= l(Date.today, format: :month) %> => "December, 2013"
Annoyingly, rails only loads the locale files when you start your server, so you have to restart your development server if you want to make a change. Is it possible to automatically reload this on file changes?
I think Rails misses new translation files, but adding translations to an existing file should work.
Try force reload it with I18n.backend.reload!
I hope this helps ;)
There's attempted support for this in rails 3.2:
https://github.com/rails/rails/blob/v3.2.16/activesupport/lib/active_support/i18n_railtie.rb
However, it comes with this disclaimer:
# Add <tt>I18n::Railtie.reloader</tt> to ActionDispatch callbacks. Since, at this
# point, no path was added to the reloader, I18n.reload! is not triggered
# on to_prepare callbacks. This will only happen on the config.after_initialize
# callback below.
There's some better looking code in rails 4, so this problem might be fixed there (I don't use rails 4 yet).
I added the following initializer, which checks for changed files is config/locales and reloads I18n:
# config/initializers/reload_locale.rb
if Rails.env == 'development'
locale_reloader = ActiveSupport::FileUpdateChecker.new(Dir["config/locales/*yml"]) do
I18n.backend.reload!
end
ActionDispatch::Callbacks.to_prepare do
locale_reloader.execute_if_updated
end
end
I18n detects changes made to existing files in its load path, but if you want to detect new files under locales and add them to the load path at runtime, try this.
# config/initializers/i18n_load_path.rb
if Rails.env.development? || Rails.env.test?
locales_path = Rails.root.join("config/locales").to_s
i18n_reloader = ActiveSupport::FileUpdateChecker.new([], locales_path => "yml") do
Dir["#{locales_path}/*.yml"].each do |locale_path|
I18n.load_path << locale_path unless I18n.load_path.include? path
end
end
ActiveSupport::Reloader.to_prepare do
i18n_reloader.execute_if_updated
end
end
That will monitor the locales directory (or any other directory you want to store locales) and add missing ones to the load path when they are added. I18n picks up on these added files so no need to call reload!.

How to change default timezone for Active Record in Rails?

In my application.rb I came across the following comment
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
config.time_zone = 'Eastern Time (US & Canada)'
As you see from above, I've made config.time_zone to EST time. However, still when records are created in the DB, it looks like datetime is being stored in UTC format.
In the above comment, they say
...and make Active Record auto-convert to this zone...
How can I do that, and where?
Also, I'll be deploying this on heroku as well and i'd like the setting to carry over
I have decided to compile this answer because all others seem to be incomplete.
config.active_record.default_timezone determines whether to use Time.local (if set to :local) or Time.utc (if set to :utc) when pulling dates and times from the database. The default is :utc.
http://guides.rubyonrails.org/configuring.html
If you want to change Rails timezone, but continue to have Active Record save in the database in UTC, use
# application.rb
config.time_zone = 'Eastern Time (US & Canada)'
If you want to change Rails timezone AND have Active Record store times in this timezone, use
# application.rb
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local
Warning: you really should think twice, even thrice, before saving times in the database in a non-UTC format.
Note
Do not forget to restart your Rails server after modifying application.rb.
Remember that config.active_record.default_timezone can take only two values
:local (converts to the timezone defined in config.time_zone)
:utc (converts to UTC)
Here's how you can find all available timezones
rake time:zones:all
adding following to application.rb works
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local # Or :utc
I came to the same conclusion as Dean Perry after much anguish. config.time_zone = 'Adelaide' and config.active_record.default_timezone = :local was the winning combination. Here's what I found during the process.
In my case (Rails 5), I ended up adding these 2 lines in my app/config/environments/development.rb
config.time_zone = "Melbourne"
config.active_record.default_timezone = :local
That's it! And to make sure that Melbourne was read correctly, I ran the command in my terminal:
bundle exec rake time:zones:all
and Melbourne was listing in the timezone I'm in!
If you want to set the timezone to UTC globally, you can do the following in Rails 4:
# Inside config/application.rb
config.time_zone = "UTC"
config.active_record.default_timezone = :utc
Be sure to restart your application or you won't see the changes.
I had to add this block to my environment.rb file and all was well :)
Rails.application.configure do
config.time_zone = "Pacific Time (US & Canada)"
config.active_record.default_timezone = :local
end
I added it before the line Rails.application.initialize!
In Ruby on Rails 6.0.1 go to config > locales > application.rb and add the following:
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module CrudRubyOnRails6
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.0
config.active_record.default_timezone = :local
config.time_zone = 'Lima'
# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.
end
end
You can see that I am configuring the time zone with 2 lines:
config.active_record.default_timezone =: local
config.time_zone = 'Lima'
I hope it helps those who are working with Ruby on Rails 6.0.1
On rails 4.2.2, go to application.rb and use config.time_zone='city' (e.g.:'London' or 'Bucharest' or 'Amsterdam' and so on).
It should work just fine. It worked for me.
for Chinese user, just add two lines below to you config/application.rb :
config.active_record.default_timezone = :local
config.time_zone = 'Beijing'
If you want local time to set, add the following text in application.rb
config.time_zone = 'Chennai'
# WARNING: This changes the way times are stored in the database (not recommended)
config.active_record.default_timezone = :local
Then restart your server

Resources