Rails views inconsistently rendered - ruby-on-rails

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.

Related

render doesn't know where to look for partials (from helper code)

When I try to render a partial from a helper, it fails with this (condensed) error message:
Missing partial /_cube_icon with [...]. Searched in:
Note that the list of searched directories is empty!
In contrast, when using render in a view, it knows where to look:
Searched in: * "/Users/Lars/GitHub/algdb/app/views"
In the helper code, I use ActionController::Base.helpers.render(). Should I use some other render function? How do I tell it where to look for partials? Could I have set up the project wrong somehow?
This is Rails 4.2.4 · Ruby 2.3.1
OK, I figured it out:
I was calling render from code in the helper directory, but not from a function in a standard SomethingHelper module.
When following that convention, things started working.
Partial files normally reside within the app/views directory and are not located in the helpers directory in Rails as you can see from the error message you are receiving. To solve your problem, I would move the _cube_icon file into the app/views directory and organize your code there. I recommend reading this section of the Rails documentation for views Layouts and Rendering
Additionally, it sounds like you may be new to rails so I would take a look at the conventions that rails offers. Rails, as you may or may not know, is an opinionated framework which means certain things need to go in certain locations in order for it to work. Here is another resource on just the view part of Rail's MVC framework Action View Overview. Hope this helps.
---Updated-----
If you really want to render a partial from a helper file, there are a few ways to do so but the best one to use is outlined below:
In app/helpers
module MyHelper
def custom_render
concat(render(:partial => 'cube_icon'))
end
end
Here are some links from stackoverflow to help you out.
Rendering a partial from helper #1
Rendering from a partial from herlper #2
Using concat rails

Rails renders wrong path

I'm not really sure what is going on here. I am writing an blog and in this blog I have a tag_controller. In this tag_controller I have this action
3 def show
4 #blog_posts = BlogPost.published.tagged_with(params[:tag]).paginate(:page => params[:page], :per_page => 5)
5 render 'blog_posts/index'
6 end
But it fails with a template missing exception. In my logfiles I can see it's looking for a template file in tag/blog_posts/index (inside views of course). But why is it doing that. Shouldn't it go directly for app/views/blog_posts/index.html.erb ?
In any case, how do I make my app find the correct template file?
Its because you have missing the leading slash in the path, thus rails is trying to find the view in the current controllers view directory i.e tags
do
render '/blog_posts/index'

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.

How can I render a specific template?

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?

Is it possible to render partials from a Ruby on Rails application to another?

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.

Resources