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
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 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 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.
I have 2 view files, index.html.erb and index.js.erb. The erb file obviously contains all the data to be sent to the yield call.
Now I want to insert the content of index.html.erb into #contents div on the page, using index.js.erb.
Is there a way to render index erb file into a variable in the js.erb file, so I can send it to the client? Currently I'm having to create a partial which contains the complete html.erb file, and calling that from both the main views.
In your index.js.erb (here example for post resource):
<%= render template: "posts/index.html" %>
<%= render template: "posts/index.html" layout: false %>
Using render is quite good described in rails guides.
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" %>