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.
On Tue, Feb 22, 2011 at 2:53 PM, Niklas N. [email protected]
wrote:
var states = new Array();
<% for state in @states -%>
states.push(new Array(<%= state.country_id %>, '<%=h state.name%>', <
%= state.id %>));
<% end -%>
…
could you try
var states = <%= @states.collect {|state| [state.country_id,
h(state.name)]}.to_json
%>
You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
–
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 %>
On Tue, Feb 22, 2011 at 12:05 AM, Niklas N. [email protected]
wrote:
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 %>
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?
–
Hassan S. ------------------------ [email protected]
twitter: @hassan
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
function countrySelected() {
country_id = $(‘account_country’).getValue();
var states = <%= @states.collect {|state| [state.country_id,
h(state.name)]}.to_json %>
<% for state in @states -%>
states.push(new Array(<%= state.country_id %>, ‘<%=h state.name %>’, <
%= state.id %>));
<% end -%>
options = $(‘account_state’).options;
options.length = 1;
states.each(function(state) {
if (state[0] == country_id) {
options[options.length] = new Option(state[1], state[2]);
}
});
}
In my routes file i have added
match “:controller(/:action(/:id(.:format)))”
Do i need to do some other configs ?
On Tue, Feb 22, 2011 at 4:05 PM, Niklas N. [email protected]
wrote:
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?
could you try
answer ?
“Ruby on Rails: Talk” group.
–
You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
–
On Tue, Feb 22, 2011 at 10:51 AM, Niklas N. [email protected]
wrote:
Yes i also belive that it dosent fire up. If i go to :
http://localhost:3000/javascripts/location_handler.js
Oops. That’s the problem – files under public are static, that is,
not
interpreted. You need to put your file somewhere within app/views.
–
Hassan S. ------------------------ [email protected]
twitter: @hassan
[Hope this wont be a dubbel post]
My feeling is also that it dosent fire the ruby part of the script.
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
source code
…
function countrySelected() {
country_id = $(‘account_country’).getValue();
var states = <%= @states.collect {|state| [state.country_id,
h(state.name)]}.to_json %>
<% for state in @states -%>
states.push(new Array(<%= state.country_id %>, ‘<%=h state.name %>’, <
%= state.id %>));
<% end -%>
options = $(‘account_state’).options;
options.length = 1;
states.each(function(state) {
if (state[0] == country_id) {
options[options.length] = new Option(state[1], state[2]);
}
});
}
function stateSelected() {
options = $(‘account_city’).options;
options.length = 1;
}
…
I have added match “:controller(/:action(/:id(.:format)))” to my
routes.rb is there anything else i need to do ?
//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.
[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
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
On Tue, Feb 22, 2011 at 8:33 PM, Niklas N. [email protected]
wrote:
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
end
So add that route and you’re done.
–
Hassan S. ------------------------ [email protected]
twitter: @hassan
Hassan, you are my hero… Thank you for taking time … Got it
working
/Niklas
On Wed, Feb 23, 2011 at 5:17 AM, Niklas N. [email protected]
wrote:
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 S. ------------------------ [email protected]
twitter: @hassan