ajax GET json data sending to controller problems
I'm calling
$.get('/profile', {'data_id': 0}, null, 'script');
in my profile/index.html.haml
when a specific event happens. This is suppose to load profile/index.js.erb
while passing the json data. In my profile controller's index
action, I have
@dataid = params[:data_id]
if @dataid == 0
#do something, ie set variables that index.js.erb needs
else
#do something else
end
However, the if
statement doesn't seem to work so because of that开发者_JAVA技巧 I believe my js.erb
file is not loading right (doesn't load at all).
However if I do if !dataid.nil?
instead of == 0 (or some other value)
, it works like before putting in the if statement/js.erb works so it must be getting that dataid is some value...
Also if I don't use the if
statement, and in my js.erb
I do dataid = <%= raw @dataid %>; alert(dataid);
, everything loads fine and the alert is correct--dataid is 0. So why is it it seems like the controller that sets the raw @dataid
in the first place doesn't know what @dataid
's value is? I'm very confused...
The main purpose was to not do everything in the controller unless needed (as specified by data_id). Also this way I thought I'd be able to do the same in my js.erb--only run the sections that I need based on this parameter.
Everything that comes via HTTP will arrive as a string. If you compare with "0", it will most likely compare equal. Try this in your controller code:
Rails.logger.debug "All params are:\n#{params.inspect}"
Rails.logger.debug "I got params[:data_id] = '#{@dataid}'"
Rails.logger.debug "The type is: #{@dataid.class}"
Rails.logger.debug "Comparison yields: #{@dataid == 0}"
Then watch the logs. You'll see what I'm talking about.
精彩评论