Changing the en.yaml for another one in rails - ruby-on-rails-3

I managed to get a fr.yaml. I want this to be the default, an old Rails 2.2 tutorial says to add:
config.i18n.default_locale = :fr
in environment.rb
It does not work, my rails server crashes on this config line.
What is the syntax for this setting in Rails 3? I do not want a multi-language app, I want to use only the french settings.

In Rails3, you add this line to application.rb
config.i18n.default_locale = :fr

This goes in config/application.rb in rails 3.

Related

Rack::Deflater works if only added to config.ru

Why am I getting Error: incorrect header check when I add
class Application < Rails::Application
config.middleware.use Rack::Deflater
...
...but not when I add use Rack::Deflater to config.ru? I double checked: gzip works and compresses responses.
Rails 3.2.15
jRuby 1.7.13
It's a bug of jRuby 1.7
https://github.com/rack/rack/issues/571
And it was fixed in 1.7.23, so I just had to upgrade.

In Rails 5, setting config.active_record.schema_format = :sql but still getting schema.rb created on db:migrate

Working on a Rails 5 app, I want to use structure.sql instead of schema.rb (we're using PostGIS with lots of custom SQL calls...). In config/initializers/database_options.rb I have the following:
# use structure.sql, not schema.rb
Rails.application.config.active_record.schema_format = :sql
If I run the following:
$ rake db:migrate
it generates db/schema.rb, not db/structure.sql.
The rails guides say:
There are two ways to dump the schema. This is set in
config/application.rb by the config.active_record.schema_format
setting, which may be either :sql or :ruby.
What magic am I missing here?
I think you should put your rails component config before Initializers.
The rails application initialize by the following order.
config/application.rb
Environment-specific configuration files
Initializers
After-initializers
You could put your config config.active_record.schema_format = :sql either in config/application.rb or config/environments/development.rb depends on environment you used.
That should work.
In your initializer do:
Rails.application.configure do
config.active_record.schema_format = :sql
end

How to set sinatra-authentication to use erb instead of haml?

I'm trying to set up the sinatra-authentication gem in a simple sinatra app, and running into an issue where sinatra can't find the correct views. I understand that sinatra-authentication uses haml by default, but I'm using erb in this app.
This in mind, I found in the sinatra-authenticaiton docs that there is a setting which allows you to change the template engine, by adding the following to your app file:
configure do
set :template_engine, :erb # for example
end
I've added this to my app.rb file, and sinatra is still looking for the signup.haml when I try to hit the /signup route in my app.
A couple of notes:
I've included the gem in my Gemfile, and successfuly run a bundle install on my app.
source 'https://rubygems.org'
gem 'sinatra'
gem 'data_mapper'
gem 'pg'
gem 'dm-postgres-adapter'
gem 'sinatra-authentication'
I saw something in the documentation that suggested that I may need to specify the location of my view files, so I added the following to my configuration block.
set :sinatra_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "views/"
**I think I've required the gem accurately in my app file by adding
require "sinatra-authentication"
use Rack::Session::Cookie, :secret => 'mys3cr3tk3y'
This gist is a current representation of my app.rb file in the root of my sinatra app. https://gist.github.com/rriggin/5378641#file-gistfile1-txt
Here is a screenshot of the error sinatra throws: http://cl.ly/image/0y041t0K3u3O
When I run the app locally, a 'dm-users' table is created in my local db as expected.
Is there another configuration setting that I'm missing in order to get sinatra-authentication to properly look for the erb templates rather than haml files. Any help would be greatly appreciated.
Thanks
The specs don't test that the template_engine setting works, and looking at the way the setting is called, I believe it's not correct, i.e.
send settings.template_engine, get_view_as_string("index.#{settings.template_engine}"), :layout => use_layout?
might better work as:
send app.settings.template_engine, get_view_as_string("index.#{app.settings.template_engine}"), :layout => use_layout?
that's what I reckon. If you fork the project, change the line and add it to your Gemfile and it works then consider writing a quick spec for it and you'll have improved the mainline of that project as well as fixed your problem.

Rails Active Admin resource problem

I recently have watched railscast 284 about active admin and wanted to implement it into my web app, however I am running into an issue when I add a resource. I get the following message every time I try to navigate to the created tab:
NameError in Admin::LoadsController#index
undefined local variable or method `per' for []:ActiveRecord::Relation
Rails.root: /Users/thomascioppettini/rails_projects/want-freight
Application Trace | Framework Trace | Full Trace
Request
Parameters:
{"order"=>"id_desc"}
Show session dump
Show env dump
Response
Headers:
None
The only thing I can think of that may affect the application is adding a recaptcha to devise, which active admin depends on.
For me, it looks like this is a pagination problem. What gem are you using? You should give as more details about your settup. Can you show us your resource file from admin directory? What version of rails and what ActiveAdmin are you using ?
If you are using the will_paginate gem, set the version to 3.0.pre2. I was using ~>3.0.pre2, which auto-updated to 3.0.2 when I ran a bundle update Reverting fixed the issue. If you're using Bundler, the line is this:
gem "will_paginate", "3.0.pre2"
I agree with Dawaid. It is a pagiantion error. Add "Kaminari" gem to you Gemfile. According to active admin docs, it is using kaminari for pagination.. will_paginate will also work for you as swilliams described...
As I understand active_admin doesn't support will_paginate anymore. But if you don't want to rewrite your pagination to Kaminari you can fix this problem with putting some code to initializers
# config/initializers/will_paginate.rb
if defined?(WillPaginate)
module WillPaginate
module ActiveRecord
module RelationMethods
alias_method :per, :per_page
alias_method :num_pages, :total_pages
end
end
end
end
module ActiveRecord
class Relation
alias_method :total_count, :count
end
end

Rails 3 - how do I avoid database altogether?

I'm trying to use rails 3 without any db backend, but it still insists on requiring 'sqlite3' gem when I try to access a page, and throws an error no such file to load -- sqlite3, even though no code in the application requires sqlite, except I left database.yml with its default setting for sqlite3, since removing the content raised other errors. Any idea how I could use rails without any database and avoid said errors? thanks.
(also, I'm familiar with Sinatra - just prefer rails for this project).
Rails 3:
In application.rb, remove the require 'rails/all' line and instead add these lines:
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
require "sprockets/railtie"
Also see Remove ActiveRecord in Rails 3
and look into the Active Model railscast
Rails 3.2.x:
You'll also need to remove/comment out this line in application.rb
config.active_record.whitelist_attributes = true
And remove/comment these two lines from development.rb
config.active_record.mass_assignment_sanitizer = :strict
config.active_record.auto_explain_threshold_in_seconds = 0.5
Rails 2.x:
In config/environment.rb add (or uncomment) the line
config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
This will cause Rails not to use those frameworks. (Note the nearly-invisible -= !)
Also, in Rails 3, remove any references to active_record in
config/environments/development.rb
config/environments/test.rb and
config/environments/production.rb such as
config.active_record.mass_assignment_sanitizer = :strict
config.active_record.auto_explain_threshold_in_seconds = 0.5
as well as removing require "rails/all" and adding the require lines in comment 21 (above).
if you are not using a database (this works with Rails 3.1.1)