How to include multiple views in one page? - ruby-on-rails

I'm turning a pure HTML website into a small Rails app and have come across an issue.
Currently I have a index.html page and a translation.html page (which displays index in another language). There is currently a link on index.html to translate the page and vice verse.
I have set index.html as the 'show' action, but am unsure how to handle translate.html page. Both will have the same information/Rails form.

Make the embed-able content in translate.html into a partial, like _translated.html. Then you include the partial _translated.html (that's what the "_" means; a "partial" view) in each of the translate.html and the index.html pages. Read up on partials, and use a code like
render :partial => 'translated'
in each of your main view pages.

Related

How to accomplish this MVC layout

Being relatively new to MVC I have been struggling for the past several weeks getting my layout to work.
I have managed to get myself really twisted into knots. So instead of trying to explain and unravel my mess perhaps instead someone could explain how I would accomplish the following at a high level.
_Layout this would have all the css js etc. It would also have basic structure.
Of course HTML tags not allowed in code block....each render is in a div.
#RenderPartial(Header)</div>
#RenderBody()</div>
#RenderPartial(Footer)</div>
RenderBody is Index.cshtml and it would be broken into three pieces
#
#Html.Partial(NavMenu, model)</div>
#Html.Partial(SubNavMenu, model)</div>
#Html.Partial(MainContent, model)</div>
I have this basic layout and it looks fine until you click one of the menu items.
The menu items render as:
<a class="k-link" href="/stuffroute">Stuff</a>
That route goes to a controller that returns a view and that navigates away from the above arrangement in Index.cshtml. So I end up with the header, footer, and subdash nav....
So the question is...
How do I route / orchestrate my layout to not lose the differing pieces?
Partials don't do anything for you here. You're essentially asking about how to create SPA (single page application), although in this case your application will have other pages, it's just that the index view will act like a SPA.
That requires JavaScript, specifically AJAX, to make requests to endpoints that will return HTML fragments you can use to replace portions of the DOM with. For example, clicking "Stuff 1" causes an AJAX request to be made to the URL that routes to FooController.GetSubNav([stuff identifier]). That action then would use what was passed to it to retrieve the correct sub-nav and return a partial view that renders that sub-nav. Your AJAX callback will then take this response, select a portion of the DOM (specifically the parent of the sub-nav) and insert the new HTML as its innerHTML.
If you're going to be doing a lot of this, you'll want to make use of some client-side MVC-style JavaScript library, like Angular for example. These make it trivial to wire everything up.

MVC Partial View - Link to location on page

Let's say I have an Html page with divs.
If I want to link to a specific location on that page, I can create a url like http://www.example.com#mylocation
Now let say I'm calling that page as a partial view instead. Can I still get to those page locations?
#Html.Partial("_MyPartialView")
Yes, you can still get to those page locations.
Rendering a partial doesn't do anything special; the final HTML is the same as if you pasted the partial's contents into the same place. (You can even access ViewBag and it should have the same variables in it from the controller.)
If you view the final page source, you'll still see your HTML anchors. You can manually append #mylocation in your browser URL and see that it still works.

Best techniques to ajaxify Rails app?

I'm currently ajaxifying my Rails app as follows.
JS
application.js
$("a").live("click", function() {
$.getScript(this.href);
//do something
return false;
});
Views
index.js.erb
$("#core").html("<%= escape_javascript(render 'index') %>");
index.html.erb
<%= render 'index' %>
_index.html.erb
#my partial
So when a user clicks a link, it will be intercepted and the corresponding js file will be executed, which renders a partial in a div. This means that, for each action, I will need 3 views, say index.js.erb, _index.html.erb, and index.html.erb.
This is painstaking to set up, and the index.html.erb file is somewhat useless, it just renders the partial (perhaps there's a way to render a full view from another view directly, hence eliminating the need for a partial?).
Is this the best way to do things? How do you usually imbricate Ajax with Rails?
Thanks.
Javascript MVC seems like it works well with MVC style backends. It standardizes ajax calls, controllers and views in a similar way that you are showing above:
http://javascriptmvc.com/

Different layout for iframe

I have a rails app with content other websites need to access via iframe.
The content should have a different layout when shown on the websites (no menu bar etc.)
I made a new layout file called iframe.html.erb
How can I check, whether the page is called form an external iframe so the right layout file is used?
As far as I know when you doing
<iframe src="www.google.pl"></iframe>
you have no control over layout or styles of page display in iframe unless you own the page and can make it look whatever you like.
EDITED
If you displaying your own site go like this:
<iframe src="/some_site_that_i_can_change_code_in?from=iframe"></iframe>
and then in controller of some_site_that_i_can_change_code_in:
if params[:from] == "iframe"
render :layout => "for_iframe"
else
render :layout => "normal"
end
A good way to control the specific layout and content when serving an iframe is to register an "iframe" mimetype.
## config/initializers/mime_types.rb
Mime::Type.register 'text/html', "iframe"
Create a view that matches the controller action being served ie: show.iframe.haml. Then, when a request comes in with format: iframe it'll render the iframe version.
That way you can control exactly what's in the iframe on other sites. No need to get crazy in the controller.
I think the only way to do this is with Javascript and then redirect, but it's kind of messy and not really a good idea. See this thread for more info: Detecting if this is an iframe load or direct

Rails include page

How would I include a page from the public folder in one of my views? I want to include a single header like I do in PHP so when I make a change it affects all the other pages. (It is for multiple views)
For the most part, the only web pages in the public folder will be purely static (custom 404 pages, FAQ, etc). If you have a piece of static HTML that you'd like to be displayed in a lot of pages, a partial is what you're looking for.
A partial doesn't have to be tied to a controller. You can create the sub-directory:
/views/static
and fill it with a whole bunch of partials, so you would have:
/views/static/_my_first_partial.html.erb
/views/static/_my_second_partial.html.erb
and anywhere you want those fragments, you can do:
render :partial => "static/my_first_partial"
and voila, you're done!
I'm pretty new to rails, but I guess for such task you would use Layouts:
Layouts and Rendering

Resources