Rails Routing Error only in production - ruby-on-rails-3

Hello i have this in routes.rb
namespace :admin do
root :to => "admin#index"
resources :employees
resources :customers
resources :users
end
frontend normally works, i can login to administration but there i have link like
<li><%= link_to "users", admin_users_path %></li>
etc..
if i click on that link i get this error
uninitialized constant Admin::UsersController
or if i click on admin_employees_path i get
uninitialized constant Admin::EmployeesController
and that behavior is at every link in administration
at server with rails s everything is fine :p
user controller is defined like that
class UsersController < Admin::AdminController
files location
controllers/admin/admin_controller.rb
controllers/users_controller.rb
My environments files
development.rb
Web::Application.configure do
config.cache_classes = false
config.whiny_nils = true
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_mailer.raise_delivery_errors = false
config.active_support.deprecation = :log
config.action_dispatch.best_standards_support = :builtin
config.active_record.mass_assignment_sanitizer = :strict
config.active_record.auto_explain_threshold_in_seconds = 0.5
config.assets.compress = false
config.assets.debug = true
end
production.rb
Web::Application.configure do
config.cache_classes = true # different
config.assets.compress = true # different
config.consider_all_requests_local = true # temporary true
config.action_controller.perform_caching = false
# not in development
config.serve_static_assets = false
config.assets.compile = true
config.assets.digest = true
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
end

You are defining your users resource under the admin namespace but not defining it in the controller.
Move your users_controller to /controllers/admin/users_controller and append the namespace to that class declaration
class Admin::UsersController < Admin::AdminController
Or move your
resources :user outside the admin namespace.

Related

Model with acts_as_tenant fails validation after Rails 5 update

rails 5.2.1 ruby 2.5.1
My Model
class InputForm < ApplicationRecord
acts_as_tenant(:tenant)
end
InputForm.validators shows
#<ActiveRecord::Validations::PresenceValidator:0x000000000baaae28
#attributes=[:tenant],
#options={:message=>:required}>
This is not allowing me to create the InputForm without tenant.
Note : I don't have any config setup (config.require_tenant = true )
or file like config/initializers/acts_as_tenant.rb
What i'm doing wrong ?
Have you tried the optional: true options when specifying acts_as_tenant?
class InputForm < ApplicationRecord
acts_as_tenant :tenant, optional: true
end
OR
You can configure your rails 5 application like so
# config/application.rb
...
module YourProject
class Application < Rails::Application
...
# Make the belongs_to value as false by default in Rails 5
config.active_record.belongs_to_required_by_default = false
...
end
end
Responded here as well.
https://github.com/ErwinM/acts_as_tenant/issues/196#issuecomment-460605781

Carrier wave without s3 storage

I am using carrier wave for image uploads and for testing purposes I don't want to use s3 storage.
This is the carrierwave.rb file
CarrierWave.configure do |config|
config.permissions = 0666
config.directory_permissions = 0777
config.fog_directory = 'xxx-development'
config.storage = :file
config.enable_processing = false
end
And in the ImageUploader class I have
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process resize_to_fill: [300, 300]
end
And when I push to heroku I get the following error:
rake aborted!
Fog provider can't be blank, Fog directory can't be blank
What is it I am doing wrong ?
If you want it conditional, perhaps set an environment condition on the fog_directory?
CarrierWave.configure do |config|
config.permissions = 0666
config.directory_permissions = 0777
config.fog_directory = 'xxx-development' unless Rails.env.development?
config.storage = :file
config.enable_processing = false
end

Access session in Helper file ? Rails 3

