Brandon
Brandon

Reputation: 1865

Javascript not executing (I included format.js)

I added format.js to my controller, yet I still cannot get the js in index.js.erb to execute when I view my index page. The only thing I can figure out is that it must be because of the model name. I had to add

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'business', 'businesses'
end

to my inflections.rb file...because my model is called Business.

my views/js are located at views/businesses/*

Please help before I pull my hair out!

All I have in the index.js.erb file is:

alert(1);

Obviously the goal is that I will get an alert when I finally get the issue fixed, letting me know it is working.

Upvotes: 0

Views: 210

Answers (2)

Super Engineer
Super Engineer

Reputation: 1976

How are you calling your index page? If you visit the index page from your your browser it won't render index.js. That's the whole point behind using format. You will have to call your index method using javascript(something like an ajax call). Your model name does not have anything to do with it.

Upvotes: 3

David Monagle
David Monagle

Reputation: 1801

Javascript execution has nothing to do with your model names. Nor is the inclusion of your JS files done based on the name of the controller or model. If you are using the asset pipeline you need the following.

Assuming that index.js is in /assets/javascripts/index.js, add the following to the /app/assets/javascripts/application.js

//= require ./index

If you don't understand how this works, you may wish to go through the Ruby on Rails Guide for the Asset Pipeline, found here: http://guides.rubyonrails.org/asset_pipeline.html

As a side note, with your inflections usage, I would recommend that if you are going to use a framework like Rails, try and use conventions that come with it. It will save you heartache in future. Name your model business so that your table is called businesses. Unless you really want something like this:

has_one :businesses

Upvotes: 0

Related Questions