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
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.
How could I manually render a rails error page from an exception ?
I mean the real rails error page, not a custom one:
I have an exception, and I need the html of this rails template from it.
EDIT :
As #Зелёный suggested, I can use templates from here, now the question is how can I render this ?
render 'rescues/diagnostics' #=> ActionView::MissingTemplate
You cant find all templates in path_to_your_gems_folder/gems/actionpack-x.x.x/lib/action_dispatch/middleware/.
Look into sources of actionpack
I suggest you to make a custom template and use styles and html from the rails sources.
So answer it is you cant just render a rails error page, because it is complex erb views with own variables and layouts.
I have the front end of a website built. It is made from a number of HTML, JavaScript, and Jquery files. Is there a way to take those files and move them into a Ruby on Rails environment so that I don't need to remake everything?
Sure. Create a new rails app (rails new app-name). Copy your existing javascript files into the Rails project at public/javascripts/. Your views/html are a bit more dependent on how you want to grow.
One option is to create a single PagesController and not bother with resources/models/etc, and you would just put all of your "views" into /app/views/pages/. You don't even have to rename them to .html.erb like you would conventionally see — you can leave them as just plain html. Your routes.rb file would look something like this:
get '/:action', :controller => 'pages', :as => 'page'
Which would afford you routes like "example.com/hello_world" that would route to PagesController#hello_world, rendering "app/views/pages/hello_world.html". If you want to use ERb, you can add that ".erb" suffix to your view file and then you can use the page_path helper to assemble links:
<%= link_to 'Hello World Demo', page_path('hello_world') %>
You will still probably want to extract the common elements into a layout at app/views/layouts/application.html.erb (again, the ERb being optional).
Another route would be to actually separate your application into resources. If you have a page that lists books, for example, you could create a BooksController and put the relevant view under app/views/books/index.html. This doesn't make sense to do if you're not going to provide some additional functionality in the future, though.
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.
I have two RoR3 applications (APP1 and APP2)
www.subdomain1.example.com
www.subdomain2.example.com
and I want to show on APP1 some views from APP2.
I tried to do that using a 'Net::HTTP' request (code in APP1)
Net::HTTP.get( URI.parse("http://www.subdomain2.example.com/users/new") )
but the response is not evaluated as HTTP code. Among other things I do not know if there are other techniques to do what I want in more easy way.
So, is it possible to render partials from APP1 to APP2 using the common and easy approach of rendering partials in the same RoR application?
Example:
render :partial => "/users/new"
If so, how can I do that?
Here, try this:
module ApplicationHelper
require 'open-uri'
def render_url(url)
open url do |f|
f.read.html_safe # remove the 'html_safe' if you're on Rails 2.x
end
end
end
In your view:
<%= render_url 'http://ilikestuffblog.com/' %>
It will work. Just one problem, though: if the site contains relative links to images, other pages, or anything else, those links will not be shown correctly. Try this to see a bunch of blank images:
<%= render_url 'http://www.ducklet.com/' %>
Also, BE WARNED that if you don't own the URL you're including, you will be subject to cross-site scripting weirdness.
If the two applications share a filesystem or have access to a shared filesystem, then you can reference a partial directly by file path. From the Rails guide on rendering:
2.2.4 Rendering an Arbitrary File
The render method can also use a view
that’s entirely outside of your
application (perhaps you’re sharing
views between two Rails applications):
render
"/u/apps/warehouse_app/current/app/views/products/show"
Rails determines that this is a file
render because of the leading slash
character. To be explicit, you can use
the :file option (which was required
on Rails 2.2 and earlier):
render :file =>
"/u/apps/warehouse_app/current/app/views/products/show"
The :file option takes an absolute
file-system path. Of course, you need
to have rights to the view that you’re
using to render the content.
It might be more prudent to create a gem that has any shared code (ie. partials) in it so both apps can use it.