3

I have a Rails app that I am feeding cross domain in production. It needs absolute references. Because of this, I have enabled the following in my config/environments/production.rb:

config.action_controller.asset_host = "http://myapp.herokuapp.com"

That works fine for images and resources but my input form that looks like this:

 <%= form_tag('/plans/collapse_plans', :method => 'post', :remote => true ) do %>

is still getting this in the console:

  Failed to load resource file://localhost/plans/collapse_plan

How can I change it so that form action will automatically include the specified host, instead of defaulting to localhost? Can I set this anywhere in config?

Raj
  • 22,346
  • 14
  • 99
  • 142
jamesdlivesinatree
  • 1,016
  • 3
  • 11
  • 36

4 Answers4

3

This seems like it will work:

https://github.com/binarylogic/settingslogic

Then I can just do:

<%= form_tag mysettings.myspecifiedhost + plans_collapse_plans_path, :method => 'post', :remote => true do %>
jamesdlivesinatree
  • 1,016
  • 3
  • 11
  • 36
  • Using concatenation of strings to render URLs probably means there's a better way to do this :) – bbozo Jan 21 '14 at 06:40
2

I may be on the wrong track here, but:

Asset host is not your application's host, asset host is a host that serves you /app/assets folder and this is configurable so you can set up a CDN for example, it's not intended for hosting action points.

If you want to target the full url of your own host use rake routes to get the route name corresponding to /plans/collapse_plans which probably looks something in the lines of plans_collapse_plans and then you can use plans_collapse_plans_url and rails will render the correct full URL for you.

If you're using the default host name rails provides automagically this will "just work", i.e.

[2] pry(#<#<Class:0x000000048fd780>>)> account_edit_url
=> "http://dev:3000/account/edit"

If this doesn't "just work", you can override all url helpers in the app by overriding default_url_options in your ApplicationController:

def default_url_options
  {:host => HOST}
end

and be sure to set the HOST constant in your application's environment, for example:

[1] pry(#<#<Class:0x00000005047d10>>)> account_edit_url
=> "http://o7ms:3000/account/edit"

If you need to override this just in certain situations you can leave the ApplicationController alone and do:

[3] pry(#<#<Class:0x000000048fd780>>)> account_edit_url(host: MY_HOST_FOR_THE_OTHER_THINGY)
=> "http://foo:3000/account/edit"

In all cases you'll set up a config option in one place and all endpoints in the app will adjust.

EDIT

If you want to go fancy,

see default_url_options and rails 3,

by overriding url_options you may be able to implement pretty calls like account_edit_url(ajax_host: true), the url_options method would look something like this if this works:

def url_options
  options = super
  if super.delete(:ajax_host)
    {host: AJAX_HOST}.merge(options)
  else
    options
  end
end
Community
  • 1
  • 1
bbozo
  • 7,075
  • 3
  • 30
  • 56
  • This is REALLY good information. You should have gotten this bounty. Seeing how "rails magic" works is really important to me. Thanks for the help. – jamesdlivesinatree Feb 08 '14 at 20:34
0

what you are trying cannot be done normally for ajax calls.
see http://en.wikipedia.org/wiki/Same-origin_policy

rejin
  • 179
  • 1
  • 13
  • I only need to know how to set the action's host, I am aware of Same Origin Policy and have already gotten around this issue, what I am doing can be done with a rails plugin (i forget the name off the top of my head) – jamesdlivesinatree Jan 18 '14 at 16:21
  • gem 'rack-cors', :require => 'rack/cors' this allows your rails app to respond to cross domain requests. – jamesdlivesinatree Jan 18 '14 at 16:23
  • what you did is for assets. give the entire url in the form tag. – rejin Jan 18 '14 at 16:34
  • I know I can do it with the full URL, I'm trying to avoid that. If I specify the path in active record, aka 'plans_collapse_plans_path' should that work? How can I give the method a host? I am looking for something I can add to the config so that it appluies universally. – jamesdlivesinatree Jan 18 '14 at 19:14
0

Two approaches:--

1.) <%= form_tag root_url + plans_collapse_plans_path, :method => 'post', :remote => true do %>

concatenation:-- root_url + plans_collapse_plans_path 

2.) in config/environments/production.rb

MyApp::Application.configure do
  # general configurations
  config.after_initialize do
    Rails.application.routes.default_url_options[:host] = 'root_url' #'localhost:3000'
  end
end
Alok Anand
  • 3,346
  • 1
  • 20
  • 17