I am newbie with rails and here is my problem
I tried to write a very simple program with rails, and when I wrote an application.html.erb, it worked well.
Here is what I got
But, when I created a folder named "shared" and a file named _navbar.html.erb in that folder, I thought that I could render my code from that file navbar.html.erb to the file application.html.erb by this code
<body>
<%= render "shared/navbar" %>
<%= yield %>
</body>
The folder shared/_navbar.html.erb is in the folder views, which means it in the same folder with the folder layouts/application.html.erb
I am setting rails -v 7.0.4 and ruby -v 3.1.2
Here is all of my code, if you need
https://github.com/nguyencuc2586/freelancer
Could you please give me some advices ? Thank you very much for your time.
Quote from the Rails Guides about Layouts and Rendering:
This makes app/views/application/ a great place for your shared partials, [...]
Following that advice from the official Rails guides, I would place the shared partial with the navigation into app/views/appplication/_navbar.html.erb and would call it from the layout view like this:
<%= render "navbar" %>
Alternatively, place the shared partial at app/views/shared/_navbar.html.erb and call it from the layout like this:
<%= render "shared/navbar" %>
Or, place the shared partial at app/views/layouts/shared/_navbar.html.erb and call it from the layout like this:
<%= render "layouts/shared/navbar" %>
Related
I am trying to render contents of user_details\index.html.erb in my custom page home.html
here is my directory structure
apps
|
views
├───co_owners
├───layouts
├───pages
├───shared
└───user_details
the home.html.erb page is pages directory... and index.html.erb pages is in user_details directory
i am using
<%= render :partial => 'user_details' %> to render it on my home page... but i am getting error ActionView::MissingTemplate in Pages#home
how can i do this?
As an answer in a previous your question
It is not a partial (starts with underscore), so you need render
template. You also need the full path of the template, from the view
directory:
<%= render template: 'user_details/index' %>
I suggest you read Layouts and Rendering in Rails
I've created a gem that has a file in the app/views/merchants_support/navigation.html.haml location and I want to use this as a partial in my rails app.
I found a stack overflow that suggested I could do something like this: = render partial: "merchants_support/navigation"
This obviously didn't work. Any thoughts on how I could do this?
Rename navigation.html.haml to _navigation.html.haml because all partions should start with _ symbol.
Here is detail information about this convention.
For rendering arbitrary files, you can write:
render file: "/path/to/rails_apps/your_app/path/to/your/file"
The :file option takes an absolute file-system path.
So, you can do:
<%= render file: Rails.root.join("app/path/to/your/file") %>
Or, maybe this is better:
<%= render file: Rails.root.join(*"app/path/to/your/file".split('/')) %>
If the file you want to render is a view in another controller(which is what your path looks like), then you can simply do:
<%= render template: "merchants_support/navigation" %>
I made a partial for a site and my client loved it. It was basically just a sidebar that helps browse the database. Now he wants the sidebar all throughout the site on various pages. So, how can I call a partial from any controller without having to copy and paste the file into each directory.
Thanks in advance!
You could try placing your partial in a folder call shared inside app/views and doing something like:
render :partial => 'shared/sidebar'
where you want to render the partial.
Yes you can:
<%= render "partial_folder/partial_name" %>
Example:
<%= render "layouts/sidebar" %>
I'm getting the following error when Rails tries to render my layout located in app/views/layouts/application.html.erb:
Missing template shared/_header.erb in view path /home/me/checkout/site/app/views:
Extracted source (around line #11):
8: <body>
9: <div id="wrap">
10: <div class="clear">...</div>
11: <%= render :partial => 'shared/header' %>
What's weird about all of this is that I have a directory named shared inside of app/views. Inside there I have a file named _header.html.erb.
I'm using Ruby 1.8.7, Rails
2.2.2, and following the documentation found here.
Update: Even more weirdness. This code works under Windows using InstantRails (Ruby 1.8.6, Rails 2.2.2). However, render :template doesn't seem to work. I'll keep on investigating.
If what you typed is correct, your partial is misnamed. It should be "_header.html.erb" and not "_header.erb.html". This is probably why Rails is not finding it.
It has been quite a while since I have touched Rails 2.2, but I believe that you are looking for the following:
<%= render :template => 'shared/header' %>
Edit: To whoever else is down down voting this, try reading the 2.2.2 docs and actually trying it first. This was a big gotcha back then.
Where would I go about placing partial files shared by more than one model?
I have a page called crop.html.erb that is used for one model - Photo.
Now I would like to use it for another model called User as well.
I could copy and paste the code but that's not very DRY, so I figured I would move it into a partial.
Since it's shared between two models - where would I place that partial?
Thanks!
The Rails convention is to put shared partials in /app/views/shared.
Update
Layout inheritance is now in the guides under layout and rendering
Template inheritance works similarly.
Rails 3.1 and following versions implement template inheritance, so I think the correct place for shared partials is now /app/views/application/, say you are in products#index you can do the following:
-# products#index
= render #products.presence || 'empty'
-# /app/views/application/_empty.html.haml
There are no items
btw it's application because the connection is the controller inheritance, so this assumes ProductsController < ApplicationController
This way if you implement /app/views/products/_empty.html.haml that will be taken, the above is a fallback for all the missing partials, and I can't check right now, but I think even for the template itself...
Railscast: template inheritance!
TL;DR
Rails 3.1, Rails 4, Rails 5 and whatever comes next
app/views/application
The engine searches this path automatically if the view is not found in the controller path.
Rails 3 and prior
app/views/shared
The engine does NOT search this path automatically.
Long story
Rails 3 (and prior version) have no default location for storing shared views.
The unofficial convention is to store shared views in app/views/shared. Wherever you'd end up storing them though, you have to specify the path
# render app/views/shared/menu.html.erb
<%= render :partial => "shared/menu" %>
This suggestion was popularized by Agile Web Development with Rails.
Rails 3.1 introduces an official standard for where to store shared views:
app/views/application
Thanks to this standard, the engine now automatically looks for templates in app/views/application. As a result, you don't have to use the full path anymore.
Those curious can follow here the thought process behind this decision.
Old syntax
# render app/views/application/menu.html.erb
# unless menu.html.erb is found in appp/views/my_controller
<%= render :partial => "menu" %>
New syntax
# render app/views/application/menu.html.erb
# unless menu.html.erb is found in appp/views/my_controller
<%= render partial: "menu" %>
Of course, you can still place your shared views wherever you want and reference them by path
<%= render :partial => "my_own_special_shared_folder/menu" %>
Unless you have a very good reason to do this though, please stick to the new standard and store your shared views in app/views/application.
The Rails View uses app/views/layouts for shared partials like header and footer, but the Ruby on Rails Guide uses app/views/shared in an example. I guess it comes down to personal preference. I would use layouts for high-level stuff like a main nav or a footer, but shared for more narrow controller-level stuff like a form.
I general have a shared folder in my views that contains commonly used partials.
I arrived here in 2021 (rails 6) and got confused by the answers (many different ways).
I asked some senior rails developers what they'd do and they also gave me 2 different answers.
But TL;DR
Create a folder called 'shared' or 'application' inside your views folder (e.g. app/views/shared or app/views/application.
Then simply move the partial there, and access it with either
<%= render partial: 'shared/socials' %>
# or
<%= render partial: 'application/socials' %>
or even simpler
<%= render 'shared/socials' %>
# or
<%= render 'application/socials' %>
It doesn't matter where you put them. You can render any partial at any arbitrary location by providing the file's path to render - it doesn't need to be associated with the controller that's rendering it. I use a directory simply called partials under the view directory, and call partials in it like this:
render :partial => 'partials/mypartial'