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'
Related
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 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"
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
Hi this is ROR beginner's question.
I have creat controller.rb and view hello.rhtml following the tutorial, but when I try to open localhost:3000/say/hello, it come up with with error: No route matches [GET] "/say/hello"
could any one advice please?
Well you need to setup a route for that in your config/routes.rb file.
For first try i would say use a script generator, enter on the command line as being in the project library > rails g controller helloworld index. This will create a route for itself, and a controller file.
After this script runs, there should be a line in your config/routes.rb
Cloud::Application.routes.draw do
get "helloworld/index"
end
Then you need to enter localhost:3000/helloworld/index in your browser url bar. Then ( as default ) rails will render the view located in app/views/helloworld/index.*. If you want to change this behaviour, go to the helloworld controller.
For more info there is a useful guide: ROUTING GUIDES
You need to define a route definition so that the URL you are requesting gets mapped to an action in the controller you have created which would render hello.rhtml. Say your controller name is says_controller.rb (thats how Rails gives the filename). In that if you define and action hello which would by default render hello.rhtml, then the fallback routes which are defined in the routes.rb file at the end would make a request to say/hello to look for the say_controller and hello action, thus rendering hello.rhtml.
For detailed help you can refer to the Rails Guides. There is a lot of helpful material and it is explained very well.
I started developing RoR recently and the best practise I got was the tutorial # rails for zombies and video's # railscasts. I suggest you watch some / make some and you get a general idea how to get started :)
-edit- on this issue: You're trying to render the hello view from the say controller.
since routing is handled by default on :controller/:action, do you have a action called hello in say? No action means no route means no view rendered.
class SayController < ApplicationController
def hello
#do nothing or add some code
logger.debug "I'm in the say controller, hello action!"
end
end
This should get it to render the hello file. You might want to take a look at restful actions / crud though, rails uses those by default.
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.