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
Related
I have partials created for several of my models. However, I also have a search controller that I would like to use different partials for. The reason for this is that the search controller displays a collection of various model types, so the partials I want to use in that view also display the model name.
I'm currently rendering with this line in views/search/index.hmtl.erb: <%= render #results %>.
Is there a way I can easily tell the call to render using the partials contained in the views/search directory? Or should I do this differently altogether?
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' %>
I'm trying to simplify a view from spree for a tablet, using mobile-fu, I copied the view I want to simplify and named it .tablet.erb and i also had to copy the admin layout and rename it to .tabler.erb, the problem is that the layout uses more partials, and those partials use some more, i'm trying to avoid having to copy all the files just to give them a different name or just plain change the render to tell it to use the html one.
Is there a way to tell it to use html format from that point on? like setting the format recursively?
<% self.formats = [:mobile, :html] %> on each view solved it
Got it from here
I have a page object and various template objects in my application. pages have names, descriptions urls etc and each have a relationship with a template. templates have different numbers of content boxes and relationships with other controllers (like blogs, galleries etc.).
When I am rendering a page I can work out what template is attached to the page, and what the relevant content is based on that. but I am not sure what the best way is to render the nested Items.
Are you meant to somehow render the templates view from within the other view? Or would you have to just rewrite the view altogether? In this case would I have to create an extra template view for each different template, bundle it with the page views, and then only include it if it is the right one?
Would this be the same for galleries and blogs? do they all need to be bundled with the page? Or can it be called from its proper location?
I'm not sure what the best practice is here and haven't had any luck googling it. I'm suspecting that the key words im using aren't correct. Or this is common knowledge that isn't worth documenting.
You can use shared partials to render views. Check out this guide.
In the views, you can render the partials based upon whatever condition you want.
For example:
- if params[:page] == "my_page"
= render "shared/my_page"
Naturally, you will still need to set up the needed data in the controller.
Shared logic for this can be placed in the Application Controller.
Just learning rails and looking for best practices help. I have the same data table that is reused across several different views (index, search results) for my controller. In an attempt to keep it DRY I have the table code in a helper method using html<< for table HTML.
I realize that now I've pulled a chunk of my HTML into the controller which I'm not a big fan of. How is this situation of having a chunk of HTML you plan to reuse across several views handled best?
Thanks!
What you want are partials. You put the partials in the same folder as the views, but partials start with an underscore (e.g: app/views/user/_my_partial.html.erb). In this partial you can put the shared HTML code, and access it from the view with:
<%= render "my_partial" %>
Yes you skip the underscore when you access the partial.
See the rails guide for more information on partials.