Can't get .js.erb to work ...

Trying to follow railscast #88, i know the js.erb file is loaded but when i try to insert ruby code in the .js.erb file i get an error "illegal XML character" i have added the following line to my routes file match ":controller(/:action(/:id(.:format)))"

and code not working is:

...

  var states = new Array();   <% for state in @states -%>   states.push(new Array(<%= state.country_id %>, '<%=h state.name %>', < %= state.id %>));   <% end -%> ...

I have spent hours on trying to solve, could someone point me to the answer ?

if i remove ruby code and do states.push(new Array(1, 'test', 1)); it works like i want, but i need Array to contain vales from my JavascriptsController

//Niklas.

Trying to follow railscast #88, i know the js.erb file is loaded but

when i try to insert ruby code in the .js.erb file i get an error

“illegal XML character” i have added the following line to my routes

file

match “:controller(/:action(/:id(.:format)))”

and code not working is:

    var states = new Array();

    <% for state in @states -%>

    states.push(new Array(<%= state.country_id %>, '<%=h [state.name](http://state.name) %>', <

%= state.id %>));

    <% end -%>

could you try

var states = <%= @states.collect {|state| [state.country_id, h(state.name)]}.to_json %>

That gives me the error (in firebug)

illegal XML character [Stop on error] var states = <%= @states.collect {|st...ountry_id, h(state.name)]}.to_json %>

That gives me the error (in firebug)

illegal XML character

[Stop on error] var states = <%= @states.collect {|st…ountry_id, h(state.name)]}.to_json %>

what’s the generated js code by that template? is it a valid js array?

If that's what the client is seeing, it would seem to indicate that your *.js.erb file is not actually being evaluated.

Is there anything (errors) in your logs to indicate why that might be?

Yes i also belive that it dosent fire up. If i go to : http://localhost:3000/javascripts/location_handler.js i get a - No route matches "/javascripts/location_handler.js" - error if i go to http://localhost:3000/javascripts/location_handler.js.erb i see the complete source

Oops. That's the problem -- files under public are static, that is, *not* interpreted. You need to put your file somewhere within app/views.

:slight_smile:

I moved the file to /views/javascripts/getlocation.js.erb But there must be something wrong with routes or something else still.

If i go to: http://localhost:3000/javascripts/getlocation.js i get a No route matches "/javascripts/getlocation.js" error.

[Routes] ModcubeApp::Application.routes.draw do

get "log_in" => "sessions#new", :as => "log_in" get "sign_up" => "accounts#new", :as => "sign_up" root :to => "pages#home"

resources :accounts

match ":controller(/:action(/:id(.:format)))"

end

[javascripts_controller.rb] class JavascriptsController < ApplicationController

  def getlocation     @states = State.find(:all)     @citys = City.find(:all)   end

end

//Niklas

I moved the file to /views/javascripts/getlocation.js.erb But there must be something wrong with routes or something else still.

If i go to: http://localhost:3000/javascripts/getlocation.js i get a No route matches "/javascripts/getlocation.js" error.

Yes, that's exactly the problem :slight_smile:

[Routes] ModcubeApp::Application.routes.draw do

get "log_in" => "sessions#new", :as => "log_in" get "sign_up" => "accounts#new", :as => "sign_up" root :to => "pages#home"

resources :accounts

match ":controller(/:action(/:id(.:format)))"

end

So add that route and you're done.

Does not match ":controller(/:action(/:id(.:format))) do the routing ? I thought it should look in controller and if not found look in public/ javascript ?

//Niklas

Does not match ":controller(/:action(/:id(.:format))) do the routing ?

Only if that pattern matches your request - which it doesn't.

> No route matches "/javascripts/getlocation.js" error.

I see a controller, an action, and a format in that request - no id ...

Hassan, you are my hero.... Thank you for taking time ... Got it working :slight_smile:

/Niklas