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" %>
Related
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" %>
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 have two different folders in View folder, lets say them A and B.
In B folder, the content is generated by scaffold, so there is 5 html.erb files inside: _form.html.erb, edit.html.erb, index.html.erb, new.html.erb and show.html.erb.
What I need is only index.html.erb, edit.html.erb and _form.html.erb, which is because users are only allowed to edit the content and do nothing else.
In A folder, I have only 1 index.html.erb, and I want everything shows on this file. How to render the index and form file to it? I want users could see the content of index.html.erb and they can also click 'edit' to go to the form.html.erb. And the form should be in my A folder's index file, too..
This information will help you: Rendering an Action's Template from Another Controller
You can simply do this in Folder A / index.html.erb
<div>
<%= render 'B/form' %>
</div>
<div>
<%= render template: 'B/index' %>
</div>
Now these files will be rendered in folder's A index file
From what you've written, it seems you may benefit from prepend_view_path:
#app/controllers/b_controller.rb
Class BController < ApplicationController
before_filter :set_path
private
def set_path
self.prepend_view_path "app/views/controller-a/"
end
end
I have a couple of layouts main.html.haml and alternative.html.haml, which share some elements. I would like to have a partial to share between them.
Using render ='my_partial' doesn't seem to work. How should I go about it?
If you have created partial inside layouts folder then
try this
<%= render :partial => 'layouts/partial' %>
Hope this will help you
I have a Home page with 2 Bootstap tabs. I'm having problems with the 2nd tab. I'm trying to render a list of workorders. For that list I'm using dataTables and ajax.
My issue is that workorders/index7.html.erb works fine as a stand alone page. But, I want to use it in a Bootstrap tab on the Home page.
For all my other Bootstrap tab lists, I'm using partials from within the same controller. But, in this case, I need to use a view from the Workorder controller not the Home controller.
This is the code I'm trying in the Home view:
<div class="tab-pane" id="tab2">
<%= render "workorders/index7" %>
</div>
That code gives me:
Missing partial workorders/index7
WHY? If I change the file name to _index7.html.erb the code won't execute to get the json.
But, because it's a partial, starting with _, the connection to the Workorder controller code for index7 doesn't work. I also tried _index7 in the Workorder controller.
The Workorder controller has this code:
def index7
#workorders = Workorder.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: Workorders3Datatable.new(view_context) }
end
end
That code uses a file called workorders3_datatable.rb to get the data via ajax and format it for dataTables.
QUESTIONS:
1) Do I have to use a partial in the Home tab? Or is there a way to render a file called workorders/index7.html.erb in the tab?
2) Why does index7.html.erb use the controller code for index7, yet the partial _index7.html.erb won't use code if I call it _index7 in the controller?
3) I read this "Render will just render the view, it won't run the action associated with it." - is that true?
Thanks for your help - I know this is confusing.
Reddirt
UPDATE 1
The view runs great as workorders/index7 - but, if I change it to a partial and put it in a tab = workorders/_index7 - the controller code for index7 doesn't seem to execute.
1) Do I have to use a partial in the Home tab? Or is there a way to view a file called workorders/index7.html in the tab?
If you want to follow this approach. From Home controller pass the collection (#workorders = Workorder.all)
Then in the home view pass it to the workorders/index7 as locals
<div class="tab-pane" id="tab2">
<%= render "workorders/index7" locals: {workorders: #workorders} %>
</div>
This will make it available to workorders/index7 do remember to change the variable to a local variable workorders.
You need to include :partial in the view for your Home page:
<%= render :partial => "workorders/index7" %>
The app/views/workorders folder will need a file called _index7.html.erb. To accomplish this from your current setup, you can move the contents of index7.html.erb into this partial, and then use:
<%= render :partial => "index7" %>
in the original index7.html.erb. That way you can render that as a whole page and within your Home page as a partial.