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.
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.
Is there a way to open .html.erb files by themselves in the browser. The Rails app I am trying to have a look at is in rails 2.3 and will not run locally on the rails server nor will it run when I give the command script/server.
I would just like to open the individual views in the browser by themselves...is this possible?
You can open them in the browser, but the <% %> and <%= %> tags will get shown as-is instead of executing the Ruby code. These are template files that need to get processed by Rails to fill in data at runtime. Your best bet is TsaiKoga's answer, making a controller action render the template in question.
In rails4 you can do it like this:
erb a.html.erb > a.html
Maybe it doesn't work in rails 2.3. If you want to render that view, maybe you can use an action just like users#index and change the code to render it:
render "devise/mailer/a.html.erb", :layout => false
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.
Rails 2.3.9.
I have this action in my ProcurementsController...
def view_downloads
#downloads = Procurement.find(params[:id]).downloads
end
I want to render this view: views/procurements/view_downloads.html.haml.
When I just leave it like that what gets rendered is the Procurements index page. (I thought is was supposed to render the template with the same name as the action by default.)
I looked in the docs for the render method and found this...
Method deprecated
This method is deprecated on the latest stable version of Rails. The last existing version (v2.3.8) is shown here.
Zero info about what to use instead, or where to get further information.
Does that mean it's deprecated in Rails 3 but I can still use it if I'm using 2.x?
Or is there now some totally new way to indicate what template you want to render now? If so which versions of Rails does it apply to?
I want to create a Ruby on Rails layout and it should be in Liquid format.
Basically what I'm trying to do is to let the users to create their own layouts and save them in the database.
I tried to use
<%= Liquid::Template.parse(<code from database>).render %> in my layout.erb file but there I can't use the 'yield' command (since this is a layout I should have to have a way of rendering pages.)
But when I use 'layout.liquid' with {{ content_for_layout }} is will work find BUT, cannot load details from the database (I mean the HTML code..)
I hope I made myself clear :D )
Take a look at this Ruby on Rails plug-in:
http://github.com/akitaonrails/dynamic_liquid_templates
Next we have to find a way to intercept the default Ruby on Rails behaviour for your controller.
class MyAwesomeController
layout :get_my_db_layout
....
protected
def get_my_db_layout
'as_if_by_a_miracle.liquid' # add your db finder here
end
end
Then, overwrite LocalFileSystem#read_template_file with your own class / method, to get the template from the database. LocalFileSystem#read_template_file is a Liquid class.
I hope, that this idea is helpful.
please read Tobis article on
https://github.com/shopify/liquid/wiki/getting-liquid-to-work-in-rails
or look at this screencast
http://railscasts.com/episodes/118-liquid