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
Related
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.
I would like to my create action on my rails API to accept either a JSON POST or and XML POST. Do I need to do anything special or should it just work out of the box as long as every comes through as params?
Rails just sees them as params that are passed in. However you will want to have a respond to block that responds properly to xml vs json
respond_to do |format|
format.xml { #render XML STUFF }
format.json { #render JSON STUFF }
end
http://api.rubyonrails.org/classes/ActionController/MimeResponds.html
Be careful, Rails 4 removed support for XML. You may need to install the actionpack-xml_parser gem to support receiving XML as POST params. This requires you to add the following to config/application.rb
config.middleware.insert_after ActionDispatch::ParamsParser, ActionDispatch::XmlParamsParser
This was answered originally here.
Don't forget to restart your Rails server once you're done :)
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.
I am sending emails in a background job using ActionMailer. Users can create new email templates but they aren't recognized until the background job is restarted. Used to use
ActionView::TemplateFinder.reload!
which forced reloading of templates (now deprecated on 2.3.4). I have tried
ActionView::Base.cache_template_loading = false
but that does not work.
What I wound up doing was setting a global variable in my background process before the Rails environment was loaded:
$background = true
then in environments/production.rb:
config.action_view.cache_template_loading = !$background
Not thrilled, but it works. I get template reloading for email templates in my background job but cached view templates for the online application.
Since your users can create (and possibly change) templates, why don't you store them on the database and render as inline erb?
render :inline => template_record.contents
Now that I suggested this, I noticed... You can also use :inline to manually read the template and pass it to ActionView. You'll have to handle the exceptional case of the template not existing, though.
render :inline => File.read( ... )
I like how Rails gives me flexibility in naming view files, ie index.erb or index.html.erb essentially do the same thing.
The problem is that I've merged two separate projects, where one uses filename.erb for all its views and the other uses filename.html.erb.
It seems that Rails expects only one naming scheme as I keep getting missing template errors for the views with only the .erb extension.
Can I get around this? Should I even want to get around this? Or should I bite the bullet and manually rename half of my view files?
To me it sounds like there may be a problem with the naming conventions you're using.
See what happens when you choose an action that isn't working and then explicitly try and render a template with:
render :template => 'products/show'
Where 'products/show' is the path to your layout in the views directory.
If that doesn't work it might help locate the issue.
Another thing to try is to use the format declaration from within your action:
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #products }
end
The docs here are also very explicit about how the conventions by which docs are found.
http://guides.rubyonrails.org/layouts_and_rendering.html
Hope that helps,
David
You should stick with the more modern rails convention of *.html.erb.
Are you using different versions of Rails? Rails versions below 2.0 wouldn't support the .html.erb format.