I am using rails 4 from Suspenders and need to include content from a middleman-blog rack app from a subfolder in a view. In my app, I have the blog content in:
rails root > my_blog > source > index.html.erb.
I have created the view as:
rails root > app > views > welcomes > index.html.haml
The code in my view is:
%h1 Welcome
= render "my_blog/source/index.html.erb"
But when I access the page I get a Missing Partials error and the message says it only looked in the views folder.
How can I render content from a folder outside views?
Partial
The problem is Rails is trying to call a partial, which is not what you're trying to call. A partial should have its name preceded with an _underscore, indicating to Rails that it's a partial, hence why you're receiving your error
The reason this is important is because although you're just calling render, you will actually call the partial too
--
Convention
One of the issues you have is that you'll be going against convention in several ways:
MVC dictates the "view" will be loaded per request (so Rails will expect it to just be present whenever you use it)
The "partials" functionality of your system needs to add to the views you're showing the users. This means you have to be sure you
have a view already showing on screen
This means you need to be certain if you're meant to be using a partial or other element in this part of your app. From the looks of it, whilst you may be doing something right, you need to be sure you're able to load the partial correctly:
<%= render "your/partial/path/_partial_name.html.erb %>
--
View Path
Further to your view path issue, although I've never encountered this issue directly, there is a function called append_view_path, which allows you to add another "path" to look at for your app:
#app/controllers/welcome_controller.rb
Class WelcomeController < ApplicationController
append_view_path(File.join(RAILS_ROOT, "app/themes/#{#current_theme}"))
end
Try this is in your view file:
%h1 Welcome
= render :partial => "my_blog/source/index"
Note: You must have _index.html.erb partial file in the above specified path.
Also, try to change the name from _index.html.erb to _someothername.html.erb, because index.html.erb is usualy a view file for index action. You can avoid the unwanted confusions.
This may help you..!
Thanks!!
Added Lines (Edited):
Please change your file name from
rails root > my_blog > source > index.html.erb.
to
rails root > my_blog > source > _index.html.erb.
Have you tried the full path?
= render "#{Rails.root}/my_blog/source/index.html.haml"
Related
Suppose I run
rails new proj1
I can do
rails generate controller abc def
and that creates among other things, .\app\views\abc\def.html.erb
And I can edit config.rb to say
get '/', to: 'abc#def'
or
root 'abc#def'
And that will load that def.html.erb template when I go to http://127.0.0.1:3000/
But I'm interested if I can do that without creating that new controller. I'm interested in whether I can go to the template just using the application controller.
So, for example, I can edit .\app\controllers\application_controller.rb and add
def a
end
then does the application controller behave like other controllers and try to render a corresponding .html.erb file e.g. the 'a' action would try to render a.html.erb? If so, I can't find where I should place the a.html.erb file?
Other controllers have a subdirectory for them within views, and files within that subdirectory for each action, and one can do them manually or by using e.g. rails generate controller blah a b c to generate the controller with actions and with a template for each action.
But I can't see that for the application controller.. I can't see where its views are.
You can do it like this:
routes.rb:
get '/', to: "application#root"
application_controller.rb
def root
end
app/views/application/root.html.erb
hello world
Running the server and visiting localhost:3000 prints "hello world"
Sinatra does things similar to this. Routes and controller actions are basically combined and it'd be up to you to implement rails-like controllers yourself if you wanted them.
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
I am using a rails mountable engine called 'Child' inside the application called 'Parent'.
I have a partial 'child/user/_index.html.erb'
Now I want to render this child's partial in parent controller. Is it possible?
Means I want to do something like
render :partial => 'child/user/index.html.erb'
Thanks
Ref this
render :file => 'child/user/index.html.erb'
###or try '/child/user/index.html.erb'
I think you can just render user/index
ref http://edgeguides.rubyonrails.org/engines.html
The engine is unable to find the partial required for rendering the
comments. Rails looks first in the application's (test/dummy)
app/views directory and then in the engine's app/views directory. When
it can't find it, it will throw this error. The engine knows to look
for blorgh/comments/comment because the model object it is receiving
is from the Blorgh::Comment class.
This what worked for me:
render file: 'child/user/_index.html.erb'
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'
Let's say I created a mountable engine called 'Soho' that has a controller for 'Users'. I can go to /users/1 to see my user with ID 1.
Inside 'Soho', I have a application.html.erb for layout.
Now let's assume I want to "blend" my engine 'Soho' in an application called 'Soho_test', and I mount my engine at "/". So, in my host application 'Soho_test', I can also go at /users/1 to see my user with ID 1. This is working.
My question is : how can I do in my host application 'Soho_test' to apply the 'Soho_test' application.html.erb to the /users/1 (user profile page) instead of the one in the 'Soho' mountable engine?
Thanks!
I found how to achieve it, so I will post my answer on my own question, in case anyone else wonders. It is actually quite easy. I should have thought about this in the first place...
All you have to do is to create a folder in your /views/layouts/ with the name of your engine. So according to my question, it would be /views/layouts/soho/. In that folder, put the application.html.erb that you want to have.
You can do the same with partials and other views. Just create a folder /views/soho/.../ and put your files there. I havn't found a rake task to copy the engine views in my host application, so I wrote one.
After reading your question over a few times, I think all you are trying to do is override a layout for a given controller.
If that is the case just specify the layout to use within your controller see the section 2.2.13.1 Specifying Layouts on a per-Controller Basis within the Rails Guide for Layouts
Here's an example:
class UsersController < ApplicationController
layout "users"
#...
end