Reputation: 919
i have a very simple rails application that is ajax-based for some of it's functionality. When i'm using it from a development environment, everything runs as expected. However, something weird happens in production environment: when an ajax call is being made, the HTML (HAML based) layouts are being rendered instead of the js (Coffee based).
When comparing the logs from production and development, it's obvious that there is something i'm doing wrong in the way the responses are handled, but i've been scratching my head for hours now without a clue.
Let's say my request is: /software_tags/1/edit.js On my development logs, i can see (And the output is right, i get the expected javascript page):
Started GET "/software_tags/1/edit.js" for 127.0.0.1 at 2012-07-17 16:59:34 -0700
Processing by SoftwareTagsController#edit as JS
Parameters: {"id"=>"1"}
Rendered shared/_error_messages.haml (0.1ms)
Rendered software_tags/_form.haml (4.5ms)
Rendered software_tags/edit.js.coffee (5.7ms) <---- THIS IS GOOD!
Completed 200 OK in 10ms (Views: 8.1ms | ActiveRecord: 0.4ms)
On my development logs however, i see (And the output is wrong, i get the HTML page):
Started GET "/software_tags/1/edit.js" for x.x.x.x at 2012-07-18 01:59:54 +0200
Processing by SoftwareTagsController#edit as JS <--- Wouldn't that force the edit.js.coffee to be rendered instead of the edit.haml?
Parameters: {"id"=>"1"}
Rendered shared/_error_messages.haml (0.1ms)
Rendered software_tags/_form.haml (2.8ms)
Rendered software_tags/edit.haml (3.2ms) <---- THIS IS WRONG!
Completed 200 OK in 5ms (Views: 3.5ms | ActiveRecord: 0.5ms)
In my controller, the edit action looks like this:
def edit
respond_to do |format|
format.js
format.html
format.json { render json: @software_tag }
end
end
When i try to access json data however, everything is fine from all the environments, so what could be the problem with the wrong output for the js output?
EDIT: Maybe i'm missing something fairly simple. i'm still unable to achieve what is working in development from my production environement. i'm using ruby 1.9.3 with Rails 3.2.6, along with jQuery / jQuery-ujs. Basically, i would refine my question: when making link_to remote: true, how can i force the server to answer me with a javascript file (And branch on the respond_to.js block instead of the html one)?
Upvotes: 0
Views: 1531
Reputation: 2563
If is gem inside :assets group, this gems are only used for asset directories (in app, vendor, lib), not in views.
There are three methods to works fine:
# Bundler.require(:default, :assets, Rails.env)
Upvotes: 5
Reputation: 919
i have finally found an answer to my question. It sounds like a bug to me, or maybe i'm missing something.
i have put debugging messages on my controller like this:
def edit
respond_to do |format|
format.js { Rails.logger.info "in JS handler" }
format.html { Rails.logger.info "in HTML handler" }
format.json { render json: @software_tag }
end
end
In fact, the right actions were called, but the file edit.haml was rendered instead of edit.js.coffe, even when being in the format.js block.
The solution i have found is to move the gem 'coffee-rails' outside the :assets group from my gemfile, like this: Before:
group :assets do
gem 'uglifier'
gem 'sass-rails'
gem 'coffee-rails'
end
After:
gem 'coffee-rails'
group :assets do
gem 'uglifier'
gem 'sass-rails'
end
Now, everything is working as expected on the production environment.
Thanks for your help guys!
Upvotes: 1
Reputation: 222
Have you tried:
respond_to :js, :only => :edit
At the top of your controller?
Or specifying the template rendering in the format.js block the same way you're doing with the json block.
Upvotes: -1