AngularJS html5mode and rails routes - ruby-on-rails

I have followed all the github issues on HTML5Mode breaking Angular apps in 1.5. I am having the same problem but none of the fixes are working for me.
Im doing the following:
<base href="/">
and
get '/*path', :to => "menus#index"
and
app.config(function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
.when("/users/:u_id/restaurants/:r_id/menus/:m_id/edit", { templateUrl: "/assets/menus/edit.html", controller: "MenuSectionCtrl" })
.when("/users/:u_id/restaurants/:r_id/menus/:m_id/sections/:sec_id/items", { templateUrl: "/assets/menus/items.html", controller: "MenuItemCtrl" })
.when("/users/:u_id/restaurants/:r_id/menus/:m_id/sections/:sec_id/items/:i_id/option_sections", { templateUrl: "/assets/menus/option_sections.html", controller: "MenuOptionSectionCtrl" })
.when("/users/:u_id/restaurants/:r_id/menus/:m_id/sections/:sec_id/items/:i_id/option_sections/:op_sec_id/options", { templateUrl: "/assets/menus/options.html", controller: "MenuOptionCtrl" })
});
I am lost as to what to do next. I have tried every combination of routes and base[href].
Anybody have any ideas?
Also, I have followed this tutorial too. http://omarriott.com/aux/angularjs-html5-routing-rails/

Try this link
http://start.jcolemorrison.com/angularjs-rails-4-1-and-ui-router-tutorial/
If you have mount only single angularjs application then it is very straight forward. You need to make this entry in routes.rb
get '*path' => 'application#index'
And in your application.controller change the index method as below
def index
render layout: 'application'
end

The following link helped me to use rack-rewrite get which seems to work for me. Here is the link. Only the following code in config.ru would allow /api routes to rails:
# config.ru
# This file is used by Rack-based servers to start the application.
use Rack::Rewrite do
rewrite %r{^(?!.*(api|\.)).*$}, '/index.html'
end
require ::File.expand_path('../config/environment', __FILE__)
run Rails.application

I managed to get mine working with:
get '*path', to: redirect('/#/%{path}')
The template I bought was working without hash after the first load but if you refresh the page it does not work. I tried manually adding the hash like /#/page and the above routing does it automatically.

Related

No route matches [GET] "/users/assets/bootstrap.min.js" only in Firefox?

Firefox gives me this error when i try to sign in or sign up into my app, i am only working locally and i am using devise gem for this. After loading the form it cracks into this routing. The file bootstrap.min.js is in the stylesheets folder and into assets folder. In the layouts i have declared:
<script src="assets/bootstrap.min.js"></script>
Chrome and Safari does not have any problem. Another strange behavoir, i guess related to the same issue, is that when i try to inspect element it falls into this error:
No route matches [GET] "/assets/bootstrap.min.css.map"
The Routes file is done like this:
Rails.application.routes.draw do
resources :properties
devise_for :users
root "properties#index"
get "about" => "pages#about"
get "user_properties" => "pages#user_properties"
get "contact" => "pages#contact"
Anyone have a clue?
try this:
<script src="/assets/bootstrap.min.js"></script>

How to accept PUT and DELETE methods in my Rails RESTful web service

I've built a Ruby On Rails 3.0.x app that returns JSON. So far, I've got only four methods in my Advertisements controller: index, show, update, destroy.
When I try to call a method from an app within the same domain (through AJAX & jQuery), I succeed. But when I try to do the same from another app within another domain, I get the following error message, only when I try to use the methods PUT and DELETE (it works fine for GET and POST):
XMLHttpRequest cannot load URL_HERE Origin URL_HERE is not allowed by
Access-Control-Allow-Origin.
My RESTful service is called via HTTP, not HTTPS.
Below is the AJAX code that I'm using (16 is the advertisement's id):
$.ajax({
type: "DELETE",
url: "http://SERVICE_URL/advertisements/16.json",
crossDomain: true,
success: function(response){
alert("test");
}
});
Any ideas?
Thanks.
You might try rack-cors. We enable CORS for our web service with:
# config/application.rb
config.middleware.use Rack::Cors do |requests|
requests.allow do |allow|
allow.origins '*'
# perhaps you would stick :put and :delete here?
# then you should follow the rack-cors documentation to only enable it where necessary
allow.resource '*', :headers => :any, :methods => [:get, :post]
end
end

rails 3 mod rewrite with refraction gem

I'm trying to use a subdomain beta.somedomain.com and I would like it to redirect to somedomain.heroku.com/beta
I'm using the refraction gem here:
https://github.com/pivotal/refraction
but I can't seem to get it to work.
Tried:
refraction_rules.rb in intializers
Refraction.configure do |req|
if req.host == 'beta.somedomain.com'
req.rewrite! "http://beta.somedomain.com/beta/#{req.path}"
end
end
and
Refraction.configure do |req|
if req.host == 'beta.somedomain.com'
req.rewrite! "http://somedomain.heroku.com/beta/#{req.path}"
end
end
Also tried
req.permanent! :host => "beta.somedomain.com"
Instead of rewrite
production.rb
config.middleware.insert_before(::Rack::Lock, ::Refraction)
But neither works, both just direct me to root '/'
You can just use the built-in routing functionality of Rails 3:
constraints :subdomain => "beta" do
match "/(:page)" => redirect { |params| "http://somedomain.heroku.com/beta/#{params[:page]}" }
end
Check the Rails Guides or this Rails Dispatch article for more information.

Routing to static html page in /public

How can I route /foo to display /public/foo.html in Rails?
You can do this:
Add this, into your routes.rb file.
match '/foo', :to => redirect('/foo.html')
Update
In Rails 4, it should use "get", not "match":
get '/foo', :to => redirect('/foo.html')
thanks Grant Birchmeier
This can be done without triggering a redirect. Follow the steps further down to be able to route static files in config/routes.rb as shown in this example:
# This route will serve public/index.html at the /login URL
# path, and have a URL helper named `login_path`:
get "/login", to: static("index.html")
# This route will serve public/register.html at the /register
# URL path, and have URL helper named `new_user_registration_path`:
get "/register", to: static("register.html"), as: :new_user_registration
Install the rails-static-router gem: https://github.com/mufid/rails-static-router#installation
Restart app (first bin/spring stop to be sure app is completely reloaded).
Start using the static(path) method in your config/routes.rb.
E.g in Rails 4 add the following route:
get '/example', :to => redirect('example.html')
Also you need to enable static files from the 'public' directory in your configuration:
config.serve_static_files = true
OR
config.serve_static_assets = true
Also you might need to provide your public directory as root in NGINX configuration.

Rails 3 & Sinatra integration issue

I am trying to set up a sinatra app inside my Rails 3 (v3.0.1) application, but having no success. Sinatra gem (v1.1.0) is setup using bundle install.
Here's what i have.
customer_app.rb class in lib directory -
class CustomerApp < Sinatra::Base
get "/test" do
"Hello World"
end
end
my routes.rb file contains -
CustomerService::Application.routes.draw do
root :to => CustomerApp
end
The URL i am trying is - http://localhost:3000/test
I get this error (on the browser) - Routing Error. No route matches "/test"
and this error in the log - ActionController::RoutingError (No route matches "/test"):
Is there anything I am missing??
Also I just noticed, even a simple rack route is not working -
root :to => proc { |env| [200, {}, ["Welcome!"]]}
The root keyword by default maps only the path /.
So you are trying to say, forward any request for / to CustomerApp which can handle requests for /test.
You should change the match filter.
CustomerService::Application.routes.draw do
match "/test" :to => CustomerApp
end

Resources