In my routes.rb I have a custom path defined:
match "foo/copy" => "foo#copy", :via => [ :post ], :as => "copy_foo"
I have an initializer in my config/initializers directory named https_by_default.rb, which contains one line:
Rails.application.routes.default_url_options[:protocol] = 'https'
If I run rails console and type Rails.application.routes.default_url_options, it returns {:protocol => 'https} as expected.
But in my view where I am using the generated custom path _url helper, it doesn't generate the URL with https. <%= copy_foo_url() %> in the ERB returns http://localhost:3000/foo/copy.
What am I missing in order to get my named routes helpers to respect default_url_options?
You should use the following instead:
<%= copy_foo_path() %>
And in your application config file add.
config.force_ssl = true
This enables ssl on all your Rails environments. If you want to enable ssl on your production environment only, then just add the line to your production config file.
Related
Given the following config/routes.rb:
constraints subdomain: 'subdomain' do
get 'path', to: 'main#index', as: :sample
end
The following works:
Rails.application.routes.url_helpers.sample_url(host: "a.b")
# "http://subdomain.a.b/path"
The following fails:
Rails.application.routes.url_helpers.sample_url(host: "a.b.c")
# "http://subdomain.b.c/path"
Any way to fix it so subdomains don't replace existing subdomains in the host but instead are linked (i.e. http://subdomain.a.b.c/path)? I realize I can change the subdomain in the route to be subdomain.a - but that'd be a bit of a pain to maintain for multiple subdomains / deployments.
Looks like you need to examine tld_length:
:tld_length - Number of labels the TLD id composed of, only used if
:subdomain or :domain are supplied. Defaults to
ActionDispatch::Http::URL.tld_length, which in turn defaults to 1.
I'll test the specifics, but the issue looks like Rails is only allowing a tld (top level domain) length of 1, which means that you can only have a subdomain and 1 "other" element (subdomain.b.c / subdomain.a.b).
The fix should be to expand the tld_length, which according to this answer, can be done in the config settings /config/application.rb:
# config/application.rb
config.action_dispatch.tld_length = 2
Tests
Without tld_length:
c:\Dev\Apps\torches>rails c
Loading development environment (Rails 5.0.0.1)
irb(main):001:0> Rails.application.routes.url_helpers.sample_url(host: "a.b")
=> "http://subdomain.a.b/path"
irb(main):002:0> Rails.application.routes.url_helpers.sample_url(host: "a.b.c")
=> "http://subdomain.b.c/path"
irb(main):003:0>
With tld_length:
c:\Dev\Apps\torches>rails c
Loading development environment (Rails 5.0.0.1)
irb(main):001:0> Rails.application.routes.url_helpers.sample_url(host: "a.b")
=> "http://subdomain.a.b/path"
irb(main):002:0> Rails.application.routes.url_helpers.sample_url(host: "a.b.c")
=> "http://subdomain.a.b.c/path"
irb(main):003:0>
create config/initializers/host_name.rb file, where you can place a constant :
HOST_NAME = 'b.c'
then anywhere in your code :
Rails.application.routes.url_helpers.sample_url(host: HOST_NAME, subdomain: 'subdomain.a')
you can also create constants for your subdomains if you like
Devise confirmation_url is producing just a relative url with no domain such as:
http://users/confirmation?confirmation_token=...
I tried changing
confirmation_url(#resources, :confirmation_token => #resource.confirmation_token)
to
confirmation_url(:confirmation_token => #resource.confirmation_token)
but it produces the same url. I upgraded to devise 2.2.3 but same outcome. Rails 3.1.4
Update:
I have set in my production.rb:
config.action_mailer.default_url_options = { :host => 'mysubdomain.mysite.com' }
and I tried setting
before_filter set_actionmailer_host
def set_actionmailer_host
ActionMailer::Base.default_url_options[:host] = request.host_with_port
end
to no avail in application controller (https://github.com/plataformatec/devise/issues/1567)
Update:
This occurs in both development and production.
Update
I can't understand why Devise isn't using the template in app/views/devise/mailer/confirmation_instructions.html.haml If I could edit that I could append the host manually: 'http://mysite.com/' + confirmation_url(...
I tried setting the scoped views setting but it didn't have any effect
This is a disaster, users can't confirm their registration :(
You might have missed to add the default url hosts to your environment config-files. When you run the rails g devise:install command you usually receive those instructions.
# config/environments/development.rb
# Default actiomailer url host (required by devise)
config.action_mailer.default_url_options = { host: 'myapp.dev' }
You need this setting for all your environments.
# config/environments/production.rb
# Default actiomailer url host (required by devise)
config.action_mailer.default_url_options = { host: 'myproductionapp.com' }
I am trying to access a url helper that I'm using in my event controller like this event_url(event).
Is there a way to access this in my event model?
Something like this?
event_url(self)
Im using rails v2.3.11.
Try adding this in your model
include ActionController::UrlWriter
You also need to add config lines to env configs:
development.rb
config.action_controller.default_url_options = {:host => "localhost:3000"}
production.rb
config.action_controller.default_url_options = {:host => "example.com"}
I have a Rails 3 app where I am trying to populate a javascript variable with every Nation in my database (less than 300 nations) as a JSON object. This is the relevant line in my nations.js.erb file:
_this.nations = <%= Nation.all.to_json :only => [:id], :methods => :text %>;
When I call my js file in a browser, /assets/users.js which does a require of the nations file, the _this.nations variable is populated perfectly. When I try to do a precompile I get the following:
$> rake assets:precompile
$> rake aborted!
uninitialized constant Nation (in nations.js.erb)
So my question is this: is it possible to reference the Nation model, or any model, from within the js.erb file for precompiling? I also tried using my NationsHelper but my error just changed to uninitialized constant NationsHelper.
I'm fairly new to RoR so if relevant information is needed that I haven't provided, please just ask.
If you have config.assets.initialize_on_precompile set to false somewhere then try enabling it
config.assets.initialize_on_precompile = true
Why don't you make the call in the controller
controller
#nations = Nation.all
nations.js.erb
_this.nations = <%= #nations.all.to_json :only => [:id], :methods => :text %>;
I'm new to Ruby on Rails (formerly and currently PHP expert) so forgive my ignorance but I'm trying to get Sinatra working as middleware to redirect some old urls since I tried the gem rack-rewrite and couldn't get that to work either.
I am using code samples from ASCIIcast so in my routes.rb I have the following:
root :to => HomeApp
(^ I'm redirecting the root only for testing)
In my lib folder I have home_app.rb
class HomeApp < Sinatra::Base
get "/" do
"Hello from Sinatra"
end
end
When I start the server (or if its already running) it produces the error:
routes.rb:10: uninitialized constant HomeApp
Which seems that it just isn't aware of the lib/home_app.rb file.
I have included Sinatra in my Gemfile and ran bundle install and confirms it is included.
I just want to reroute old urls from my old site to my new ruby app but can't get any of this middleware/rack stuff working. All documentation assumes you aren't a total newb or is for RoR pre-3.0.
You don't need to use Sinatra if you want to redirect some URLs. You can use the new redirect method. See the Rails Dispatch article.
match "/stories/:year/:month/:day/:name" => redirect("/%{name}")
constraints :user_agent => /iPhone/, :subdomain => /^(?!i\.)/ do
match "*path" => redirect {|params, req| "http://i.myapp.com/#{req.fullpath}" }
end
In your specific case, the problem is that the HomeApp class is not loaded. Either add the /lib folder to your load path changing application.rb
config.autoload_paths += %W( #{config.root}/lib )
or require the file.