Rendering controller/action within a different view - ruby-on-rails

I am looking for a way in Ruby on Rails to render a completely different controller's action within other views.
For example I am writing a band website that displays their albums. So I have /albums/list that shows a list of all of their albums with other stuff around the page as the layout. Now what I am looking to do is also render /news/list in every page as part of the layout as well so that every page you go to you can see it.
I cannot find a way to call the news controller list action and display it in the list view using partial views. Any help would be appreciated.

http://api.rubyonrails.org/classes/ActionView/Helpers/RenderingHelper.html#method-i-render
render() method give you ability to render specific file, placed in any directory :)
also read this article - http://blog.plataformatec.com.br/2012/01/my-five-favorite-hidden-features-in-rails-3-2 especially about "Custom partial paths".

Related

Both create and list in the same view MVC4?

I am new to MVC4. I know basic CRUD functionality in MVC, but how can I achieve both create and list in the same view? Like after a user creates data the user will automatically be redirected to the same view and view the list of data.
You may use partial views to accomplish this. Create the create / edit pages the same way as you do and below the form you may render a partial view that would render the list with even edit / delete buttons.
Alternatively, you may pass both the form data (in case of edit) and the list data to a view to render there.
I would suggest you to use the partial views approach as it would simplify the view and the partial view can be reused on some other page also.
Please take this as a starting point and not as a follow-it-blindly solution.

Rails: Nested views or partials or..?

I new to Ruby on Rails, and I am coming from Drupal/PHP background.
I am trying to do something simple. A "Static" controller for some pages like about us, disclaimer etc. and a "Video" controller for videos.
The whole application has same look. I am trying to figure out how I can avoid duplicating views.
In drupal the theming goes like this:
html.tpl.php (renders page.tpl.php)
- page.tpl.php (renders multiple block.tpl.php)
- block.tpl.php (renders more nested tpls or some html generated by theme functions).
I find this to be great.
In rails, what I read is there is application.html.erb (Which I think is similar to html.tpl.php of Drupal, as it normally contains stylesheets, js, etc. available throughout the application) called "Layout". Then there are "Views", which "yield" content in this layout. We specify the view in controller, or it gets one automatically. I think of a view as "page.tpl.php" of Drupal.
Most of my site uses same structure i.e. header, top menu, content, right sidebar, footer. But content of each area changes depending on the path. So I am thinking of doing it like this:
Both 'Static' & 'Video' will call the same view, with different variable values i.e. values for content area and sidebar.
This View will the use 'render' to render another view, with a subset of variables passed from controller i.e. it will render content & sidebar views.
If needed, those views will render more views, and so on.
Another one I read about is partials. I can't seem to differentiate between rendering a view inside another view, and rendering a partial.
Which method is better? Or is there a better "Rails Way"?
This is quite a large topic to cover but edited highlights are
If you want to repeat content on every page, like a header or navbar for example then put it in layouts/application.html.erb
if you want to keep your views tidy with content specific to that page then you can use partials. Normally in views I create a folder called shared and then put my partials in there. for example if i save a partial called _form.html.erb (make sure you name partials with the underscore at the beginning of the file name), i would then call that in my view like
<%= render 'shared/form' %>
you can also use partials in the layouts/application file to keep that clean aswell
This is just my way of doing it, I'm sure more experienced rails guys have better ways
Hope that helps, any more questions then just ask

Add View Dialog MVC3

I am busy changing the default T4 Templates that gets generated when I click on the "Add View" dialog from my Controller. I know how to change the templates. But the following i can not wrap my head around.
I want to be able to generate the following when the user clicks on Add View.
A partial view that only contains the Form part of the view that is generated by default
A View that calls the partial view generated above.
Is it possible to generate 2 files like this using the T4 Templates ?
thanks in advance
Why? Are you going to be routinely doing this exact same thing over and over again? If so, then a T4 template may make sense. If you're doing it once or twice, then just modify the files by hand.

ASP.NET MVC loading multiple partial views into a single div using JQuery

I am working on a help page which is composed of a navigation tree, content box, and search box. The navigation tree has links to Frequently Asked Questions and Glossary, each of which are linked to an Action which return partial views.
To the right of the navigation tree I have a single content div that I would like to contain whichever partial view is selected in the navigation tree.
Success case (what I want to accomplish): clicking on one of the FAQ links calls the FAQ() method in my controller, then returns a partial view which is then displayed in my content div to the right of the navigation tree. Clicking on another item would cause another partial view to be loaded.
How do I go about this? I've read a ton of ASP.NET MVC JQuery loading blog posts and tutorials but can't find anyone who's done exactly this.
Many thanks!
You should be able to use the jQuery method .load() to load HTML into your div.
http://api.jquery.com/load/
You can create an action that returns the partial view as HTML.
ASP.NET MVC Returning Partial View as a full View Page
jQuery:
one easy way you can do is load all partial views in "Container Div" at page load in one go (if performance is not a issue)
then assign each partial div with different div id inside "container", than use jquery to control show(); hide(); for each div.
MVC:
however if i were you, "Glossary" and "FAQ" looks same model to me it shouldn't be put in different partial view in first place.
if they did designed in separate model, in this scenario, i would recommend you to create a proxy class as a ViewModel above models you want to display, and then load it with one partial view only

What is the quickest, easiest and lowest cost way to populate a bootstrap modal form

As an example, I have a table of items with edit buttons on each item:
I want to populate an edit form in a bootstrap modal as quickly, efficiently and as easily as possible.
Currently, I have tried populating via Javascript into a partial but this is a bit bulky and doesn't suit my needs when there are specific functions, varying inputs and different ways to input data. In some of my tables, the editing functions require 500+ lines of Javascript to calculate and process a bunch of different situations.
I've also tried generating a new modal partial for each item with partial Models but in larger tables with 1000+ items, this tends to lag the page quite a bit or take a significant time to load.
I need a way to quickly populate a modal with as little code as possible. Hopefully, I'd like a globalised way to do this for any given Model.
I also need the ability to populate the form action to do a variety of different things depending on the item.
I've heard that Ajax is a possible way to do this, but as I am relatively new to web development, I am not 100% sure how to do this.
What would be incredibly useful and would solve all my issues, is a way to render the modal AFTER loading of the page and on the input of an edit button.
So page renders -> you click edit on a given item -> it then renders the edit modal. Although I don't think this is possible.
This is just an example modal form:
Thanks
Currently, I have tried populating via Javascript into a partial but this is a bit bulky and doesn't suit my needs when there are specific functions, varying inputs and different ways to input data.
I've also tried generating a new modal partial for each item with partial Models
To display editing record in popup modal, we can achieve it either with JavaScript completely or mix using partial view.
If you do not want to generate/populate popup modal for editing row via 100% JavaScript client code, you can try:
1) when you click on edit button, make ajax request to controller action that accepts parameter Id of selected item
2) in that action, you can retrieve detailed and expected information of the item you want to edit based on Id, and the action would return a partial view
3) in partial view, you can render expected input fields based on your retrieved item information
4) in ajax success callback function, dynamically populate body of popup modal with returned data
Besides, this thread discussed rendering partial view as modal popup, you can refer to it.

Resources