How to change rails default "destroy" action in javascript? - ruby-on-rails

I would like to change the default functionality Rails uses for the destroy action by adding ajax and an animation to remove the item. I know how to implement the javascript, but I'm not sure what is the best way to "tap in" or override this bit of javascript, especially now that the jquery_ujs file is vendored away somewhere due to the asset pipeline. What is the best practice for doing this?

You can still add ajax and animation without bothering about the rails default behavior or the ujs behavior.
In the destroy action, you just have to respond to a javascript request and create the necessary js file that does the animation magic using jquery.
def destroy
# perform destructive action (optional)
respond_to do |format|
format.js
end
end
Make sure there's a file called destroy.js.erb in the views/<controller_name>/ folder.

Related

Rails is rendering a javascript .js.erb file in the view

I have a situation in Rails 5.2 where a controller is rendering a .js.erb file to the view literally, i.e. showing the javascript code in the view itself.
How can I get Rails to run the js.erb file instead of showing it as if it were a DOM file? It is taking the user away from a page I want the user to stay on.
# controller
def submit_custom_data
...perform some functions...
respond_to do |format|
format.js
end
end
# submit_custom_data.js.erb
console.log('submit_custom_data...');
... Other javascript ...
Additional Info
By preserving the browser console logs on Chrome, I see this:
Resource interpreted as Document but transferred with MIME type text/javascript: "http://localhost:3000/submit_custom_data/1068.js?c=1&d=10&db=1&s=8&tm=&w=3".
Navigated to http://localhost:3000/submit_custom_data/1068.js?c=1&d=10&db=1&s=8&tm=&w=3
This issue did NOT occur on Rails 4.2.7. After updating to Rails 5.2.7 (and updating the bundle), I'm now seeing these issues.

How can I use js.erb in redmine plugin-development

Due to the fact that the asset pipeline es deactivated, how can I use js.erb - files in plugin-development?
These files (i.e. files in app/views/**/*.js.erb) are used as the view for an actual web request. There, the assets pipeline would never kick in at all.
Instead, you can just return the response as you normally would for html. Just make sure to correctly set the format on your render call in your controller, e.g.
respond_to do |format|
format.js {}
end
or (in case the requested format is not set as js), you can use an explicit render call:
render format: :js

Rails dynamic select menu for 3.1

How would this be updated for Rails 3.1?
http://railscasts.com/episodes/88-dynamic-select-menus
I just can't figure out how to call the js.erb file and have it run the code to generate the javascript dynamically.
Might be something: in Rails 3.1, you're most likely using jQuery instead of Prototype. The example code on the Railscasts site is using good old Prototype instead of the new hotness that is jQuery (default javascript library in Rails 3.1).
Once all your jquery pipes are connected, having rails respond to and render your js.erb is the same as always. In your controller:
def country_selected
// whatever you need to do
respond_to do |format|
format.js
end
end
Then in your view directory, you have a country_selected.js.erb that you can put in whatever javascript you want to update the second select menu. (Remember you have to escape your shiz for it to work correctly) e.g.
<%= escape_javascript(params[:country]) %>
By the way, I think .rjs was moved out of Rails proper and into it's own Gem. Something else to keep in mind regarding Rails 3.1 vs. javascript.

Do I need to require original file when overriding controller from Rails Engine?

I'm trying to override an action in a controller defined by a Rails Engine.
It seems like I need to require the original file before reopening the class, like so:
require File.join(RAILS_ROOT, 'vendor/plugins/myplugin/app/controllers/some_controller')
class SomeController
def index
render :text => 'this is my index'
end
end
This makes sense, but that require is pretty ugly. Is there some sort of Rails magic that would allow me to avoid the initial require?
This is a complete guess...
Seems more of a load timing problem. As in, your file is getting loaded before the plug-in. Where is your action located? config/initializers? lib?
I'm not to sure when Rails Engines gets loaded so play around with the location (should work by putting it in lib).
Or, better yet, create your own plug-in with the changes and make sure it loads after the original.
And you probably want something more like:
SomeController.class_eval do
def index
...
end
end

Why am I suddenly getting Missing Template errors with edge Rails (2.3)?

After freezing edge rails, all my controller examples are failing with
MissingTemplate errors.
e.g., "Missing template attachments/create.erb in view path app/views"
Trying to actually render the views gives me the same error.
I noticed I can fix most of them by using respond_to but I usually
never use it. I almost always only need to respond to one format in
one action so I omit respond_to and let Rails figure out which file to
render.
Does Rails suddenly require respond_to blocks in every action as of 2.3?
Just found this, which answers my question:
http://rails.lighthouseapp.com/projects/8994/tickets/1590-xhrs-require-explicit-respond_to

Resources