how to get session in helper file?
UserHelper.rb
module UsersHelper
def self.auth login, password
user = Users.where("firstname = :firstname AND password = :password", {:firstname => login, :password => password})
if user != []
return true
else
return false
end
end
def self.is_auth? level
puts #session
user = Users.where("firstname = :firstname AND password = :password", {:firstname => #session[:firstname], :password => #session[:password]})
if user != []
return true
else
return false
end
end
end
Admin_controller.rb
class AdminController < ApplicationController
include Rails.application.routes.url_helpers
def initialization
#session = session
end
def index
#session = session
if UsersHelper.is_auth?(2)
render :text => "ssssss"
end
end
def auth
if params[:send] != nil
if UsersHelper.auth params[:firstname], params[:password]
session[:firstname] = params[:firstname]
session[:password] = params[:password]
redirect_to :action => "index"
else
#error = 1
end
end
end
def exit
session.delete(:firstname)
session.delete(:password)
render :json => session
end
end
Error
undefined method `[]' for nil:NilClass
app/helpers/users_helper.rb:13:in `is_auth?'
app/controllers/admin_controller.rb:8:in `index'
Only Controller can access session.
So, in a nutshell, if you are going to use this method in Controllers only like what is you case, you can define it as ApplicationController's method. Or define it a module and include it in AppplicationController.
class ApplicationController < ActionController::Base
def auth
end
def is_auth?
end
end
If you want to use the method in both controller and view, just declare them as helper_method
class ApplicationController < ActionController::Base
helper_method :auth, :is_auth?
def auth
end
def is_auth?
end
end
Ref: http://apidock.com/rails/ActionController/Helpers/ClassMethods/helper_method
Another note: In my opinion it's really not worth the time to build auth system from scratch by yourself. The functionalities are not easy but quite general. There are well baked gems such as Devise, Authlogic. Better to use them.

ruby on rails action mailer - confirmation email not being sent

I'm working on a ruby on rails project made mostly by someone else, and my rails knowledge is so-so.
The registration process works like this (or used to, but doesn't seem to now, for some reason):
A person registers with name, email, password. (All done with Devise)
The person is sent an email, asking them to click on a link, and when they do, they are then a registered user and can log in to the app with their name and password.
When they do step 1 and click 'Sign Up' a message comes up on the screen, 'Congratulations! Check your email and click the link.'
But no email is being sent. In my App/config/environments/development.rb I have:
QuestionnaireSite::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
# config.action_mailer.delivery_method = :letter_opener
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com',
:user_name => 'questionnaire.dev#gmail.com',
:password => 'qwerty_123',
:authentication => 'plain',
:enable_starttls_auto => true
}
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log
# Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
config.active_record.auto_explain_threshold_in_seconds = 0.5
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
config.cache_store = :mem_cache_store
end
When I comment out the line:
config.action_mailer.delivery_method = :smtp
and uncomment:
# config.action_mailer.delivery_method = :letter_opener
Everything works as planned (letter_opener is a gem that automates the registration process) so I know there's something going on in this file. As I said, it was working before, and I'm pretty sure those are the only lines I've changed. Is there something else I should be doing?
You need the following in your development.rb file as well as all the other stuff:
config.action_mailer.perform_deliveries = true
Without the above, your app won't actually send the email, and instead will simply display it's contents in your server log... Check the command line output (where rails server is running)
Note: this setting is enabled by default in production, so there's no need to specify it there
First of all, thank you all above for your help and guidance.
#Peter Klipfel - in fact, you were right. I was simply refreshing my app, therefore no error messages with:
config.action_mailer.raise_delivery_errors = false
I had to restart webrick for it to take effect. When that was done I got the error:
Net::SMTPAuthenticationError in Devise::RegistrationsController#create
username and password don't match
As I said, I inherited this project from someone else - questionnaire.dev#gmail probably doesn't exist any more. I changed that part of the code to:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.default :charset => "utf-8"
config.action_mailer.delivery_method = :smtp
# config.action_mailer.delivery_method = :letter_opener
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
# in previous Stackoverflow I read :domain part wasn't needed, so leave it out
# :domain => 'gmail.com',
:user_name => 'my_gmail_address#gmail.com',
:password => 'my_gmail_password',
:authentication => 'plain',
:enable_starttls_auto => true
}
And now everything works as it should (for the moment anyway!)

Carrierwave setup

please, could you please have a look to my config? I'm trying to setup carrierwave in my app but it doesn't work in the production server. It's working locally even as production environment. The thing is that it's saving the images into the tmp directory but the result is 'Empty file upload result'. I tried save to file and save to S3, same result. I'm using Passenger - Nginx.
I'm getting mad with this for two days now. Any idea or tip even about how I can debug this is welcome. I setup all permissions so I don't think it's a permissions issue. It could be a cache thing, the files are stored into tmp directory but it seems that the app don't see them as owned by the user???
Thanks!!!
#application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
Bundler.require(*Rails.groups(:assets => %w(development test)))
end
module MyApp
class Application < Rails::Application
config.encoding = "utf-8"
config.filter_parameters += [:password]
config.active_support.escape_html_entities_in_json = true
config.active_record.whitelist_attributes = true
config.assets.initialize_on_precompile = false
config.assets.enabled = true
config.assets.version = '1.0'
end
end
# avatar_uploader.rb
class AvatarUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
include ActiveModel::Conversion
extend ActiveModel::Naming
def extension_white_list
%w(jpg jpeg gif png mp3)
end
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :resize_to_fill => [200,200]
end
end
# carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'ACCESS', # required
:aws_secret_access_key => 'SECRET', # required
:region => 'eu-west-1' # optional, defaults to 'us-east-1'
}
config.fog_directory = 'my_app_bucket'
end
# production.rb
MyApp::Application.configure do
config.cache_classes = true
config.consider_all_requests_local = true # default false, debug true
config.action_controller.perform_caching = true #default true
config.serve_static_assets = true # Carrierwave true - Default false
config.assets.compress = true
config.assets.compile = false # false for real production
config.assets.digest = true
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
config.log_level = :debug
config.cache_store = :dalli_store
config.assets.precompile += %w( search.js )
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.action_mailer.default_url_options = { :host => 'localhost' }
end
Solved!
My ImageMagick install was missing delegates. Now:
convert -list configure
DELEGATES bzlib fontconfig freetype jpeg jng lqr pango png x11 xml zlib