Take view file (haml) partial in Gem and use in Rails - ruby-on-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" %>

Related

The render class did not render the file at rails?

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" %>

How to render a normal view file using "render" method in Rails?

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.

Rails - Rendering a Partial without having to use "_" in front of the filename?

How do I render a partial without having to supply the "_" in front of the file name? Is there a parameter I can call to not use it?
This problem popped up using RABL and Backbone - using RABL requires me to have a file in my views like "index.json.rabl". But, when I use embed the JSON right on the page load (as is usual with Backbone), I'm required to call the file "_index.json.rabl". These 2 files are the exact same thing, just required to have different names. I'm looking to use just 1 file, "index.json.rabl" and force the render() function to look for that file name, without the "_".
=> EDIT
The standard solutions that people have described below don't work. It's likely a RABL issue then? The below code always goes to the views/countries/_index.json.rabl file.
In my .erb file
countryList.reset(<%=get_json("countries", "index", #countries)%>);
In my application_helper.rb file
def get_json(view_path, view_action, object)
path = view_path + '/' + view_action + ".json"
return raw(render(path, object: object, :formats => [:rabl]))
end
You can render a file by doing the following:
render :file => "filename"
From the RailsCast #322 on RABL:
<div id="articles" data-articles="<%= render(template: "articles/index.json.rabl") %>" >
Start from here, and then figure out what's wrong. But it's clear that render template: path is the syntax you want.
did you try render :template => "file_name" ?
Ok try:
<%= render :file => 'views_directory/index' %>
Where views_directory is the name of your directory in the views 8)
OLD:
If the content is the same use:
render :partial => "index"
in index.json.rabl and the content in _index.json.rabl

Universal partial?

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" %>

What statement can I use in Rails 3 to render HTML from a small static file?

I have a file in:
RAILS_ROOT/public/system/pages
It is a snippet of HTML. I would like to render it, along with other things, in one of my views.
But it seems like when I try to do a render from a view, Rails is always looking for a partial. But it doesn't pick up the file even when I name it with a leading underscore. How can I read and display this HTML snippet within a view?
have you tried with
<%= render :file => 'your/path/', :layout => false %>
inside the erb?

Resources