RoR, where do I put generally used templates? - ruby-on-rails

I am new to Ruby on Rails. I know that for each controller you have a specific views folder that holds all of it's views. I also know there is the layout folder for the layouts.
But what if I have a bit of a template that keeps popping up in many templates across the system but it's not a footer or header or otherwise layout related.
I want to refer to it using the <%= render.... %> command but where should I put this template?
Is there a generally agreed upon location?
Can I just create a directory under views and store it there?

Rails will automatically look in 'views/application' and in the folder that contains the current parent view.
That said, you can place partials anywhere you like, and refer to them like so:
<%= render 'foo/bar' %>
As #apneadiving suggests, 'shared' is a good name for the folder.
<%= render 'shared/bar' %>

Related

Use template to create multi-page ruby on rails web application

Is there any way to create a webpage template that I will be applying to all my webpages?
I am new to ruby on rails, I have gained enough knowledge to understand how flow works in it but can't find out the way to use the same page-template for all pages on the site.
I am using RubyMine but can work on command prompt if required.
Any help would be greatly appreciated.
app/views/layout/application.html.erb this is a common layout in which you will found <%= yield %> which render all the pages in <body> tag. Now as per your requirement you want some common template to show on all pages.
So better to make one partial file..For example, Header and Footer remains same in whole site. For doing this, make one partial file called _header.html.erb for header part and _footer.html.erb for footer part. Put these files under app/views/layout/_your_partialfile.html.erb
Then render them like:
<%= render partial: "/layouts/header" %>
<%= yield %>
<%= render partial: "/layouts/footer" %>
For more info refer : http://guides.rubyonrails.org/layouts_and_rendering.html
I hope this makes you clear to understand now. :)
In general, rails projects have a file in app/layouts/application.html.haml that's applied to every single page you load. You can put navbars there, login links, etc.

Is there anyway to create a centralized partial in Rails 4?

I am pretty new to Rails. One of the requirement is to have a Filter Panel that would appear along each record list and will be used to filter records based on criteria. What actually I am looking for an HTML based UI of it that will contain input fields along with labels that I would like to pass from Controller. Since I will be using it across views so I don't want to put in a view specific folder. What's the best way to accomplish this?
You can actually render partials from any folder. For example, in users/show.html.erb you can render a partial _info from, say, transactions.
<%= render 'transactions/info' %>
A common thing to do is to put such shared partials into a separate directory with a descriptive name (I use "shared").
<%= render 'shared/filter_panel' %>
you can put them in /views/application/ dir, rails automatically look for partials in this directory
if you use the application directory then you can just do render 'partial' from any view and it will render /views/application/_partial.html.erb
you can also create for e.g. /views/admin/base dir (if you have admin/base_controller.rb) and put your admin namespace partials there
if you have many partials, I recommend you put them into subdirectories

rails pass instance variable to layout or local variable

I have a navigation bar (in fact two) and I use a before action in some controllers to fill it's dynamic data (the second bar may not exist in some), I've seen a lot of complains about not passing a lot of instance variables to views, and all of them suggested passing locals in render. I've been wondering using a instance variable to generate these stuff in the main layout is a good idea or not, and if it's not, how should I do this, render seems to overwrite the default view and I use the data in the main layout only
I not sure that I understand well your question. But for some of my menus I use something like that in my layouts:
<%= yield(:menu_top) %>
and I use
content_for :menu_top
to generate content in this area.
For exemple:
<%= content_for :menu_top do %>
<li>my specific content or var</li>
<% end %>
Here is the rails guide for content_for: link

Rails - File path to load content from a yaml file

I'm have a variety of text files with static long form text as content. Right now I am storing them in a separate "content" file in the config folder. For instance "../config/content/content1.yml" "../config/content/content2.yml" and so on.
I would like to string these files together in my application. So in my controller I have variables that attempt to pull the content of each file, for example
#content1 = YAML.load_file("#{Rails.root}/app/config/content/content1.yml")
#content2 = YAML.load_file("#{Rails.root}/app/config/content/content2.yml")
I then try load that variable into my view with
<%= #content1 %>
<%= #content2 %>
This and everything else I've tried doesn't seem to work though. I'd really just like to get the text to display in my view. Any help to point me in the right direction would be much appreciated. I'm very noobish with rails still.
I don't know why you don't want to store this data in DB, but lets describe what you need using only views.
According to comments that what you need are partials.
Example:
Lets have file app/views/content/editor1/paragraph1.html:
Example paragraph
Then in your view you can render this using:
<%= render partial: 'content/editor1/paragraph1' %>

How do you handle widgets in rails?

StackOverflow, for example, has a user's reputation displayed up top. Clearly, this is grabbed from the database, and it's displayed on every page. Of course, this isn't in every controller action on every page, because that would be incredibly redundant.
How do you handle this kind of situation in rails? The only way I can think of it is to use before_filters to pass the models into the page, but that just seems like abuse of that feature. There seems to be the cells gem that does what I want, but I'd imagine this is a common problem and there must be a simple solution for it in rails without having to resort to plugins or gems.
What you are looking for is the layout. In rails this is where you define headers, footers, and sidebars that frame your site. Look for app/views/layouts/application.html.erb in your generated rails code. Towards the bottom you will see:
<body>
<%= yield %>
</body>
The yield is where rest of the app gets invoked. Everything before and after the yield will appear on every page. So, using your example, you might query the database and set the instance variable #reputation in the application controller:
#reputation = User.find( current_user ).reputation
then display it in the layout like this:
<body>
<%= #reputation %>
<%= yield %>
</body>
This is covered thoroughly in the book "Agile Web Development With Rails". If you are going to develop in Rails I recommend getting the latest edition.
I would just make a partial with the widget in it and render it in the layout(s) where you want it to appear. Let it do whatever it needs to do, eg connect to the db, run some js to connect to an external site, etc.
If you're concerned about optimisation then deal with it when it becomes a problem.
I guess, you can put the code you need into a view helper. And then render some partial, like it was said before, in the layouts where you want it to appear, calling helper's function.
Look here:
Rails view helpers in helper file

Resources